Skip to content

Commit 8090f2a

Browse files
authored
Add authToken for the npm registry to user-level .npmrc if it is not there (#110)
1 parent b98cec9 commit 8090f2a

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,32 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
4242
"No changesets found, attempting to publish any unpublished packages to npm"
4343
);
4444

45-
let npmrcPath = `${process.env.HOME}/.npmrc`;
46-
if (fs.existsSync(npmrcPath)) {
47-
console.log("Found existing .npmrc file");
45+
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
46+
if (fs.existsSync(userNpmrcPath)) {
47+
console.log("Found existing user .npmrc file");
48+
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
49+
const authLine = userNpmrcContent.split("\n").find((line) => {
50+
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
51+
return /^\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
52+
});
53+
if (authLine) {
54+
console.log(
55+
"Found existing auth token for the npm registry in the user .npmrc file"
56+
);
57+
} else {
58+
console.log(
59+
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
60+
);
61+
fs.appendFileSync(
62+
userNpmrcPath,
63+
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
64+
);
65+
}
4866
} else {
49-
console.log("No .npmrc file found, creating one");
67+
console.log("No user .npmrc file found, creating one");
5068
fs.writeFileSync(
51-
npmrcPath,
52-
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`
69+
userNpmrcPath,
70+
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
5371
);
5472
}
5573

0 commit comments

Comments
 (0)