diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts index d884e23c..c98a2432 100644 --- a/__tests__/authutil.test.ts +++ b/__tests__/authutil.test.ts @@ -144,7 +144,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/` + `//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -153,7 +153,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `@myscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/` + `@myscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -162,7 +162,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `@myscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/` + `@myscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -172,7 +172,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/` + `//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -182,7 +182,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/` + `registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -192,7 +192,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/` + `registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -205,7 +205,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `@otherscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/` + `@otherscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}` ); }); @@ -215,7 +215,7 @@ describe('authutil tests', () => { await auth.configAuthentication('https://registry.npmjs.org/'); const contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); expect(contents).toBe( - `@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/` + `@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}` ); }); }); diff --git a/src/authutil.ts b/src/authutil.ts index 37d8cfe1..8f0e2b0e 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -44,6 +44,9 @@ function writeRegistryToFile(registryUrl: string, fileLocation: string) { registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}'; const registryString = `${scope}registry=${registryUrl}`; newContents += `${authString}${os.EOL}${registryString}`; + if (newContents && !newContents.endsWith(os.EOL)) { + newContents += os.EOL; + } fs.writeFileSync(fileLocation, newContents); core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation); // Only export NODE_AUTH_TOKEN if explicitly provided by user