diff --git a/lib/api/npm.js b/lib/api/npm.js index 12becdabd14a5e2fb6242dc21b2527e01a75905a..3a60f114d4284c89ae97a7507a8fc8724a5e3efd 100644 --- a/lib/api/npm.js +++ b/lib/api/npm.js @@ -27,7 +27,12 @@ async function setNpmrc(input) { async function getDependency(name, logger) { logger.debug(`getDependency(${name})`); const scope = name.split('/')[0]; - const regUrl = getRegistryUrl(scope, npmrc); + let regUrl; + try { + regUrl = getRegistryUrl(scope, npmrc); + } catch (err) { + regUrl = 'https://registry.npmjs.org'; + } const pkgUrl = url.resolve( regUrl, encodeURIComponent(name).replace(/^%40/, '@') @@ -35,9 +40,9 @@ async function getDependency(name, logger) { const authInfo = registryAuthToken(regUrl, { npmrc }); const headers = {}; - if (authInfo) { + if (authInfo && authInfo.type && authInfo.token) { headers.authorization = `${authInfo.type} ${authInfo.token}`; - } else if (process.env.NPM_TOKEN) { + } else if (process.env.NPM_TOKEN && process.env.NPM_TOKEN !== 'undefined') { headers.authorization = `Bearer ${process.env.NPM_TOKEN}`; } diff --git a/test/api/__snapshots__/npm.spec.js.snap b/test/api/__snapshots__/npm.spec.js.snap index 6d70d5a16d61414ec5004ef62a36c08515a5f756..134266500f14e03696a20dde060ed4a9ed368b59 100644 --- a/test/api/__snapshots__/npm.spec.js.snap +++ b/test/api/__snapshots__/npm.spec.js.snap @@ -21,9 +21,7 @@ exports[`api/npm should fetch package info from custom registry 2`] = ` Array [ "https://npm.mycustomregistry.com/foobar", Object { - "headers": Object { - "authorization": "Bearer undefined", - }, + "headers": Object {}, "json": true, }, ] @@ -114,6 +112,33 @@ Array [ ] `; +exports[`api/npm should use default registry if missing from npmrc 1`] = ` +Object { + "dist-tags": Object { + "latest": "0.0.1", + }, + "homepage": "https://google.com", + "name": undefined, + "renovate-config": undefined, + "repositoryUrl": "https://google.com", + "versions": Object { + "0.0.1": Object { + "time": "", + }, + }, +} +`; + +exports[`api/npm should use default registry if missing from npmrc 2`] = ` +Array [ + "https://registry.npmjs.org/foobar", + Object { + "headers": Object {}, + "json": true, + }, +] +`; + exports[`api/npm should use homepage 1`] = ` Object { "dist-tags": Object { diff --git a/test/api/npm.spec.js b/test/api/npm.spec.js index bed2cc8ca148b03dad25bc2c240b49b6525f9e4c..9e1aeccc3332fd86b79db40d96c151c1e8cad7f8 100644 --- a/test/api/npm.spec.js +++ b/test/api/npm.spec.js @@ -92,4 +92,12 @@ describe('api/npm', () => { const call = got.mock.calls[0]; expect(call).toMatchSnapshot(); }); + it('should use default registry if missing from npmrc', async () => { + got.mockImplementation(() => Promise.resolve(npmResponse)); + npm.setNpmrc('foo=bar'); + const res = await npm.getDependency('foobar', logger); + expect(res).toMatchSnapshot(); + const call = got.mock.calls[0]; + expect(call).toMatchSnapshot(); + }); });