Skip to content

Commit 089392d

Browse files
committedJan 2, 2019
fix(version): Avoid recursive root lifecycle execution
Fixes #1844
1 parent b7255da commit 089392d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
 

Diff for: ‎commands/version/__tests__/version-lifecycle-scripts.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname,
1919
const lernaVersion = require("@lerna-test/command-runner")(require("../command"));
2020

2121
describe("lifecycle scripts", () => {
22+
const npmLifecycleEvent = process.env.npm_lifecycle_event;
23+
24+
afterEach(() => {
25+
process.env.npm_lifecycle_event = npmLifecycleEvent;
26+
});
27+
2228
it("calls version lifecycle scripts for root and packages", async () => {
2329
const cwd = await initFixture("lifecycle");
2430

@@ -54,4 +60,18 @@ Map {
5460
}
5561
`);
5662
});
63+
64+
it("does not execute recursive root scripts", async () => {
65+
const cwd = await initFixture("lifecycle");
66+
67+
process.env.npm_lifecycle_event = "version";
68+
69+
await lernaVersion(cwd)();
70+
71+
expect(runLifecycle.getOrderedCalls()).toEqual([
72+
["package-1", "preversion"],
73+
["package-1", "version"],
74+
["package-1", "postversion"],
75+
]);
76+
});
5777
});

Diff for: ‎commands/version/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ class VersionCommand extends Command {
174174

175175
this.runPackageLifecycle = createRunner(this.options);
176176

177+
// don't execute recursively if run from a poorly-named script
178+
this.runRootLifecycle = /^(pre|post)?version$/.test(process.env.npm_lifecycle_event)
179+
? stage => {
180+
this.logger.warn("lifecycle", "Skipping root %j because it has already been called", stage);
181+
}
182+
: stage => this.runPackageLifecycle(this.project.manifest, stage);
183+
177184
const tasks = [
178185
() => this.getVersionsForUpdates(),
179186
versions => this.setUpdatesForVersions(versions),
@@ -403,7 +410,7 @@ class VersionCommand extends Command {
403410
// @see https://docs.npmjs.com/misc/scripts
404411

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

408415
const actions = [
409416
pkg => this.runPackageLifecycle(pkg, "preversion").then(() => pkg),
@@ -487,7 +494,7 @@ class VersionCommand extends Command {
487494
}
488495

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

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

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

520527
return chain;
521528
}

0 commit comments

Comments
 (0)
Please sign in to comment.