Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .changeset/tricky-impalas-protect.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 34 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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`
);
}
}