Add authToken for the npm registry to user-level .npmrc if it is not there (#110)

This commit is contained in:
Mateusz Burzyński
2021-11-09 06:28:22 +01:00
committed by GitHub
parent b98cec9758
commit 8090f2a918
2 changed files with 25 additions and 7 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+24 -6
View File
@@ -42,14 +42,32 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
"No changesets found, attempting to publish any unpublished packages to npm"
);
let npmrcPath = `${process.env.HOME}/.npmrc`;
if (fs.existsSync(npmrcPath)) {
console.log("Found existing .npmrc file");
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
if (fs.existsSync(userNpmrcPath)) {
console.log("Found existing user .npmrc file");
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
const authLine = userNpmrcContent.split("\n").find((line) => {
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
return /^\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
});
if (authLine) {
console.log(
"Found existing auth token for the npm registry in the user .npmrc file"
);
} else {
console.log(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
);
fs.appendFileSync(
userNpmrcPath,
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
} else {
console.log("No .npmrc file found, creating one");
console.log("No user .npmrc file found, creating one");
fs.writeFileSync(
npmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}