Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): Create .gitignore with lerna init #3270

Merged
merged 3 commits into from Jul 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions commands/init/README.md
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
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
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
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
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions integration/lerna-init.test.js
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