|
| 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