Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/clear-cycles-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@changesets/action": minor
---

Support project as well as user `.npmrc` files.

See https://github.com/changesets/action/issues/89.

Checks for a project local `.npmrc` before the user `.npmrc`, which avoids potentially misleading log messages (not finding a user `.npmrc` is not a problem if there's a project one) and unecessarily generating a user `.npmrc`.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ jobs:
run: my-slack-bot send-notification --message "A new version of ${GITHUB_REPOSITORY} was published!"
```

By default the GitHub Action creates a `.npmrc` file with the following content:
By default the GitHub Action creates a `.npmrc` file with the following content to the user `$HOME` `.npmrc`:

```
//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}
```

However, if a `.npmrc` file is found, the GitHub Action does not recreate the file. This is useful if you need to configure the `.npmrc` file on your own.
However, if either a project or user `.npmrc` file is found, the GitHub Action does not recreate the file. This is useful if you need to configure the `.npmrc` file on your own.
For example, you can add a step before running the Changesets GitHub Action:

```yml
Expand Down
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,34 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
);

let userNpmrcPath = `${process.env.HOME}/.npmrc`;
if (await fileExists(userNpmrcPath)) {
core.info("Found existing user .npmrc file");
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
let npmrcPath = await ["./.npmrc", userNpmrcPath].reduce<
Promise<string | undefined>
>(async (acc, path) => {
if (await acc) return acc;
return (await fileExists(path)) ? path : undefined;
}, Promise.resolve(undefined));
if (npmrcPath) {
core.info(`Found existing .npmrc file at "${npmrcPath}"`);
const userNpmrcContent = await fs.readFile(npmrcPath, "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 /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
});
if (authLine) {
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
"Found existing auth token for the npm registry in the .npmrc file"
);
} else {
core.info(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
"Didn't find existing auth token for the npm registry in the .npmrc file, creating one"
);
await fs.appendFile(
userNpmrcPath,
npmrcPath,
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
} else {
core.info("No user .npmrc file found, creating one");
core.info("No .npmrc file found, creating one");
await fs.writeFile(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
Expand Down