Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
spk commands will append to existing .gitignore files, will create a …
Browse files Browse the repository at this point in the history
…file if needed. (#18)

* Changing to always append to existing .gitignore file, will create if needed

* updating references to spk.log to use constant

Co-authored-by: Yvonne Radsmikham <yvonne.radsmikham@gmail.com>
  • Loading branch information
mtarng and yradsmikham authored Apr 20, 2020
1 parent efb45e1 commit 0dd6d8d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/commands/hld/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { logger } from "../../logger";
import decorator from "./init.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";
import { CLI_LOG_FILENAME } from "../../lib/constants";

// values that we need to pull out from command operator
interface CommandOptions {
Expand Down Expand Up @@ -39,7 +40,7 @@ export const initialize = async (
componentPath
);
// Create .gitignore file in directory ignoring spk.log, if one doesn't already exist.
generateGitIgnoreFile(hldRepoPath, "spk.log");
generateGitIgnoreFile(hldRepoPath, [CLI_LOG_FILENAME]);

// If requested, create new git branch, commit, and push
if (gitPush) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/project/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BedrockFile, MaintainersFile } from "../../types";
import decorator from "./init.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";
import { CLI_LOG_FILENAME } from "../../lib/constants";

// values that we need to pull out from command operator
interface CommandOptions {
Expand Down Expand Up @@ -151,7 +152,7 @@ export const initialize = async (
generateBedrockFile(absProjectRoot, defaultRing);
await generateMaintainersFile(absProjectRoot, []);
await generateHldLifecyclePipelineYaml(absProjectRoot);
generateGitIgnoreFile(absProjectRoot, "spk.log");
generateGitIgnoreFile(absProjectRoot, [CLI_LOG_FILENAME]);

logger.info(`Project initialization complete!`);
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/service/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const createService = async (
);

// Create empty .gitignore file in directory
generateGitIgnoreFile(newServiceDir, "");
generateGitIgnoreFile(newServiceDir, [""]);

// Create simple Dockerfile in directory
generateDockerfile(newServiceDir);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const BEDROCK_FILENAME = "bedrock.yaml";
export const BUILD_SCRIPT_URL =
"https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/build.sh";

export const CLI_LOG_FILENAME = "spk.log";

export const HELM_VERSION = "2.16.3";

export const HLD_COMPONENT_FILENAME = "component.yaml";
Expand Down
44 changes: 31 additions & 13 deletions src/lib/fileutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
VM_IMAGE,
BEDROCK_FILENAME,
} from "../lib/constants";
import { disableVerboseLogging, enableVerboseLogging } from "../logger";
import { disableVerboseLogging, enableVerboseLogging, logger } from "../logger";
import {
createTestComponentYaml,
createTestHldAzurePipelinesYaml,
Expand Down Expand Up @@ -614,8 +614,9 @@ describe("Adding a new service to a Maintainer file", () => {
});
});

describe("generating service gitignore file", () => {
describe("generating or updating .gitignore file", () => {
const targetDirectory = "my-new-service";
const writeSpy = jest.spyOn(fs, "appendFileSync");

beforeEach(() => {
mockFs({
Expand All @@ -624,29 +625,46 @@ describe("generating service gitignore file", () => {
});
afterEach(() => {
mockFs.restore();
jest.clearAllMocks();
});

const content = "hello world";
const values: string[] = ["file1", "file2", "file3"];
const expectedContent = "\n# Bedrock files ---\nfile1\nfile2\nfile3";
const absTargetPath = path.resolve(targetDirectory);
const expectedGitIgnoreFilePath = `${absTargetPath}/.gitignore`;

it("should not do anything if file exist", () => {
it("should append values if file exist", () => {
const mockFsOptions = {
[`${targetDirectory}/.gitignore`]: "foobar",
[`${targetDirectory}/.gitignore`]: "someexistingfiletype",
};
mockFs(mockFsOptions);

const writeSpy = jest.spyOn(fs, "writeFileSync");
generateGitIgnoreFile(targetDirectory, content);
expect(writeSpy).not.toBeCalled();
generateGitIgnoreFile(targetDirectory, values);

expect(writeSpy).toBeCalledWith(
expectedGitIgnoreFilePath,
expectedContent,
"utf8"
);
expect(fs.readFileSync(expectedGitIgnoreFilePath, "utf8")).toEqual(
"someexistingfiletype" + expectedContent
);
});

it("should generate the file if one does not exist", () => {
const writeSpy = jest.spyOn(fs, "writeFileSync");
generateGitIgnoreFile(targetDirectory, content);
expect(fs.existsSync(expectedGitIgnoreFilePath)).toBe(false);

const absTargetPath = path.resolve(targetDirectory);
const expectedGitIgnoreFilePath = `${absTargetPath}/.gitignore`;
generateGitIgnoreFile(targetDirectory, values);

expect(writeSpy).toBeCalledWith(expectedGitIgnoreFilePath, content, "utf8");
expect(writeSpy).toBeCalledWith(
expectedGitIgnoreFilePath,
expectedContent,
"utf8"
);
expect(fs.existsSync(expectedGitIgnoreFilePath)).toBe(true);
expect(fs.readFileSync(expectedGitIgnoreFilePath, "utf8")).toEqual(
expectedContent
);
});
});

Expand Down
24 changes: 10 additions & 14 deletions src/lib/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,30 +932,26 @@ export const addNewServiceToMaintainersFile = (
};

/**
* Writes out a default .gitignore file if one doesn't exist
* Appends content to an existing .gitignore file in target directory.
* Will create a new file if one does not exist.
*
* @param targetDirectory directory to generate the .gitignore file
* @param content content of file
* @param values array of values to ignore
*/
export const generateGitIgnoreFile = (
targetDirectory: string,
content: string
values: string[]
): void => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating starter .gitignore in ${absTargetPath}`);
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");
logger.info(
`Attempting to add values: "${values}" to ${gitIgnoreFilePath}. Will create a new file if needed.`
);

try {
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");

if (fs.existsSync(gitIgnoreFilePath)) {
logger.warn(
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
);
return;
}
const content = ["\n# Bedrock files ---", ...values].join("\n");

logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
fs.appendFileSync(gitIgnoreFilePath, content, "utf8");
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format.",
"fileutils-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.",
"fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",
"fileutils-generate-git-ignore-file": "Could not generate .gitignore file in {0}.",
"fileutils-generate-git-ignore-file": "Could not append to existing or generate new .gitignore file in {0}.",
"fileutils-update-ring-trigger-svc-file-not-found": "Could not update ring trigger because {0} was not in path {1}.",

"git-checkout-commit-push-create-PR-link": "Could not checkout, commit, push or create pull request",
Expand Down
3 changes: 2 additions & 1 deletion src/logger/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import fs from "fs";
import path from "path";
import uuid from "uuid/v4";
import { disableVerboseLogging, enableVerboseLogging, logger } from ".";
import { CLI_LOG_FILENAME } from "../lib/constants";

const logFile = path.join(process.cwd(), "spk.log");
const logFile = path.join(process.cwd(), CLI_LOG_FILENAME);

test("A new info log gets added to logs", () => {
const randomString = uuid();
Expand Down
3 changes: 2 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createLogger, format, transports } from "winston";
import { CLI_LOG_FILENAME } from "../lib/constants";

// visit https://github.com/winstonjs/logform for format options
export const logger = createLogger({
Expand All @@ -7,7 +8,7 @@ export const logger = createLogger({
defaultMeta: { service: "spk" },
transports: [
new transports.File({
filename: "spk.log",
filename: CLI_LOG_FILENAME,
format: format.simple(),
}),
new transports.Console({
Expand Down

0 comments on commit 0dd6d8d

Please sign in to comment.