Skip to content

Commit

Permalink
refactor(publish): Pass npmConfig to dist-tag methods
Browse files Browse the repository at this point in the history
The pkg.version is sufficient when passed to distTag.add(),
no separate parameter is required.
  • Loading branch information
evocateur committed Aug 8, 2018
1 parent 37e3ec7 commit 0097360
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
10 changes: 4 additions & 6 deletions commands/publish/__tests__/publish-tagging.test.js
Expand Up @@ -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 () => {
Expand All @@ -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" })
);
});
34 changes: 16 additions & 18 deletions commands/publish/index.js
Expand Up @@ -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() {
Expand Down
18 changes: 9 additions & 9 deletions utils/npm-dist-tag/__tests__/npm-dist-tag.test.js
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions utils/npm-dist-tag/npm-dist-tag.js
Expand Up @@ -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(
Expand All @@ -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));
Expand Down

0 comments on commit 0097360

Please sign in to comment.