Skip to content

Commit

Permalink
feat(dist-tag): Prompt for OTP when required
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 14, 2019
1 parent 86383f5 commit af870bb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
25 changes: 17 additions & 8 deletions commands/publish/__tests__/publish-tagging.test.js
Expand Up @@ -69,12 +69,15 @@ Map {
const conf = expect.objectContaining({
tag: "latest",
});
const cache = expect.objectContaining({
otp: undefined,
});

expect(npmDistTag.remove).toHaveBeenCalledWith("@integration/package-1@1.0.1", "lerna-temp", conf);
expect(npmDistTag.remove).toHaveBeenCalledWith("@integration/package-2@1.0.1", "lerna-temp", conf);
expect(npmDistTag.remove).toHaveBeenCalledWith("@integration/package-1@1.0.1", "lerna-temp", conf, cache);
expect(npmDistTag.remove).toHaveBeenCalledWith("@integration/package-2@1.0.1", "lerna-temp", conf, cache);

expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1", "CUSTOM", conf); // <--
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1", "latest", conf);
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1", "CUSTOM", conf, cache); // <--
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1", "latest", conf, cache);
});

test("publish --dist-tag beta --temp-tag", async () => {
Expand All @@ -92,9 +95,12 @@ Map {
const conf = expect.objectContaining({
tag: "beta",
});
const cache = expect.objectContaining({
otp: undefined,
});

expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1", "beta", conf); // <--
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1", "beta", conf);
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1", "beta", conf, cache); // <--
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1", "beta", conf, cache);
});

test("publish prerelease --pre-dist-tag beta", async () => {
Expand Down Expand Up @@ -154,7 +160,10 @@ Map {
const conf = expect.objectContaining({
tag: "next",
});
const cache = expect.objectContaining({
otp: undefined,
});

expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1-beta.0", "beta", conf);
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1-beta.0", "beta", conf);
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-1@1.0.1-beta.0", "beta", conf, cache);
expect(npmDistTag.add).toHaveBeenCalledWith("@integration/package-2@1.0.1-beta.0", "beta", conf, cache);
});
4 changes: 2 additions & 2 deletions commands/publish/index.js
Expand Up @@ -697,8 +697,8 @@ class PublishCommand extends Command {
const distTag = preDistTag || getDistTag(pkg.get("publishConfig"));

return Promise.resolve()
.then(() => pulseTillDone(npmDistTag.remove(spec, "lerna-temp", opts)))
.then(() => pulseTillDone(npmDistTag.add(spec, distTag, opts)))
.then(() => pulseTillDone(npmDistTag.remove(spec, "lerna-temp", opts, this.otpCache)))
.then(() => pulseTillDone(npmDistTag.add(spec, distTag, opts, this.otpCache)))
.then(() => {
tracker.success("dist-tag", "%s@%s => %j", pkg.name, pkg.version, distTag);
tracker.completeWork(1);
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/npm-dist-tag/__tests__/npm-dist-tag.test.js
@@ -1,6 +1,7 @@
"use strict";

jest.mock("npm-registry-fetch");
jest.mock("@lerna/otplease", () => (cb, opts) => Promise.resolve(cb(opts)));

// mocked modules
const fetch = require("npm-registry-fetch");
Expand Down
9 changes: 5 additions & 4 deletions utils/npm-dist-tag/npm-dist-tag.js
Expand Up @@ -4,6 +4,7 @@ const log = require("npmlog");
const npa = require("npm-package-arg");
const fetch = require("npm-registry-fetch");
const figgyPudding = require("figgy-pudding");
const otplease = require("@lerna/otplease");

exports.add = add;
exports.remove = remove;
Expand All @@ -23,7 +24,7 @@ const DistTagConfig = figgyPudding(
}
);

function add(spec, tag, _opts) {
function add(spec, tag, _opts, otpCache) {
const opts = DistTagConfig(_opts, {
spec: npa(spec),
tag,
Expand Down Expand Up @@ -53,7 +54,7 @@ function add(spec, tag, _opts) {
});

// success returns HTTP 204, thus no JSON to parse
return fetch(uri, payload).then(() => {
return otplease(wrappedPayload => fetch(uri, wrappedPayload), payload, otpCache).then(() => {
opts.log.verbose("dist-tag", `added "${cleanTag}" to ${name}@${version}`);

// eslint-disable-next-line no-param-reassign
Expand All @@ -64,7 +65,7 @@ function add(spec, tag, _opts) {
});
}

function remove(spec, tag, _opts) {
function remove(spec, tag, _opts, otpCache) {
const opts = DistTagConfig(_opts, {
spec: npa(spec),
});
Expand All @@ -85,7 +86,7 @@ function remove(spec, tag, _opts) {
});

// the delete properly returns a 204, so no json to parse
return fetch(uri, payload).then(() => {
return otplease(wrappedPayload => fetch(uri, wrappedPayload), payload, otpCache).then(() => {
opts.log.verbose("dist-tag", `removed "${tag}" from ${opts.spec.name}@${version}`);

// eslint-disable-next-line no-param-reassign
Expand Down
1 change: 1 addition & 0 deletions utils/npm-dist-tag/package.json
Expand Up @@ -32,6 +32,7 @@
"test": "echo \"Run tests from root\" && exit 1"
},
"dependencies": {
"@lerna/otplease": "file:../../core/otplease",
"figgy-pudding": "^3.5.1",
"npm-package-arg": "^6.1.0",
"npm-registry-fetch": "^3.9.0",
Expand Down

0 comments on commit af870bb

Please sign in to comment.