Skip to content

Commit ebc8ba6

Browse files
committed
feat(publish): Validate npm registry and package access prerequisites
Fixes #55 Fixes #1045 Fixes #1347
1 parent 0097360 commit ebc8ba6

13 files changed

Lines changed: 140 additions & 1 deletion

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ jest.unmock("@lerna/collect-updates");
55

66
// local modules _must_ be explicitly mocked
77
jest.mock("../lib/get-packages-without-license");
8+
jest.mock("../lib/verify-npm-package-access");
9+
jest.mock("../lib/verify-npm-registry");
810

911
const fs = require("fs-extra");
1012
const path = require("path");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// local modules _must_ be explicitly mocked
44
jest.mock("../lib/get-packages-without-license");
5+
jest.mock("../lib/verify-npm-package-access");
6+
jest.mock("../lib/verify-npm-registry");
57
// FIXME: better mock for version command
68
jest.mock("../../version/lib/git-push");
79
jest.mock("../../version/lib/is-anything-committed");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

33
// local modules _must_ be explicitly mocked
4+
jest.mock("../lib/verify-npm-package-access");
5+
jest.mock("../lib/verify-npm-registry");
46
jest.mock("../lib/create-temp-licenses", () => jest.fn(() => Promise.resolve()));
57
jest.mock("../lib/remove-temp-licenses", () => jest.fn(() => Promise.resolve()));
68
// FIXME: better mock for version command

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// local modules _must_ be explicitly mocked
44
jest.mock("../lib/get-packages-without-license");
5+
jest.mock("../lib/verify-npm-package-access");
6+
jest.mock("../lib/verify-npm-registry");
57
// FIXME: better mock for version command
68
jest.mock("../../version/lib/git-push");
79
jest.mock("../../version/lib/is-anything-committed");

‎commands/publish/__tests__/publish-relative-file-specifiers.test.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ jest.unmock("@lerna/collect-updates");
55

66
// local modules _must_ be explicitly mocked
77
jest.mock("../lib/get-packages-without-license");
8+
jest.mock("../lib/verify-npm-package-access");
9+
jest.mock("../lib/verify-npm-registry");
810
// FIXME: better mock for version command
911
jest.mock("../../version/lib/git-push");
1012
jest.mock("../../version/lib/is-anything-committed");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// local modules _must_ be explicitly mocked
44
jest.mock("../lib/get-packages-without-license");
5+
jest.mock("../lib/verify-npm-package-access");
6+
jest.mock("../lib/verify-npm-registry");
57
// FIXME: better mock for version command
68
jest.mock("../../version/lib/git-push");
79
jest.mock("../../version/lib/is-anything-committed");

‎commands/publish/index.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const getTaggedPackages = require("./lib/get-tagged-packages");
2525
const getPackagesWithoutLicense = require("./lib/get-packages-without-license");
2626
const gitCheckout = require("./lib/git-checkout");
2727
const removeTempLicenses = require("./lib/remove-temp-licenses");
28+
const verifyNpmRegistry = require("./lib/verify-npm-registry");
29+
const verifyNpmPackageAccess = require("./lib/verify-npm-package-access");
2830

2931
module.exports = factory;
3032

@@ -89,7 +91,9 @@ class PublishCommand extends Command {
8991
)
9092
: [this.packagesToPublish];
9193

92-
return Promise.resolve().then(() => this.prepareLicenseActions());
94+
return Promise.resolve()
95+
.then(() => this.prepareRegistryActions())
96+
.then(() => this.prepareLicenseActions());
9397
});
9498
}
9599

@@ -232,6 +236,12 @@ class PublishCommand extends Command {
232236
});
233237
}
234238

239+
prepareRegistryActions() {
240+
return Promise.resolve()
241+
.then(() => verifyNpmRegistry(this.project.rootPath, this.npmConfig))
242+
.then(() => verifyNpmPackageAccess(this.packagesToPublish, this.project.rootPath, this.npmConfig));
243+
}
244+
235245
updateCanaryVersions() {
236246
const publishableUpdates = this.updates.filter(node => !node.pkg.private);
237247

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
// to mock user modules, you _must_ call `jest.mock('./path/to/module')`
4+
module.exports = jest.fn(() => Promise.resolve());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
// to mock user modules, you _must_ call `jest.mock('./path/to/module')`
4+
module.exports = jest.fn(() => Promise.resolve());
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use strict";
2+
3+
const log = require("npmlog");
4+
const childProcess = require("@lerna/child-process");
5+
const getExecOpts = require("@lerna/get-npm-exec-opts");
6+
const ValidationError = require("@lerna/validation-error");
7+
8+
module.exports = verifyNpmPackageAccess;
9+
10+
function verifyNpmPackageAccess(packages, location, { registry }) {
11+
log.silly("verifyNpmPackageAccess");
12+
13+
const args = [
14+
"access",
15+
"ls-packages",
16+
// immediate feedback from request errors, not excruciatingly slow retries
17+
// @see https://docs.npmjs.com/misc/config#fetch-retries
18+
"--fetch-retries=0",
19+
// including http requests makes raw logging easier to debug for end users
20+
"--loglevel=http",
21+
];
22+
const opts = getExecOpts({ location }, registry);
23+
24+
// we do not need special log handling
25+
delete opts.pkg;
26+
27+
return childProcess.exec("npm", args, opts).then(
28+
result => {
29+
const permission = JSON.parse(result.stdout);
30+
31+
for (const pkg of packages) {
32+
if (permission[pkg.name] !== "read-write") {
33+
throw new ValidationError(
34+
"EACCESS",
35+
"You do not have write permission required to publish %j",
36+
pkg.name
37+
);
38+
}
39+
}
40+
},
41+
// only catch npm error, not validation error above
42+
({ stderr }) => {
43+
// pass if registry does not support ls-packages endpoint
44+
if (/E500/.test(stderr) && /ECONNREFUSED/.test(stderr)) {
45+
// most likely a private registry (npm Enterprise, verdaccio, etc)
46+
log.warn(
47+
"EREGISTRY",
48+
"Registry %j does not support `npm access ls-packages`, skipping permission checks...",
49+
registry
50+
);
51+
52+
// don't log redundant errors
53+
return;
54+
}
55+
56+
if (/ENEEDAUTH/.test(stderr)) {
57+
throw new ValidationError(
58+
"ENEEDAUTH",
59+
"You must be logged in to publish packages. Use `npm login` and try again."
60+
);
61+
}
62+
63+
// Log the error cleanly to stderr, it already has npmlog decorations
64+
log.pause();
65+
console.error(stderr); // eslint-disable-line no-console
66+
log.resume();
67+
68+
throw new ValidationError("EWHOAMI", "Authentication error. Use `npm whoami` to troubleshoot.");
69+
}
70+
);
71+
}

0 commit comments

Comments
 (0)