From 20476f6a53a984210eb2d17ba0ae4decc4dab5e1 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 21 Apr 2018 18:51:18 +0300 Subject: [PATCH] fix "Enable using a custom npm registry" issue (#123) fix "Enable using a custom npm registry" issue --- src/npm-utils.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/npm-utils.js b/src/npm-utils.js index 55a96d4..d0ed43c 100644 --- a/src/npm-utils.js +++ b/src/npm-utils.js @@ -137,7 +137,70 @@ function oldNpmCache(pkg, deferred) { * @return {Promise} The manifest object. */ function getManifest(pkg) { - return require('pacote').manifest(pkg).then((pkgJson) => ({ + // detect custom registry and user auth info + let opts = { + registry: npm.config.get('registry'), + }; + + // https://github.com/npm/npm/blob/latest/lib/config/pacote.js + npm.config.keys.forEach(function(k) { + const authMatchGlobal = k.match( + /^(_authToken|username|_password|password|email|always-auth|_auth)$/ + ); + const authMatchScoped = k[0] === '/' && k.match( + /(.*):(_authToken|username|_password|password|email|always-auth|_auth)$/ + ); + + // if it matches scoped it will also match global + if (authMatchGlobal || authMatchScoped) { + let nerfDart = null; + let key = null; + let val = null; + + if (!opts.auth) { + opts.auth = {}; + } + + if (authMatchScoped) { + nerfDart = authMatchScoped[1]; + key = authMatchScoped[2]; + val = npm.config.get(k); + if (!opts.auth[nerfDart]) { + opts.auth[nerfDart] = { + alwaysAuth: !!npm.config.get('always-auth'), + }; + } + } else { + key = authMatchGlobal[1]; + val = npm.config.get(k); + opts.auth.alwaysAuth = !!npm.config.get('always-auth'); + } + + const auth = authMatchScoped ? opts.auth[nerfDart] : opts.auth; + if (key === '_authToken') { + auth.token = val; + } else if (key.match(/password$/i)) { + auth.password = + // the config file stores password auth already-encoded. pacote expects + // the actual username/password pair. + Buffer.from(val, 'base64').toString('utf8'); + } else if (key === 'always-auth') { + auth.alwaysAuth = val === 'false' ? false : !!val; + } else { + auth[key] = val; + } + } + + if (k[0] === '@') { + if (!opts.scopeTargets) { + opts.scopeTargets = {}; + } + opts.scopeTargets[k.replace(/:registry$/, '')] = npm.config.get(k); + } + }); + + + return require('pacote').manifest(pkg, opts).then((pkgJson) => ({ integrity: pkgJson._integrity, manifest: { name: pkgJson.name,