diff --git a/.changeset/clear-cycles-smash.md b/.changeset/clear-cycles-smash.md new file mode 100644 index 00000000..3fd65615 --- /dev/null +++ b/.changeset/clear-cycles-smash.md @@ -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`. diff --git a/README.md b/README.md index 5ea2e5e5..cc1a762a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.ts b/src/index.ts index 1b0f63ab..d4241d6c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 + >(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`