From 00973609ed7dcf2db55c90266bfc3d3a3bece292 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Wed, 8 Aug 2018 12:09:00 -0700 Subject: [PATCH] refactor(publish): Pass npmConfig to dist-tag methods The pkg.version is sufficient when passed to distTag.add(), no separate parameter is required. --- .../publish/__tests__/publish-tagging.test.js | 10 +++--- commands/publish/index.js | 34 +++++++++---------- .../__tests__/npm-dist-tag.test.js | 18 +++++----- utils/npm-dist-tag/npm-dist-tag.js | 10 +++--- 4 files changed, 34 insertions(+), 38 deletions(-) diff --git a/commands/publish/__tests__/publish-tagging.test.js b/commands/publish/__tests__/publish-tagging.test.js index 8ed063e1fe..55c0ca8321 100644 --- a/commands/publish/__tests__/publish-tagging.test.js +++ b/commands/publish/__tests__/publish-tagging.test.js @@ -25,8 +25,8 @@ test("publish --npm-tag", async () => { await lernaPublish(cwd)("--npm-tag", "custom"); - expect(npmPublish.registry.size).toBe(1); expect(npmPublish.registry.get("package-3")).toBe("custom"); + expect(npmDistTag.check).not.toBeCalled(); }); test("publish --temp-tag", async () => { @@ -36,18 +36,16 @@ test("publish --temp-tag", async () => { await lernaPublish(cwd)("--temp-tag", "--registry", "test-registry"); - expect(npmPublish.registry.size).toBe(1); expect(npmPublish.registry.get("package-4")).toBe("lerna-temp"); expect(npmDistTag.remove).lastCalledWith( expect.objectContaining({ name: "package-4" }), "lerna-temp", - "test-registry" + expect.objectContaining({ registry: "test-registry" }) ); expect(npmDistTag.add).lastCalledWith( - expect.objectContaining({ name: "package-4" }), - "1.0.1", + expect.objectContaining({ name: "package-4", version: "1.0.1" }), "latest", - "test-registry" + expect.objectContaining({ registry: "test-registry" }) ); }); diff --git a/commands/publish/index.js b/commands/publish/index.js index 88fd6bb7b5..9d80cd090f 100644 --- a/commands/publish/index.js +++ b/commands/publish/index.js @@ -376,36 +376,34 @@ class PublishCommand extends Command { } npmUpdateAsLatest() { + const distTag = this.getDistTag() || "latest"; const tracker = this.logger.newItem("npmUpdateAsLatest"); tracker.addWork(this.packagesToPublish.length); - const mapPackage = pkg => - this.updateTag(pkg).then(() => { - tracker.info("latest", pkg.name); - tracker.completeWork(1); - }); + let chain = Promise.resolve(); - return pFinally(runParallelBatches(this.batchedPackages, this.concurrency, mapPackage), () => - tracker.finish() + chain = chain.then(() => + runParallelBatches(this.batchedPackages, this.concurrency, pkg => + this.updateTag(pkg, distTag).then(() => { + tracker.info("dist-tag", "%s@%s => %j", pkg.name, pkg.version, distTag); + tracker.completeWork(1); + }) + ) ); + + return pFinally(chain, () => tracker.finish()); } - removeTempTag(pkg) { + updateTag(pkg, distTag) { return Promise.resolve() - .then(() => npmDistTag.check(pkg, "lerna-temp", this.npmConfig.registry)) + .then(() => npmDistTag.check(pkg, "lerna-temp", this.npmConfig)) .then(exists => { if (exists) { - return npmDistTag.remove(pkg, "lerna-temp", this.npmConfig.registry); + return npmDistTag.remove(pkg, "lerna-temp", this.npmConfig); } - }); - } - - updateTag(pkg) { - const distTag = this.getDistTag() || "latest"; - const version = this.options.canary ? pkg.version : this.updatesVersions.get(pkg.name); - - return this.removeTempTag(pkg).then(() => npmDistTag.add(pkg, version, distTag, this.npmConfig.registry)); + }) + .then(() => npmDistTag.add(pkg, distTag, this.npmConfig)); } getDistTag() { diff --git a/utils/npm-dist-tag/__tests__/npm-dist-tag.test.js b/utils/npm-dist-tag/__tests__/npm-dist-tag.test.js index 9295b293a9..f02726611d 100644 --- a/utils/npm-dist-tag/__tests__/npm-dist-tag.test.js +++ b/utils/npm-dist-tag/__tests__/npm-dist-tag.test.js @@ -16,14 +16,14 @@ describe("dist-tag", () => { describe("npmDistTag.add()", () => { const pkg = { name: "foo-pkg", + version: "1.0.0", location: "/test/npm/dist-tag/add", }; - const version = "1.0.0"; const tag = "added-tag"; const registry = "https://custom-registry/add"; it("adds a dist-tag for a given package@version", async () => { - await npmDistTag.add(pkg, version, tag); + await npmDistTag.add(pkg, tag, {}); expect(ChildProcessUtilities.exec).lastCalledWith("npm", ["dist-tag", "add", "foo-pkg@1.0.0", tag], { cwd: pkg.location, @@ -33,7 +33,7 @@ describe("dist-tag", () => { }); it("supports custom registry", async () => { - await npmDistTag.add(pkg, version, tag, registry); + await npmDistTag.add(pkg, tag, { registry }); expect(ChildProcessUtilities.exec).lastCalledWith("npm", ["dist-tag", "add", "foo-pkg@1.0.0", tag], { cwd: pkg.location, @@ -54,7 +54,7 @@ describe("dist-tag", () => { const registry = "https://custom-registry/remove"; it("removes a dist-tag for a given package", async () => { - await npmDistTag.remove(pkg, tag); + await npmDistTag.remove(pkg, tag, {}); expect(ChildProcessUtilities.exec).lastCalledWith("npm", ["dist-tag", "rm", pkg.name, tag], { cwd: pkg.location, @@ -64,7 +64,7 @@ describe("dist-tag", () => { }); it("supports custom registry", async () => { - await npmDistTag.remove(pkg, tag, registry); + await npmDistTag.remove(pkg, tag, { registry }); expect(ChildProcessUtilities.exec).lastCalledWith("npm", ["dist-tag", "rm", pkg.name, tag], { cwd: pkg.location, @@ -86,9 +86,9 @@ describe("dist-tag", () => { it("tests if a dist-tag for a given package exists", () => { ChildProcessUtilities.execSync.mockReturnValue(["latest", "target-tag"].join(os.EOL)); - expect(npmDistTag.check(pkg, "target-tag")).toBe(true); - expect(npmDistTag.check(pkg, "latest")).toBe(true); - expect(npmDistTag.check(pkg, "missing")).toBe(false); + expect(npmDistTag.check(pkg, "target-tag", {})).toBe(true); + expect(npmDistTag.check(pkg, "latest", {})).toBe(true); + expect(npmDistTag.check(pkg, "missing", {})).toBe(false); expect(ChildProcessUtilities.execSync).lastCalledWith("npm", ["dist-tag", "ls", pkg.name], { cwd: pkg.location, @@ -100,7 +100,7 @@ describe("dist-tag", () => { it("supports custom registry", () => { ChildProcessUtilities.execSync.mockReturnValue("target-tag"); - expect(npmDistTag.check(pkg, "target-tag", registry)).toBe(true); + expect(npmDistTag.check(pkg, "target-tag", { registry })).toBe(true); expect(ChildProcessUtilities.execSync).lastCalledWith("npm", ["dist-tag", "ls", pkg.name], { cwd: pkg.location, diff --git a/utils/npm-dist-tag/npm-dist-tag.js b/utils/npm-dist-tag/npm-dist-tag.js index 387d74672e..22ac2d5775 100644 --- a/utils/npm-dist-tag/npm-dist-tag.js +++ b/utils/npm-dist-tag/npm-dist-tag.js @@ -9,17 +9,17 @@ exports.add = add; exports.check = check; exports.remove = remove; -function add(pkg, version, tag, registry) { - log.silly("npmDistTag.add", tag, version, pkg.name); +function add(pkg, tag, { registry }) { + log.silly("npmDistTag.add", tag, pkg.version, pkg.name); return ChildProcessUtilities.exec( "npm", - ["dist-tag", "add", `${pkg.name}@${version}`, tag], + ["dist-tag", "add", `${pkg.name}@${pkg.version}`, tag], getExecOpts(pkg, registry) ); } -function check(pkg, tag, registry) { +function check(pkg, tag, { registry }) { log.silly("npmDistTag.check", tag, pkg.name); const result = ChildProcessUtilities.execSync( @@ -31,7 +31,7 @@ function check(pkg, tag, registry) { return result.indexOf(tag) >= 0; } -function remove(pkg, tag, registry) { +function remove(pkg, tag, { registry }) { log.silly("npmDistTag.remove", tag, pkg.name); return ChildProcessUtilities.exec("npm", ["dist-tag", "rm", pkg.name, tag], getExecOpts(pkg, registry));