Skip to content

Commit

Permalink
feat(init): create .gitignore with lerna init (#3270)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jul 29, 2022
1 parent dfd30fa commit 3461348
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions commands/init/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ When run, this command will:

1. Add `lerna` as a [`devDependency`](https://docs.npmjs.com/files/package.json#devdependencies) in `package.json` if it doesn't already exist.
2. Create a `lerna.json` config file to store the `version` number.
3. Generate a `.gitignore` file if one doesn't already exist.

Example output on a new git repo:

Expand Down
26 changes: 26 additions & 0 deletions commands/init/__tests__/init-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ describe("InitCommand", () => {
expect(gitDirExists).toBe(true);
});

it("initializes .gitignore", async () => {
const testDir = tempy.directory();

await lernaInit(testDir)();

const gitIgnorePath = path.join(testDir, ".gitignore");
const gitIgnoreContent = await fs.readFile(gitIgnorePath, "utf8");

expect(gitIgnoreContent).toMatchInlineSnapshot(`"node_modules/"`);
});

it("initializes git repo with lerna files in independent mode", async () => {
const testDir = tempy.directory();

Expand Down Expand Up @@ -147,6 +158,21 @@ describe("InitCommand", () => {
});
});

describe("when .gitignore exists", () => {
it("does not change existing .gitignore", async () => {
const testDir = await tempy.directory();

const gitIgnorePath = path.join(testDir, ".gitignore");
await fs.writeFile(gitIgnorePath, "dist/");

await lernaInit(testDir)();

const gitIgnoreContent = await fs.readFile(gitIgnorePath, "utf8");

expect(gitIgnoreContent).toMatchInlineSnapshot(`"dist/"`);
});
});

describe("when package.json exists", () => {
it("adds lerna to sorted devDependencies", async () => {
const testDir = await initFixture("has-package");
Expand Down
14 changes: 14 additions & 0 deletions commands/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class InitCommand extends Command {
execute() {
let chain = Promise.resolve();

chain = chain.then(() => this.ensureGitIgnore());
chain = chain.then(() => this.ensureConfig());
chain = chain.then(() => this.ensurePackagesDir());

Expand All @@ -51,6 +52,19 @@ class InitCommand extends Command {
});
}

ensureGitIgnore() {
const gitIgnorePath = path.join(this.project.rootPath, ".gitignore");

let chain = Promise.resolve();

if (!fs.existsSync(gitIgnorePath)) {
this.logger.info("", "Creating .gitignore");
chain = chain.then(() => fs.writeFile(gitIgnorePath, "node_modules/"));
}

return chain;
}

ensureConfig() {
const hasExistingLernaConfig = !!this.project.version;
const hasExistingPackageJson = !!this.project.manifest;
Expand Down
8 changes: 8 additions & 0 deletions e2e/tests/lerna-init/lerna-init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("lerna-init", () => {
expect(output.stderr).toMatchInlineSnapshot(`
"lerna notice cli v999.9.9-e2e.0
lerna info Initializing Git repository
lerna info Creating .gitignore
lerna info Creating package.json
lerna info Creating lerna.json
lerna info Creating packages directory
Expand Down Expand Up @@ -50,6 +51,7 @@ describe("lerna-init", () => {
}
"
`);
expect(await fixture.readWorkspaceFile(".gitignore")).toMatchInlineSnapshot(`"node_modules/"`);
});

describe("--independent", () => {
Expand All @@ -59,6 +61,7 @@ describe("lerna-init", () => {
expect(output.stderr).toMatchInlineSnapshot(`
"lerna notice cli v999.9.9-e2e.0
lerna info Initializing Git repository
lerna info Creating .gitignore
lerna info Creating package.json
lerna info Creating lerna.json
lerna info Creating packages directory
Expand Down Expand Up @@ -89,6 +92,7 @@ describe("lerna-init", () => {
}
"
`);
expect(await fixture.readWorkspaceFile(".gitignore")).toMatchInlineSnapshot(`"node_modules/"`);
});
});

Expand All @@ -99,6 +103,7 @@ describe("lerna-init", () => {
expect(output.stderr).toMatchInlineSnapshot(`
"lerna notice cli v999.9.9-e2e.0
lerna info Initializing Git repository
lerna info Creating .gitignore
lerna info Creating package.json
lerna info Creating lerna.json
lerna info Creating packages directory
Expand Down Expand Up @@ -134,6 +139,7 @@ describe("lerna-init", () => {
}
"
`);
expect(await fixture.readWorkspaceFile(".gitignore")).toMatchInlineSnapshot(`"node_modules/"`);
});
});

Expand All @@ -144,6 +150,7 @@ describe("lerna-init", () => {
expect(output.stderr).toMatchInlineSnapshot(`
"lerna notice cli v999.9.9-e2e.0
lerna info Initializing Git repository
lerna info Creating .gitignore
lerna info Creating package.json
lerna info Creating lerna.json
lerna info Creating packages directory
Expand Down Expand Up @@ -179,6 +186,7 @@ describe("lerna-init", () => {
}
"
`);
expect(await fixture.readWorkspaceFile(".gitignore")).toMatchInlineSnapshot(`"node_modules/"`);
});
});
});
1 change: 1 addition & 0 deletions integration/__fixtures__/lerna-init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions integration/lerna-init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("lerna init", () => {
expect(stderr).toMatchInlineSnapshot(`
lerna notice cli __TEST_VERSION__
lerna info Initializing Git repository
lerna info Creating .gitignore
lerna info Creating package.json
lerna info Creating lerna.json
lerna info Creating packages directory
Expand Down

0 comments on commit 3461348

Please sign in to comment.