Skip to content

Commit 5c94ce4

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 5c94ce4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

.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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,34 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
6767
"No changesets found. Attempting to publish any unpublished packages to npm"
6868
);
6969

70-
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");
70+
let userNpmrcPath = `${process.env.HOME}/.npmrc`
71+
let npmrcPath = await ["./.npmrc", userNpmrcPath]
72+
.reduce<Promise<string | undefined>>(async (acc, path) => {
73+
if (await acc) return acc;
74+
return await fileExists(path) ? path : undefined;
75+
}, Promise.resolve(undefined));
76+
if (npmrcPath) {
77+
core.info(`Found existing .npmrc file at "${npmrcPath}"`);
78+
const userNpmrcContent = await fs.readFile(npmrcPath, "utf8");
7479
const authLine = userNpmrcContent.split("\n").find((line) => {
7580
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
7681
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
7782
});
7883
if (authLine) {
7984
core.info(
80-
"Found existing auth token for the npm registry in the user .npmrc file"
85+
"Found existing auth token for the npm registry in the .npmrc file"
8186
);
8287
} else {
8388
core.info(
84-
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
89+
"Didn't find existing auth token for the npm registry in the .npmrc file, creating one"
8590
);
8691
await fs.appendFile(
87-
userNpmrcPath,
92+
npmrcPath,
8893
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
8994
);
9095
}
9196
} else {
92-
core.info("No user .npmrc file found, creating one");
97+
core.info("No .npmrc file found, creating one");
9398
await fs.writeFile(
9499
userNpmrcPath,
95100
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`

0 commit comments

Comments
 (0)