From be85abc3530712cff746e766e271c147117d9691 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 10 Apr 2021 06:53:58 +0200 Subject: [PATCH] feat(npm): retain npmrc lines without variables (#9484) Instead of ignoring the entire .npmrc file if it contains environment variables, instead just strip out the necessary lines. BREAKING: .npmrc files with environment variables will no longer be completely ignore - instead only the lines with variables will be stripped. --- lib/manager/npm/extract/index.spec.ts | 6 +++--- lib/manager/npm/extract/index.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/manager/npm/extract/index.spec.ts b/lib/manager/npm/extract/index.spec.ts index e4d6bac61dc2b0..beda16f2a1b85c 100644 --- a/lib/manager/npm/extract/index.spec.ts +++ b/lib/manager/npm/extract/index.spec.ts @@ -127,11 +127,11 @@ describe(getName(__filename), () => { ); expect(res.npmrc).toBeUndefined(); }); - it('finds and discards .npmrc', async () => { + it('finds and filters .npmrc with variables', async () => { fs.readLocalFile = jest.fn((fileName) => { if (fileName === '.npmrc') { // eslint-disable-next-line - return '//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}\n'; + return 'registry=https://registry.npmjs.org\n//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}\n'; } return null; }); @@ -140,7 +140,7 @@ describe(getName(__filename), () => { 'package.json', {} ); - expect(res.npmrc).toEqual(''); + expect(res.npmrc).toEqual('registry=https://registry.npmjs.org\n'); }); it('finds lerna', async () => { fs.readLocalFile = jest.fn((fileName) => { diff --git a/lib/manager/npm/extract/index.ts b/lib/manager/npm/extract/index.ts index cbc04c1596e629..7d745f386604d9 100644 --- a/lib/manager/npm/extract/index.ts +++ b/lib/manager/npm/extract/index.ts @@ -106,8 +106,14 @@ export async function extractPackageFile( npmrc = npmrc.replace(/(^|\n)package-lock.*?(\n|$)/g, '\n'); } if (npmrc.includes('=${') && !getAdminConfig().exposeAllEnv) { - logger.debug('Overriding .npmrc file with variables'); - npmrc = ''; + logger.debug( + { npmrcFileName }, + 'Stripping .npmrc file of lines with variables' + ); + npmrc = npmrc + .split('\n') + .filter((line) => !line.includes('=${')) + .join('\n'); } } }