Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit c0de6a8

Browse files
authored
Adding starter Dockerfile for services, if necessairy (#82)
1 parent ceffc45 commit c0de6a8

File tree

6 files changed

+84
-5
lines changed

6 files changed

+84
-5
lines changed

src/commands/project/init.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ describe("Initializing a blank standard repo", () => {
2727
const filepaths = [
2828
"bedrock.yaml",
2929
"maintainers.yaml",
30-
"azure-pipelines.yaml"
30+
"azure-pipelines.yaml",
31+
"Dockerfile"
3132
].map(filename => path.join(randomTmpDir, filename));
3233

3334
for (const filepath of filepaths) {
@@ -79,11 +80,16 @@ describe("Initializing a blank mono-repo", () => {
7980

8081
const gitIgnoreFilePath = path.join(subProjectDir, ".gitignore");
8182
expect(fs.existsSync(gitIgnoreFilePath)).toBe(true);
83+
84+
const dockerfilePath = path.join(subProjectDir, "Dockerfile");
85+
expect(fs.existsSync(dockerfilePath)).toBe(true);
8286
}
8387

8488
// azure-pipelines.yaml should not be in the root
8589
expect(fs.existsSync(path.join(randomTmpDir, "azure-pipelines.yaml"))).toBe(
8690
false
8791
);
92+
93+
expect(fs.existsSync(path.join(randomTmpDir, "Dockerfile"))).toBe(false);
8894
});
8995
});

src/commands/project/init.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path";
44
import shelljs from "shelljs";
55
import * as config from "../../config";
66
import {
7+
generateDockerfile,
78
generateGitIgnoreFile,
89
generateStarterAzurePipelinesYaml
910
} from "../../lib/fileutils";
@@ -71,7 +72,7 @@ export const initCommandDecorator = (command: commander.Command): void => {
7172
};
7273

7374
/**
74-
* Initializes the `rootProject` with a bedrock.yaml, maintainers.yaml, and azure-pipelines.yaml file
75+
* Initializes the `rootProject` with a bedrock.yaml, maintainers.yaml
7576
* If opts.monoRepo == true, the root directly will be initialized as a mono-repo
7677
* If opts.monoRepo == true, all direct subdirectories under opts.packagesDir will be initialized as individual projects
7778
*
@@ -116,6 +117,7 @@ export const initialize = async (
116117
for (const absPackagePath of absPackagePaths) {
117118
await generateStarterAzurePipelinesYaml(absProjectRoot, absPackagePath);
118119
generateGitIgnoreFile(absPackagePath, gitIgnoreFileContent);
120+
generateDockerfile(absPackagePath);
119121
}
120122

121123
logger.info(`Project initialization complete!`);

src/commands/service/create.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("Adding a service to a repo directory", () => {
5252
expect(fs.existsSync(serviceDirPath)).toBe(true);
5353

5454
// Verify new azure-pipelines created
55-
const filepaths = ["azure-pipelines.yaml"].map(filename =>
55+
const filepaths = ["azure-pipelines.yaml", "Dockerfile"].map(filename =>
5656
path.join(serviceDirPath, filename)
5757
);
5858

@@ -90,8 +90,8 @@ describe("Adding a service to a repo directory", () => {
9090
const serviceDirPath = path.join(randomTmpDir, packageDir, serviceName);
9191
expect(fs.existsSync(serviceDirPath)).toBe(true);
9292

93-
// Verify new azure-pipelines created
94-
const filepaths = ["azure-pipelines.yaml"].map(filename =>
93+
// Verify new azure-pipelines and Dockerfile created
94+
const filepaths = ["azure-pipelines.yaml", "Dockerfile"].map(filename =>
9595
path.join(serviceDirPath, filename)
9696
);
9797

src/commands/service/create.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { logger } from "../../logger";
66
import {
77
addNewServiceToBedrockFile,
88
addNewServiceToMaintainersFile,
9+
generateDockerfile,
910
generateGitIgnoreFile,
1011
generateStarterAzurePipelinesYaml
1112
} from "../../lib/fileutils";
@@ -212,6 +213,9 @@ export const createService = async (
212213
// Create empty .gitignore file in directory
213214
generateGitIgnoreFile(newServiceDir, "");
214215

216+
// Create simple Dockerfile in directory
217+
generateDockerfile(newServiceDir);
218+
215219
// add maintainers to file in parent repo file
216220
const newUser = {
217221
email: maintainerEmail,

src/lib/fileutils.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IBedrockFile, IHelmConfig, IMaintainersFile } from "../types";
1515
import {
1616
addNewServiceToBedrockFile,
1717
addNewServiceToMaintainersFile,
18+
generateDockerfile,
1819
generateGitIgnoreFile,
1920
generateHldAzurePipelinesYaml
2021
} from "./fileutils";
@@ -197,3 +198,41 @@ describe("Adding a new service to a Bedrock file", () => {
197198
);
198199
});
199200
});
201+
202+
describe("generating service Dockerfile", () => {
203+
const targetDirectory = "my-new-service";
204+
205+
beforeEach(() => {
206+
mockFs({
207+
"my-new-service": {}
208+
});
209+
});
210+
afterEach(() => {
211+
mockFs.restore();
212+
});
213+
214+
it("should not do anything if file exist", async () => {
215+
const mockFsOptions = {
216+
[`${targetDirectory}/Dockerfile`]: "hello!!!!"
217+
};
218+
mockFs(mockFsOptions);
219+
220+
const writeSpy = jest.spyOn(fs, "writeFileSync");
221+
generateDockerfile(targetDirectory);
222+
expect(writeSpy).not.toBeCalled();
223+
});
224+
225+
it("should generate the file if one does not exist", async () => {
226+
const writeSpy = jest.spyOn(fs, "writeFileSync");
227+
generateDockerfile(targetDirectory);
228+
229+
const absTargetPath = path.resolve(targetDirectory);
230+
const expectedGitIgnoreFilePath = `${absTargetPath}/Dockerfile`;
231+
232+
expect(writeSpy).toBeCalledWith(
233+
expectedGitIgnoreFilePath,
234+
"FROM alpine\nRUN echo 'hello world'",
235+
"utf8"
236+
);
237+
});
238+
});

src/lib/fileutils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,31 @@ export const addNewServiceToBedrockFile = (
294294
logger.info("Updating bedrock.yaml");
295295
fs.writeFileSync(bedrockFilePath, yaml.safeDump(bedrockFile), "utf8");
296296
};
297+
298+
/**
299+
* Writes out a default Dockerfile if one doesn't exist
300+
*
301+
* @param targetDirectory directory to generate the Dockerfile
302+
* @param content content of file
303+
*/
304+
export const generateDockerfile = (targetDirectory: string) => {
305+
const absTargetPath = path.resolve(targetDirectory);
306+
logger.info(`Generating starter Dockerfile in ${absTargetPath}`);
307+
308+
const dockerfilePath = path.join(absTargetPath, "Dockerfile");
309+
310+
if (fs.existsSync(dockerfilePath)) {
311+
logger.warn(
312+
`Existing Dockerfile found at ${dockerfilePath}, skipping generation.`
313+
);
314+
315+
return;
316+
}
317+
318+
logger.info(`Writing Dockerfile to ${dockerfilePath}`);
319+
fs.writeFileSync(
320+
dockerfilePath,
321+
"FROM alpine\nRUN echo 'hello world'",
322+
"utf8"
323+
);
324+
};

0 commit comments

Comments
 (0)