Skip to content

Commit

Permalink
npm authentication (#5)
Browse files Browse the repository at this point in the history
* fix(npm-verify): write NPM_TOKEN to a local .npmrc file

* fix(npm plugin): unset npm_* env variables before using `npm`
  • Loading branch information
Pavel910 committed Mar 9, 2018
1 parent 7927bc2 commit c55addf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/plugins/npm/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export default () => {
fs.writeJsonSync(path.join(pkg.location, "package.json"), pkg.packageJSON, {
spaces: 2
});
const shell = await execa("npm", ["publish", `${pkg.location}`]);
// 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
const shell = await execa.shell(
`unset $(env | awk -F= '$1 ~ /^npm_/ {print $1}') && npm publish ${
pkg.location
}`
);
logger.log(shell.stdout);
pkg.npmPublish = {
...shell
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/npm/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default () => {
await fs.appendFile("./.npmrc", `\n//registry.npmjs.org/:_authToken=${NPM_TOKEN}`);
// 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");
await execa.shell("unset $(env | awk -F= '$1 ~ /^npm_/ {print $1}') && npm whoami");
next();
} catch (err) {
throw new Error("EINVALIDNPMTOKEN: " + err.message);
Expand Down
20 changes: 12 additions & 8 deletions tests/npmPublish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ describe("npmPublish plugin test", function() {

let packageWritten = false;
proxyquire("../src/plugins/npm/publish", {
execa: () => {
packageWritten = fileExists(dir + "/package.json");
return { stdout: pkg.packageJSON.name + "@" + pkg.packageJSON.version };
execa: {
shell: () => {
packageWritten = fileExists(dir + "/package.json");
return { stdout: pkg.packageJSON.name + "@" + pkg.packageJSON.version };
}
}
});

Expand Down Expand Up @@ -105,11 +107,13 @@ describe("npmPublish plugin test", function() {
};

proxyquire("../src/plugins/npm/publish", {
execa: stub()
.onFirstCall()
.throws(() => new Error("Invalid package"))
.onSecondCall()
.returns({ stdout: pkg2.packageJSON.name + "@" + pkg2.packageJSON.version })
execa: {
shell: stub()
.onFirstCall()
.throws(() => new Error("Invalid package"))
.onSecondCall()
.returns({ stdout: pkg2.packageJSON.name + "@" + pkg2.packageJSON.version })
}
});

const { default: npmPublishFactory } = await import("../src/plugins/npm/publish");
Expand Down
2 changes: 1 addition & 1 deletion tests/npmVerify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("npmVerify plugin test", function() {

proxyquire(modulePath, {
execa: {
shellSync: async () => {
shell: async () => {
const npmrc = await fs.readFile("./.npmrc");
if (npmrc.includes("//registry.npmjs.org/:_authToken=npm-token")) {
return true;
Expand Down

0 comments on commit c55addf

Please sign in to comment.