Skip to content

Commit 627cfc2

Browse files
committed
fix(publish): Improve npm pack experience
- It works now - Including integration tests (very slow though :P) - Logging is good - Package instances now expose `.rootPath` read-only property - Removed pkg.tarball, now decorated by npmPack() from JSON result
1 parent 9c767ac commit 627cfc2

13 files changed

Lines changed: 212 additions & 79 deletions

File tree

‎commands/__mocks__/@lerna/npm-publish.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ const mockNpmPublish = jest.fn((pkg, tag) => {
1010
return Promise.resolve();
1111
});
1212

13-
const mockNpmPack = jest.fn(pkg => {
14-
packed.add(pkg.name);
13+
const mockNpmPack = jest.fn((rootManifest, packages) => {
14+
packages.forEach(pkg => {
15+
packed.add(pkg.name);
1516

16-
return Promise.resolve(pkg.tarball);
17+
// simulate decoration after npm pack
18+
pkg.tarball = `${pkg.name}-MOCKED.tgz`;
19+
});
20+
21+
return Promise.resolve();
1722
});
1823

1924
// a convenient format for assertions

‎commands/publish/__tests__/__fixtures__/lifecycle/packages/package-1/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"postversion": "echo postversion-package-1",
88
"prepublish": "echo prepublish-package-1",
99
"prepare": "echo prepare-package-1",
10-
"prepublishOnly": "echo prepublish-package-1",
10+
"prepublishOnly": "echo prepublishOnly-package-1",
1111
"postpublish": "echo postpublish-package-1"
1212
}
1313
}

‎commands/publish/__tests__/publish-command.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ describe("PublishCommand", () => {
2828

2929
await lernaPublish(testDir)();
3030

31+
expect(npmPublish.packed).toMatchInlineSnapshot(`
32+
Set {
33+
"package-1",
34+
"package-3",
35+
"package-4",
36+
"package-2",
37+
}
38+
`);
3139
expect(npmPublish.order()).toEqual([
3240
"package-1",
3341
"package-3",

‎commands/publish/__tests__/publish-lifecycle-scripts.test.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ describe("lifecycle scripts", () => {
2929
expect(runLifecycle).toHaveBeenCalledWith(expect.objectContaining({ name: "lifecycle" }), script);
3030
});
3131

32-
// all leaf package lifecycles are called by npm pack
32+
// all leaf package lifecycles _EXCEPT_ postpublish are called by npm pack
3333
expect(runLifecycle).not.toHaveBeenCalledWith(
3434
expect.objectContaining({ name: "package-1" }),
35-
expect.stringMatching(/(prepare|prepublishOnly|postpublish)/)
35+
expect.stringMatching(/(prepare|prepublishOnly)/)
36+
);
37+
expect(runLifecycle).toHaveBeenCalledWith(
38+
expect.objectContaining({ name: "package-1" }),
39+
expect.stringMatching("postpublish")
3640
);
3741

3842
// package-2 lacks version lifecycle scripts
@@ -52,6 +56,7 @@ describe("lifecycle scripts", () => {
5256
// publish-specific
5357
["lifecycle", "prepare"],
5458
["lifecycle", "prepublishOnly"],
59+
["package-1", "postpublish"],
5560
["lifecycle", "postpublish"],
5661
]);
5762
});

‎commands/publish/index.js‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const os = require("os");
4+
const fs = require("fs-extra");
45
const path = require("path");
56
const pFinally = require("p-finally");
67
const pMap = require("p-map");
@@ -373,9 +374,9 @@ class PublishCommand extends Command {
373374
}
374375

375376
chain = chain.then(() =>
376-
runParallelBatches(this.batchedPackages, this.concurrency, pkg =>
377-
npmPublish.npmPack(pkg).then(() => {
378-
tracker.completeWork(1);
377+
pReduce(this.batchedPackages, (_, batch) =>
378+
npmPublish.npmPack(this.project.manifest, batch).then(() => {
379+
tracker.completeWork(batch.length);
379380
})
380381
)
381382
);
@@ -399,15 +400,20 @@ class PublishCommand extends Command {
399400

400401
chain = chain.then(() =>
401402
runParallelBatches(this.batchedPackages, this.concurrency, pkg =>
402-
npmPublish(pkg, distTag, this.npmConfig).then(() => {
403-
tracker.info("published", pkg.name);
404-
405-
if (this.options.requireScripts) {
406-
this.execScript(pkg, "postpublish");
407-
}
408-
409-
tracker.completeWork(1);
410-
})
403+
npmPublish(pkg, distTag, this.npmConfig)
404+
// postpublish is _not_ run when publishing a tarball
405+
.then(() => this.runPackageLifecycle(pkg, "postpublish"))
406+
// don't leave the generated tarball hanging around
407+
.then(() => fs.remove(path.join(pkg.rootPath, pkg.tarball)))
408+
.then(() => {
409+
tracker.info("published", pkg.name);
410+
411+
if (this.options.requireScripts) {
412+
this.execScript(pkg, "postpublish");
413+
}
414+
415+
tracker.completeWork(1);
416+
})
411417
)
412418
);
413419

‎core/package/__tests__/core-package.test.js‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe("Package", () => {
3737
});
3838
});
3939

40+
describe("get .rootPath", () => {
41+
it("should return the rootPath", () => {
42+
const pkg = factory({ name: "get-rootPath" });
43+
expect(pkg.rootPath).toBe(path.normalize("/root"));
44+
});
45+
});
46+
4047
describe("get .version", () => {
4148
it("should return the version", () => {
4249
const pkg = factory({ version: "1.0.0" });
@@ -144,18 +151,6 @@ describe("Package", () => {
144151
});
145152
});
146153

147-
describe("get .tarball", () => {
148-
it("generates the destination filename of npm pack output", () => {
149-
const pkg = factory({ name: "pack-me", version: "1.2.3" });
150-
expect(pkg.tarball).toBe("pack-me-1.2.3.tgz");
151-
});
152-
153-
it("normalizes package scopes", () => {
154-
const pkg = factory({ name: "@scoped/pack-me", version: "4.5.6" });
155-
expect(pkg.tarball).toBe("scoped-pack-me-4.5.6.tgz");
156-
});
157-
});
158-
159154
describe(".toJSON()", () => {
160155
it("should return clone of internal package for serialization", () => {
161156
const json = {

‎core/package/index.js‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ function binSafeName({ name, scope }) {
88
return scope ? name.substring(scope.length + 1) : name;
99
}
1010

11-
function packedFileName(pkg) {
12-
// copied almost verbatim from https://git.io/fNobb
13-
const name =
14-
pkg.name[0] === "@"
15-
? // scoped packages get special treatment
16-
pkg.name.substr(1).replace(/\//g, "-")
17-
: pkg.name;
18-
19-
return `${name}-${pkg.version}.tgz`;
20-
}
21-
2211
// package.json files are not that complicated, so this is intentionally naïve
2312
function shallowCopy(json) {
2413
return Object.keys(json).reduce((obj, key) => {
@@ -57,6 +46,9 @@ class Package {
5746
resolved: {
5847
value: resolved,
5948
},
49+
rootPath: {
50+
value: rootPath,
51+
},
6052
// mutable
6153
version: {
6254
get() {
@@ -127,12 +119,6 @@ class Package {
127119
serialize: {
128120
value: () => writePkg(this.manifestLocation, pkg),
129121
},
130-
// compute result of `npm pack` filename
131-
tarball: {
132-
get() {
133-
return packedFileName(pkg);
134-
},
135-
},
136122
});
137123
}
138124

‎helpers/cli-runner/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function runner(cwd, env) {
1010
const opts = {
1111
cwd,
1212
env: Object.assign({ CI: "true" }, env),
13+
// when debugging integration test snapshots, uncomment next line
14+
// stdio: ["ignore", "inherit", "inherit"],
1315
};
1416

1517
return (...args) => execa("node", [LERNA_BIN].concat(args), opts);

‎integration/__snapshots__/lerna-publish.test.js.snap‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ prepare-root
4747
4848
prepublishOnly-root
4949
50+
> [email protected] prepublish __TEST_ROOTDIR__/packages/package-1
51+
> echo prepublish-package-1
52+
53+
prepublish-package-1
54+
5055
> [email protected] prepare __TEST_ROOTDIR__/packages/package-1
5156
> echo prepare-package-1
5257
5358
prepare-package-1
5459
55-
> [email protected] prepublishOnly __TEST_ROOTDIR__/packages/package-1
56-
> echo prepublish-package-1
57-
58-
prepublish-package-1
59-
6060
> [email protected] postpublish __TEST_ROOTDIR__/packages/package-1
6161
> echo postpublish-package-1
6262
@@ -380,8 +380,8 @@ postversion-package-1
380380
postversion-root
381381
prepare-root
382382
prepublishOnly-root
383-
prepare-package-1
384383
prepublish-package-1
384+
prepare-package-1
385385
postpublish-package-1
386386
postpublish-root
387387
Successfully published:

‎package-lock.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)