Skip to content
Snippets Groups Projects
Commit a1f9d7b5 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

fix: catch registry url error and default to npm (#769)

parent bae9ae05
No related branches found
No related tags found
No related merge requests found
......@@ -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}`;
}
......
......@@ -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 {
......
......@@ -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();
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment