Skip to content

Commit 8d80b2c

Browse files
committed
feat(publish): Run npm pack before npm publish
1 parent 088ea54 commit 8d80b2c

5 files changed

Lines changed: 76 additions & 45 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("licenses", () => {
3838
it("removes all temporary licenses on error", async () => {
3939
const cwd = await initFixture("licenses");
4040

41-
npmPublish.mockImplementationOnce(() => Promise.reject(new Error("boom")));
41+
npmPublish.npmPack.mockImplementationOnce(() => Promise.reject(new Error("boom")));
4242

4343
try {
4444
await lernaPublish(cwd)();
@@ -53,7 +53,7 @@ describe("licenses", () => {
5353
it("does not override original error when removal rejects", async () => {
5454
const cwd = await initFixture("licenses");
5555

56-
npmPublish.mockImplementationOnce(() => Promise.reject(new Error("boom")));
56+
npmPublish.npmPack.mockImplementationOnce(() => Promise.reject(new Error("boom")));
5757
removeTempLicenses.mockImplementationOnce(() => Promise.reject(new Error("shaka-lakka")));
5858

5959
try {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ describe("lifecycle scripts", () => {
2424

2525
await lernaPublish(cwd)();
2626

27-
expect(runLifecycle).toHaveBeenCalledTimes(12);
28-
2927
["prepare", "prepublishOnly", "postpublish"].forEach(script => {
3028
// "lifecycle" is the root manifest name
3129
expect(runLifecycle).toHaveBeenCalledWith(expect.objectContaining({ name: "lifecycle" }), script);
32-
expect(runLifecycle).toHaveBeenCalledWith(expect.objectContaining({ name: "package-1" }), script);
3330
});
3431

32+
// all leaf package lifecycles are called by npm pack
33+
expect(runLifecycle).not.toHaveBeenCalledWith(
34+
expect.objectContaining({ name: "package-1" }),
35+
expect.stringMatching(/(prepare|prepublishOnly|postpublish)/)
36+
);
37+
3538
// package-2 lacks version lifecycle scripts
3639
expect(runLifecycle).not.toHaveBeenCalledWith(
3740
expect.objectContaining({ name: "package-2" }),
@@ -49,9 +52,6 @@ describe("lifecycle scripts", () => {
4952
// publish-specific
5053
["lifecycle", "prepare"],
5154
["lifecycle", "prepublishOnly"],
52-
["package-1", "prepare"],
53-
["package-1", "prepublishOnly"],
54-
["package-1", "postpublish"],
5555
["lifecycle", "postpublish"],
5656
]);
5757
});

‎commands/publish/index.js‎

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class PublishCommand extends Command {
109109

110110
chain = chain.then(() => this.resolveLocalDependencyLinks());
111111
chain = chain.then(() => this.annotateGitHead());
112-
chain = chain.then(() => this.npmPublish());
112+
chain = chain.then(() => this.packUpdated());
113+
chain = chain.then(() => this.publishPacked());
113114
chain = chain.then(() => this.resetChanges());
114115

115116
if (this.options.tempTag) {
@@ -338,49 +339,70 @@ class PublishCommand extends Command {
338339
});
339340
}
340341

341-
npmPublish() {
342-
const tracker = this.logger.newItem("npmPublish");
343-
// if we skip temp tags we should tag with the proper value immediately
344-
const distTag = this.options.tempTag ? "lerna-temp" : this.getDistTag();
342+
packUpdated() {
343+
const tracker = this.logger.newItem("npm pack");
344+
345+
tracker.addWork(
346+
this.options.requireScripts
347+
? // track completion of prepublish.js on updates as well
348+
this.packagesToPublish.length + this.updates.length
349+
: this.packagesToPublish.length
350+
);
345351

346352
let chain = Promise.resolve();
347353

348354
chain = chain.then(() => createTempLicenses(this.project.licensePath, this.packagesToBeLicensed));
349-
350355
chain = chain.then(() => this.runPrepublishScripts(this.project.manifest));
351-
chain = chain.then(() =>
352-
pMap(this.updates, ({ pkg }) => {
353-
if (this.options.requireScripts) {
356+
357+
if (this.options.requireScripts) {
358+
chain = chain.then(() =>
359+
pMap(this.updates, ({ pkg }) => {
354360
this.execScript(pkg, "prepublish");
355-
}
361+
tracker.completeWork(1);
362+
})
363+
);
364+
}
356365

357-
return this.runPrepublishScripts(pkg);
358-
})
366+
chain = chain.then(() =>
367+
runParallelBatches(this.batchedPackages, this.concurrency, pkg =>
368+
npmPublish.npmPack(pkg).then(() => {
369+
tracker.completeWork(1);
370+
})
371+
)
359372
);
360373

374+
chain = chain.then(() => removeTempLicenses(this.packagesToBeLicensed));
375+
376+
// remove temporary license files if _any_ error occurs _anywhere_ in the promise chain
377+
chain = chain.catch(error => this.removeTempLicensesOnError(error));
378+
379+
return pFinally(chain, () => tracker.finish());
380+
}
381+
382+
publishPacked() {
383+
// if we skip temp tags we should tag with the proper value immediately
384+
const distTag = this.options.tempTag ? "lerna-temp" : this.getDistTag();
385+
const tracker = this.logger.newItem(`${this.npmConfig.npmClient} publish`);
386+
361387
tracker.addWork(this.packagesToPublish.length);
362388

363-
const mapPackage = pkg => {
364-
tracker.verbose("publishing", pkg.name);
389+
let chain = Promise.resolve();
365390

366-
return npmPublish(pkg, distTag, this.npmConfig).then(() => {
367-
tracker.info("published", pkg.name);
368-
tracker.completeWork(1);
391+
chain = chain.then(() =>
392+
runParallelBatches(this.batchedPackages, this.concurrency, pkg =>
393+
npmPublish(pkg, distTag, this.npmConfig).then(() => {
394+
tracker.info("published", pkg.name);
369395

370-
if (this.options.requireScripts) {
371-
this.execScript(pkg, "postpublish");
372-
}
396+
if (this.options.requireScripts) {
397+
this.execScript(pkg, "postpublish");
398+
}
373399

374-
return this.runPackageLifecycle(pkg, "postpublish");
375-
});
376-
};
400+
tracker.completeWork(1);
401+
})
402+
)
403+
);
377404

378-
chain = chain.then(() => runParallelBatches(this.batchedPackages, this.concurrency, mapPackage));
379405
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "postpublish"));
380-
chain = chain.then(() => removeTempLicenses(this.packagesToBeLicensed));
381-
382-
// remove temporary license files if _any_ error occurs _anywhere_ in the promise chain
383-
chain = chain.catch(error => this.removeTempLicensesOnError(error));
384406

385407
return pFinally(chain, () => tracker.finish());
386408
}

‎utils/npm-publish/__tests__/npm-publish.test.js‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("npm-publish", () => {
2121

2222
expect(ChildProcessUtilities.exec).lastCalledWith(
2323
"npm",
24-
["publish", "--ignore-scripts", "--tag", "published-tag"],
24+
["publish", "--ignore-scripts", "--tag", "published-tag", "test-1.10.100.tgz"],
2525
{
2626
cwd: pkg.location,
2727
env: {},
@@ -33,19 +33,23 @@ describe("npm-publish", () => {
3333
it("does not pass --tag when none present (npm default)", async () => {
3434
await npmPublish(pkg, undefined, { npmClient: "npm" });
3535

36-
expect(ChildProcessUtilities.exec).lastCalledWith("npm", ["publish", "--ignore-scripts"], {
37-
cwd: pkg.location,
38-
env: {},
39-
pkg,
40-
});
36+
expect(ChildProcessUtilities.exec).lastCalledWith(
37+
"npm",
38+
["publish", "--ignore-scripts", "test-1.10.100.tgz"],
39+
{
40+
cwd: pkg.location,
41+
env: {},
42+
pkg,
43+
}
44+
);
4145
});
4246

4347
it("trims trailing whitespace in tag parameter", async () => {
4448
await npmPublish(pkg, "trailing-tag ", { npmClient: "npm" });
4549

4650
expect(ChildProcessUtilities.exec).lastCalledWith(
4751
"npm",
48-
["publish", "--ignore-scripts", "--tag", "trailing-tag"],
52+
["publish", "--ignore-scripts", "--tag", "trailing-tag", "test-1.10.100.tgz"],
4953
{
5054
cwd: pkg.location,
5155
env: {},
@@ -61,7 +65,7 @@ describe("npm-publish", () => {
6165

6266
expect(ChildProcessUtilities.exec).lastCalledWith(
6367
"npm",
64-
["publish", "--ignore-scripts", "--tag", "custom-registry"],
68+
["publish", "--ignore-scripts", "--tag", "custom-registry", "test-1.10.100.tgz"],
6569
{
6670
cwd: pkg.location,
6771
env: {
@@ -86,6 +90,7 @@ describe("npm-publish", () => {
8690
"--new-version",
8791
pkg.version,
8892
"--non-interactive",
93+
"test-1.10.100.tgz",
8994
],
9095
{
9196
cwd: pkg.location,

‎utils/npm-publish/npm-publish.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = npmPublish;
99
module.exports.npmPack = npmPack;
1010

1111
function npmPublish(pkg, tag, { npmClient, registry }) {
12-
log.silly("npmPublish", tag, pkg.name);
12+
log.verbose("publish", pkg.name);
1313

1414
const distTag = tag && tag.trim();
1515
const opts = getExecOpts(pkg, registry);
@@ -25,6 +25,10 @@ function npmPublish(pkg, tag, { npmClient, registry }) {
2525
args.push("--new-version", pkg.version, "--non-interactive");
2626
}
2727

28+
// always add tarball file, created by npmPack()
29+
args.push(pkg.tarball);
30+
31+
log.silly("exec", npmClient, ...args);
2832
return ChildProcessUtilities.exec(npmClient, args, opts);
2933
}
3034

0 commit comments

Comments
 (0)