mirror of
https://github.com/changesets/action
synced 2026-10-26 16:39:56 +08:00
Add authToken for the npm registry to user-level .npmrc if it is not there (#110)
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+24
-6
@@ -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`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user