Skip to content

Commit

Permalink
Change exit codes for updated and publish (#807)
Browse files Browse the repository at this point in the history
It is now possible to run `lerna publish` in CI unconditionally, only publishing when changes are actually detected, and never failing when it decides to not publish anything.

Previously:
- `lerna publish` when there are no updates to publish would throw an error
- `lerna updated` when there are no updates would `exit 0`, making it ineffective as a chained filter (e.g., `lerna updated && lerna publish`)

Now:
- `lerna publish` when there are no updates is a no-op, exiting successfully with a helpful log message
- `lerna updated` when there are no updates will exit non-zero (but _not_ throw an error), enabling it to be an effective filter

Fixes #802
  • Loading branch information
koddsson authored and evocateur committed May 18, 2017
1 parent 03eb4e9 commit 880fdf3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/commands/PublishCommand.js
Expand Up @@ -124,7 +124,8 @@ export default class PublishCommand extends Command {
: [packagesToPublish];

if (!this.updates.length) {
callback(new Error("No updated packages to publish."));
this.logger.info("No updated packages to publish.");
callback(null, true);
return;
}

Expand Down
8 changes: 7 additions & 1 deletion src/commands/UpdatedCommand.js
Expand Up @@ -19,7 +19,13 @@ export default class UpdatedCommand extends Command {
initialize(callback) {
const updatedPackagesCollector = new UpdatedPackagesCollector(this);
this.updates = updatedPackagesCollector.getUpdates();
callback(null, true);

const proceedWithUpdates = this.updates.length > 0;
if (!proceedWithUpdates) {
this.logger.info("No packages need updating");
}

callback(null, proceedWithUpdates);
}

get otherCommandConfigs() {
Expand Down
22 changes: 22 additions & 0 deletions test/UpdatedCommand.js
Expand Up @@ -221,6 +221,17 @@ describe("UpdatedCommand", () => {
}
}));
});

it("should return a non-zero exit code when there are no changes", (done) => {
gitTag({ cwd: testDir });

const updatedCommand = new UpdatedCommand([], {}, testDir);

updatedCommand.runValidations();
updatedCommand.runPreparations();

updatedCommand.runCommand(exitWithCode(1, done));
});
});

/** =========================================================================
Expand Down Expand Up @@ -330,6 +341,17 @@ describe("UpdatedCommand", () => {
}
}));
});

it("should return a non-zero exit code when there are no changes", (done) => {
gitTag({ cwd: testDir });

const updatedCommand = new UpdatedCommand([], {}, testDir);

updatedCommand.runValidations();
updatedCommand.runPreparations();

updatedCommand.runCommand(exitWithCode(1, done));
});
});
});

Expand Down
10 changes: 10 additions & 0 deletions test/integration/__snapshots__/lerna-publish.test.js.snap
Expand Up @@ -83,6 +83,14 @@ Array [
]
`;

exports[`stderr: exists with a exit code 0 when there are no updates 1`] = `
"lerna info version __TEST_VERSION__
lerna info current version 1.0.0
lerna info Checking for updated packages...
lerna info Comparing with tag v1.0.0
lerna info No updated packages to publish. "
`;

exports[`stderr: updates fixed versions 1`] = `
"lerna info version __TEST_VERSION__
lerna info current version 1.0.0
Expand All @@ -101,6 +109,8 @@ lerna info Comparing with initial commit.
lerna info auto-confirmed "
`;

exports[`stdout: exists with a exit code 0 when there are no updates 1`] = `""`;

exports[`stdout: updates fixed versions 1`] = `
"
Changes:
Expand Down
17 changes: 17 additions & 0 deletions test/integration/lerna-publish.test.js
Expand Up @@ -11,6 +11,23 @@ const lastCommitMessage = (cwd) =>
execa.stdout("git", ["log", "-1", "--format=%B"], { cwd }).then(normalizeNewline);

describe("lerna publish", () => {
test.concurrent("exists with a exit code 0 when there are no updates", async () => {
const cwd = await initFixture("PublishCommand/normal");
await execa("git", ["tag", "-a", "v1.0.0", "-m", "v1.0.0"], { cwd });

const args = [
"publish",
"--skip-npm",
"--cd-version=patch",
"--yes",
];

const { stdout, stderr } = await execa(LERNA_BIN, args, { cwd });

expect(stdout).toMatchSnapshot("stdout: exists with a exit code 0 when there are no updates");
expect(stderr).toMatchSnapshot("stderr: exists with a exit code 0 when there are no updates");
});

test.concurrent("updates fixed versions", async () => {
const cwd = await initFixture("PublishCommand/normal");
const args = [
Expand Down

0 comments on commit 880fdf3

Please sign in to comment.