diff --git a/.changeset/tricky-impalas-protect.md b/.changeset/tricky-impalas-protect.md new file mode 100644 index 00000000..eca701d3 --- /dev/null +++ b/.changeset/tricky-impalas-protect.md @@ -0,0 +1,6 @@ +--- +"@changesets/action": minor +--- + +Added option to ignore creation or modification of `.npmrc` file. +This resolves issues where the correctly configured `.npmrc` file does not match the assumptions made by the action, such as when authenticating to Azure DevOps Artifact Feeds. diff --git a/README.md b/README.md index 49f2cca5..17f80efc 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ This action for [Changesets](https://github.com/atlassian/changesets) creates a - title - The pull request title. Default to `Version Packages` - setupGitUser - Sets up the git user for commits as `"github-actions[bot]"`. Default to `true` - cwd - Changes node's `process.cwd()` if the project is not located on the root. Default to `process.cwd()` +- skipNpmrc - Skips the creation or update of the `.npmrc` file. Only set to `true` if registry authentication is already handled. Defaults to `false`. ### Outputs diff --git a/action.yml b/action.yml index c9ca1b68..2a555e48 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,10 @@ inputs: description: "A boolean value to indicate whether to create Github releases after `publish` or not" required: false default: true + skipNpmrc: + description: "A boolean value to indicate whether to skip the `npmrc` file creation/modification or not" + required: false + default: false outputs: published: description: A boolean value to indicate whether a publishing is happened or not diff --git a/src/index.ts b/src/index.ts index 087eccc5..31280542 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,33 +52,8 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; "No changesets found, attempting to publish any unpublished packages to npm" ); - 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 /^\s*\/\/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 user .npmrc file found, creating one"); - fs.writeFileSync( - userNpmrcPath, - `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` - ); + if (!core.getBooleanInput("skipNpmrc")) { + await createOrUpdateNpmrc(); } const result = await runPublish({ @@ -113,3 +88,35 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; console.error(err); core.setFailed(err.message); }); + + +async function createOrUpdateNpmrc() { + 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 /^\s*\/\/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 user .npmrc file found, creating one"); + fs.writeFileSync( + userNpmrcPath, + `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` + ); + } +}