Skip to content

Commit c0ad316

Browse files
committedDec 20, 2018
fix(run-lifecycle): Do not execute on packages that lack the target script, avoiding spurious logs
1 parent dde588a commit c0ad316

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed
 

Diff for: ‎commands/__mocks__/@lerna/run-lifecycle.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const mockRunLifecycle = jest.fn(pkg => Promise.resolve(pkg));
44
const mockCreateRunner = jest.fn(() => (pkg, stage) => {
5+
// no longer the actual API, but approximates inner logic of default export
56
if (pkg.scripts[stage]) {
67
return mockRunLifecycle(pkg, stage);
78
}

Diff for: ‎utils/run-lifecycle/__tests__/run-lifecycle.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,39 @@ const npmConf = require("@lerna/npm-conf");
88
const runLifecycle = require("../run-lifecycle");
99

1010
describe("runLifecycle()", () => {
11+
it("skips packages without scripts", async () => {
12+
const pkg = {
13+
name: "no-scripts",
14+
};
15+
16+
const result = await runLifecycle(pkg, "foo", new Map());
17+
18+
expect(result).toBe(pkg);
19+
expect(runScript).not.toHaveBeenCalled();
20+
});
21+
22+
it("skips packages without matching script", async () => {
23+
const pkg = {
24+
name: "missing-script",
25+
scripts: {
26+
test: "foo",
27+
},
28+
};
29+
30+
const result = await runLifecycle(pkg, "bar", new Map());
31+
32+
expect(result).toBe(pkg);
33+
expect(runScript).not.toHaveBeenCalled();
34+
});
35+
1136
it("calls npm-lifecycle with prepared arguments", async () => {
1237
const pkg = {
1338
name: "test-name",
1439
version: "1.0.0-test",
1540
location: "test-location",
41+
scripts: {
42+
preversion: "test",
43+
},
1644
};
1745
const stage = "preversion";
1846
const opts = npmConf({ "custom-cli-flag": true });
@@ -47,6 +75,9 @@ describe("runLifecycle()", () => {
4775
name: "dashed-name",
4876
version: "1.0.0-dashed",
4977
location: "dashed-location",
78+
scripts: {
79+
prepublish: "test",
80+
},
5081
};
5182
const dir = pkg.location;
5283
const stage = "prepublish";
@@ -82,6 +113,9 @@ describe("runLifecycle()", () => {
82113
name: "circular-name",
83114
version: "1.0.0-circular",
84115
location: "circular-location",
116+
scripts: {
117+
prepack: "test",
118+
},
85119
};
86120
const stage = "prepack";
87121
const opts = new Map();

Diff for: ‎utils/run-lifecycle/run-lifecycle.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ function runLifecycle(pkg, stage, _opts) {
4545
const dir = pkg.location;
4646
const config = {};
4747

48+
if (!pkg.scripts || !pkg.scripts[stage]) {
49+
opts.log.silly("run-lifecycle", "No script for %j in %j, continuing", stage, pkg.name);
50+
51+
return Promise.resolve(pkg);
52+
}
53+
4854
// https://github.com/zkat/figgy-pudding/blob/7d68bd3/index.js#L42-L64
4955
for (const [key, val] of opts) {
5056
// omit falsy values and circular objects
@@ -107,11 +113,5 @@ function runLifecycle(pkg, stage, _opts) {
107113
function createRunner(commandOptions) {
108114
const cfg = npmConf(commandOptions).snapshot;
109115

110-
return (pkg, stage) => {
111-
if (pkg.scripts && pkg.scripts[stage]) {
112-
return runLifecycle(pkg, stage, cfg);
113-
}
114-
115-
return Promise.resolve(pkg);
116-
};
116+
return (pkg, stage) => runLifecycle(pkg, stage, cfg);
117117
}

0 commit comments

Comments
 (0)
Please sign in to comment.