Skip to content

Commit 970e050

Browse files
committed
Support project as well as user .npmrc files.
See #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`.
1 parent 04d574e commit 970e050

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

.changeset/clear-cycles-smash.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@changesets/action": minor
3+
---
4+
5+
Support project as well as user `.npmrc` files.
6+
7+
See https://github.com/changesets/action/issues/89.
8+
9+
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`.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ jobs:
104104
run: my-slack-bot send-notification --message "A new version of ${GITHUB_REPOSITORY} was published!"
105105
```
106106

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

109109
```
110110
//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}
111111
```
112112

113-
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.
113+
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.
114114
For example, you can add a step before running the Changesets GitHub Action:
115115

116116
```yml

src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,34 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
6868
);
6969

7070
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
71-
if (await fileExists(userNpmrcPath)) {
72-
core.info("Found existing user .npmrc file");
73-
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
71+
let npmrcPath = await ["./.npmrc", userNpmrcPath].reduce<
72+
Promise<string | undefined>
73+
>(async (acc, path) => {
74+
if (await acc) return acc;
75+
return (await fileExists(path)) ? path : undefined;
76+
}, Promise.resolve(undefined));
77+
if (npmrcPath) {
78+
core.info(`Found existing .npmrc file at "${npmrcPath}"`);
79+
const userNpmrcContent = await fs.readFile(npmrcPath, "utf8");
7480
const authLine = userNpmrcContent.split("\n").find((line) => {
7581
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
7682
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
7783
});
7884
if (authLine) {
7985
core.info(
80-
"Found existing auth token for the npm registry in the user .npmrc file"
86+
"Found existing auth token for the npm registry in the .npmrc file"
8187
);
8288
} else {
8389
core.info(
84-
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
90+
"Didn't find existing auth token for the npm registry in the .npmrc file, creating one"
8591
);
8692
await fs.appendFile(
87-
userNpmrcPath,
93+
npmrcPath,
8894
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
8995
);
9096
}
9197
} else {
92-
core.info("No user .npmrc file found, creating one");
98+
core.info("No .npmrc file found, creating one");
9399
await fs.writeFile(
94100
userNpmrcPath,
95101
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`

0 commit comments

Comments
 (0)