Skip to content

Commit

Permalink
fix(version): Avoid recursive root lifecycle execution
Browse files Browse the repository at this point in the history
Fixes #1844
  • Loading branch information
evocateur committed Jan 2, 2019
1 parent b7255da commit 089392d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
20 changes: 20 additions & 0 deletions commands/version/__tests__/version-lifecycle-scripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname,
const lernaVersion = require("@lerna-test/command-runner")(require("../command"));

describe("lifecycle scripts", () => {
const npmLifecycleEvent = process.env.npm_lifecycle_event;

afterEach(() => {
process.env.npm_lifecycle_event = npmLifecycleEvent;
});

it("calls version lifecycle scripts for root and packages", async () => {
const cwd = await initFixture("lifecycle");

Expand Down Expand Up @@ -54,4 +60,18 @@ Map {
}
`);
});

it("does not execute recursive root scripts", async () => {
const cwd = await initFixture("lifecycle");

process.env.npm_lifecycle_event = "version";

await lernaVersion(cwd)();

expect(runLifecycle.getOrderedCalls()).toEqual([
["package-1", "preversion"],
["package-1", "version"],
["package-1", "postversion"],
]);
});
});
13 changes: 10 additions & 3 deletions commands/version/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ class VersionCommand extends Command {

this.runPackageLifecycle = createRunner(this.options);

// don't execute recursively if run from a poorly-named script
this.runRootLifecycle = /^(pre|post)?version$/.test(process.env.npm_lifecycle_event)
? stage => {
this.logger.warn("lifecycle", "Skipping root %j because it has already been called", stage);
}
: stage => this.runPackageLifecycle(this.project.manifest, stage);

const tasks = [
() => this.getVersionsForUpdates(),
versions => this.setUpdatesForVersions(versions),
Expand Down Expand Up @@ -403,7 +410,7 @@ class VersionCommand extends Command {
// @see https://docs.npmjs.com/misc/scripts

// exec preversion lifecycle in root (before all updates)
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "preversion"));
chain = chain.then(() => this.runRootLifecycle("preversion"));

const actions = [
pkg => this.runPackageLifecycle(pkg, "preversion").then(() => pkg),
Expand Down Expand Up @@ -487,7 +494,7 @@ class VersionCommand extends Command {
}

// exec version lifecycle in root (after all updates)
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "version"));
chain = chain.then(() => this.runRootLifecycle("version"));

if (this.commitAndTag) {
chain = chain.then(() => gitAdd(Array.from(changedFiles), this.execOpts));
Expand Down Expand Up @@ -515,7 +522,7 @@ class VersionCommand extends Command {
);

// run postversion, if set, in the root directory
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "postversion"));
chain = chain.then(() => this.runRootLifecycle("postversion"));

return chain;
}
Expand Down

0 comments on commit 089392d

Please sign in to comment.