From 7927bc2dffffab2ef07404d4d7f24f23275d4681 Mon Sep 17 00:00:00 2001 From: Pavel Denisjuk Date: Fri, 9 Mar 2018 15:48:34 +0100 Subject: [PATCH] npm verification (#4) * fix(npm-verify): write NPM_TOKEN to a local .npmrc file * test(npm-verify): add test for a scenario where NPM_TOKEN is not set at all. * fix(npm-verify): unset npm_* environment variables when running whoami command --- src/plugins/npm/verify.js | 4 +++- tests/npmVerify.test.js | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/plugins/npm/verify.js b/src/plugins/npm/verify.js index 333b1ca..dc67970 100644 --- a/src/plugins/npm/verify.js +++ b/src/plugins/npm/verify.js @@ -15,7 +15,9 @@ export default () => { logger.log("Verifying access to NPM..."); try { await fs.appendFile("./.npmrc", `\n//registry.npmjs.org/:_authToken=${NPM_TOKEN}`); - await execa("npm", ["whoami"]); + // We need to unset the `npm_` env variables to make sure local `.npmrc` is being read. + // This is required when running scripts with yarn: https://github.com/yarnpkg/yarn/issues/4475 + execa.shellSync("unset $(env | awk -F= '$1 ~ /^npm_/ {print $1}') && npm whoami"); next(); } catch (err) { throw new Error("EINVALIDNPMTOKEN: " + err.message); diff --git a/tests/npmVerify.test.js b/tests/npmVerify.test.js index 580be7e..ed291b7 100644 --- a/tests/npmVerify.test.js +++ b/tests/npmVerify.test.js @@ -51,12 +51,14 @@ describe("npmVerify plugin test", function() { process.env["NPM_TOKEN"] = "npm-token"; proxyquire(modulePath, { - execa: async () => { - const npmrc = await fs.readFile("./.npmrc"); - if (npmrc.includes("//registry.npmjs.org/:_authToken=npm-token")) { - return true; + execa: { + shellSync: async () => { + const npmrc = await fs.readFile("./.npmrc"); + if (npmrc.includes("//registry.npmjs.org/:_authToken=npm-token")) { + return true; + } + throw new Error("Command failed: npm whoami"); } - throw new Error("Command failed: npm whoami"); } });