-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): Validate npm registry and package access prerequisites
- Loading branch information
Showing
13 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
// to mock user modules, you _must_ call `jest.mock('./path/to/module')` | ||
module.exports = jest.fn(() => Promise.resolve()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
// to mock user modules, you _must_ call `jest.mock('./path/to/module')` | ||
module.exports = jest.fn(() => Promise.resolve()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use strict"; | ||
|
||
const log = require("npmlog"); | ||
const childProcess = require("@lerna/child-process"); | ||
const getExecOpts = require("@lerna/get-npm-exec-opts"); | ||
const ValidationError = require("@lerna/validation-error"); | ||
|
||
module.exports = verifyNpmPackageAccess; | ||
|
||
function verifyNpmPackageAccess(packages, location, { registry }) { | ||
log.silly("verifyNpmPackageAccess"); | ||
|
||
const args = [ | ||
"access", | ||
"ls-packages", | ||
// immediate feedback from request errors, not excruciatingly slow retries | ||
// @see https://docs.npmjs.com/misc/config#fetch-retries | ||
"--fetch-retries=0", | ||
// including http requests makes raw logging easier to debug for end users | ||
"--loglevel=http", | ||
]; | ||
const opts = getExecOpts({ location }, registry); | ||
|
||
// we do not need special log handling | ||
delete opts.pkg; | ||
|
||
return childProcess.exec("npm", args, opts).then( | ||
result => { | ||
const permission = JSON.parse(result.stdout); | ||
|
||
for (const pkg of packages) { | ||
if (permission[pkg.name] !== "read-write") { | ||
throw new ValidationError( | ||
"EACCESS", | ||
"You do not have write permission required to publish %j", | ||
pkg.name | ||
); | ||
} | ||
} | ||
}, | ||
// only catch npm error, not validation error above | ||
({ stderr }) => { | ||
// pass if registry does not support ls-packages endpoint | ||
if (/E500/.test(stderr) && /ECONNREFUSED/.test(stderr)) { | ||
// most likely a private registry (npm Enterprise, verdaccio, etc) | ||
log.warn( | ||
"EREGISTRY", | ||
"Registry %j does not support `npm access ls-packages`, skipping permission checks...", | ||
registry | ||
); | ||
|
||
// don't log redundant errors | ||
return; | ||
} | ||
|
||
if (/ENEEDAUTH/.test(stderr)) { | ||
throw new ValidationError( | ||
"ENEEDAUTH", | ||
"You must be logged in to publish packages. Use `npm login` and try again." | ||
); | ||
} | ||
|
||
// Log the error cleanly to stderr, it already has npmlog decorations | ||
log.pause(); | ||
console.error(stderr); // eslint-disable-line no-console | ||
log.resume(); | ||
|
||
throw new ValidationError("EWHOAMI", "Authentication error. Use `npm whoami` to troubleshoot."); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use strict"; | ||
|
||
const log = require("npmlog"); | ||
const childProcess = require("@lerna/child-process"); | ||
const getExecOpts = require("@lerna/get-npm-exec-opts"); | ||
const ValidationError = require("@lerna/validation-error"); | ||
|
||
module.exports = verifyNpmRegistry; | ||
|
||
function verifyNpmRegistry(location, { registry }) { | ||
log.silly("verifyNpmRegistry"); | ||
|
||
const args = [ | ||
"ping", | ||
// immediate feedback from request errors, not excruciatingly slow retries | ||
// @see https://docs.npmjs.com/misc/config#fetch-retries | ||
"--fetch-retries=0", | ||
// including http requests makes raw logging easier to debug for end users | ||
"--loglevel=http", | ||
]; | ||
const opts = getExecOpts({ location }, registry); | ||
|
||
// we do not need special log handling | ||
delete opts.pkg; | ||
|
||
return childProcess.exec("npm", args, opts).catch(({ stderr }) => { | ||
// Log the error cleanly to stderr, it already has npmlog decorations | ||
log.pause(); | ||
console.error(stderr); // eslint-disable-line no-console | ||
log.resume(); | ||
|
||
throw new ValidationError("EREGISTRY", "Connection to npm registry failed"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.