diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts index ec0617cc..9b02367c 100644 --- a/__tests__/authutil.test.ts +++ b/__tests__/authutil.test.ts @@ -173,7 +173,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}` ); }); @@ -182,7 +182,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}` ); }); @@ -191,7 +191,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}` ); }); @@ -201,7 +201,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}` ); }); @@ -211,7 +211,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}` ); }); @@ -221,7 +221,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}` ); }); @@ -234,7 +234,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}` ); }); @@ -244,7 +244,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