Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/commands/project/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe("Initializing a blank standard repo", () => {
const filepaths = [
"bedrock.yaml",
"maintainers.yaml",
"azure-pipelines.yaml"
"azure-pipelines.yaml",
"Dockerfile"
].map(filename => path.join(randomTmpDir, filename));

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

const gitIgnoreFilePath = path.join(subProjectDir, ".gitignore");
expect(fs.existsSync(gitIgnoreFilePath)).toBe(true);

const dockerfilePath = path.join(subProjectDir, "Dockerfile");
expect(fs.existsSync(dockerfilePath)).toBe(true);
}

// azure-pipelines.yaml should not be in the root
expect(fs.existsSync(path.join(randomTmpDir, "azure-pipelines.yaml"))).toBe(
false
);

expect(fs.existsSync(path.join(randomTmpDir, "Dockerfile"))).toBe(false);
});
});
4 changes: 3 additions & 1 deletion src/commands/project/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import shelljs from "shelljs";
import * as config from "../../config";
import {
generateDockerfile,
generateGitIgnoreFile,
generateStarterAzurePipelinesYaml
} from "../../lib/fileutils";
Expand Down Expand Up @@ -71,7 +72,7 @@ export const initCommandDecorator = (command: commander.Command): void => {
};

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

logger.info(`Project initialization complete!`);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/service/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Adding a service to a repo directory", () => {
expect(fs.existsSync(serviceDirPath)).toBe(true);

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

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

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

Expand Down
4 changes: 4 additions & 0 deletions src/commands/service/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { logger } from "../../logger";
import {
addNewServiceToBedrockFile,
addNewServiceToMaintainersFile,
generateDockerfile,
generateGitIgnoreFile,
generateStarterAzurePipelinesYaml
} from "../../lib/fileutils";
Expand Down Expand Up @@ -212,6 +213,9 @@ export const createService = async (
// Create empty .gitignore file in directory
generateGitIgnoreFile(newServiceDir, "");

// Create simple Dockerfile in directory
generateDockerfile(newServiceDir);

// add maintainers to file in parent repo file
const newUser = {
email: maintainerEmail,
Expand Down
39 changes: 39 additions & 0 deletions src/lib/fileutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IBedrockFile, IHelmConfig, IMaintainersFile } from "../types";
import {
addNewServiceToBedrockFile,
addNewServiceToMaintainersFile,
generateDockerfile,
generateGitIgnoreFile,
generateHldAzurePipelinesYaml
} from "./fileutils";
Expand Down Expand Up @@ -197,3 +198,41 @@ describe("Adding a new service to a Bedrock file", () => {
);
});
});

describe("generating service Dockerfile", () => {
const targetDirectory = "my-new-service";

beforeEach(() => {
mockFs({
"my-new-service": {}
});
});
afterEach(() => {
mockFs.restore();
});

it("should not do anything if file exist", async () => {
const mockFsOptions = {
[`${targetDirectory}/Dockerfile`]: "hello!!!!"
};
mockFs(mockFsOptions);

const writeSpy = jest.spyOn(fs, "writeFileSync");
generateDockerfile(targetDirectory);
expect(writeSpy).not.toBeCalled();
});

it("should generate the file if one does not exist", async () => {
const writeSpy = jest.spyOn(fs, "writeFileSync");
generateDockerfile(targetDirectory);

const absTargetPath = path.resolve(targetDirectory);
const expectedGitIgnoreFilePath = `${absTargetPath}/Dockerfile`;

expect(writeSpy).toBeCalledWith(
expectedGitIgnoreFilePath,
"FROM alpine\nRUN echo 'hello world'",
"utf8"
);
});
});
28 changes: 28 additions & 0 deletions src/lib/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,31 @@ export const addNewServiceToBedrockFile = (
logger.info("Updating bedrock.yaml");
fs.writeFileSync(bedrockFilePath, yaml.safeDump(bedrockFile), "utf8");
};

/**
* Writes out a default Dockerfile if one doesn't exist
*
* @param targetDirectory directory to generate the Dockerfile
* @param content content of file
*/
export const generateDockerfile = (targetDirectory: string) => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating starter Dockerfile in ${absTargetPath}`);

const dockerfilePath = path.join(absTargetPath, "Dockerfile");

if (fs.existsSync(dockerfilePath)) {
logger.warn(
`Existing Dockerfile found at ${dockerfilePath}, skipping generation.`
);

return;
}

logger.info(`Writing Dockerfile to ${dockerfilePath}`);
fs.writeFileSync(
dockerfilePath,
"FROM alpine\nRUN echo 'hello world'",
"utf8"
);
};