Skip to content

Commit

Permalink
fix: changesets#424 - Publish in custom artifactory
Browse files Browse the repository at this point in the history
  • Loading branch information
gethari committed Nov 19, 2024
1 parent 31ff97e commit 8669687
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs-extra";
import * as gitUtils from "./gitUtils";
import { runPublish, runVersion } from "./run";
import readChangesetState from "./readChangesetState";
import { extractAuthTokenLine } from "./utils";

const getOptionalInput = (name: string) => core.getInput(name) || undefined;

Expand Down Expand Up @@ -59,10 +60,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
if (fs.existsSync(userNpmrcPath)) {
core.info("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);
});
const authLine = extractAuthTokenLine(userNpmrcContent);
if (authLine) {
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
Expand Down
61 changes: 60 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getChangelogEntry, BumpLevels, sortTheThings } from "./utils";
import { getChangelogEntry, BumpLevels, sortTheThings, extractAuthTokenLine } from "./utils";

let changelog = `# @keystone-alpha/email
Expand Down Expand Up @@ -99,3 +99,62 @@ test("it sorts the things right", () => {
];
expect(things.sort(sortTheThings)).toMatchSnapshot();
});

/**
* Test the extractAuthTokenLine function for various registries.
*/
describe("extractAuthTokenLine", () => {
it("should correctly find the auth token line for multiple registries", () => {
const testCases = [
{
name: "Custom private registry",
npmrc: `
registry=https://custom.private-registry.com/api/npm/npm/
//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234
always-auth=true
`,
expected: "//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234",
},
{
name: "NPM default registry",
npmrc: `
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=efgh5678
`,
expected: "//registry.npmjs.org/:_authToken=efgh5678",
},
{
name: "AWS CodeArtifact registry",
npmrc: `
registry=https://mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/
//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012
`,
expected:
"//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012",
},
{
name: "Azure DevOps registry",
npmrc: `
registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/
//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456
`,
expected:
"//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456",
},
];

testCases.forEach(({ name, npmrc, expected }) => {
const result = extractAuthTokenLine(npmrc);
expect(result).toBe(expected);
});
});

it("should return undefined if no auth token line is present", () => {
const npmrcContent = `
registry=https://custom.private-registry.com/api/npm/npm/
always-auth=true
`;
const result = extractAuthTokenLine(npmrcContent);
expect(result).toBeUndefined();
});
});
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,20 @@ export function sortTheThings(
}
return -1;
}

/**
* Extracts the line containing the auth token from .npmrc content.
*
* @param {string} npmrcContent - The content of the .npmrc file as a string.
* @returns {string | undefined} - The line containing the auth token or undefined if not found.
*/
export function extractAuthTokenLine(npmrcContent:string) {
/**
* @see https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
* Also dynamically adapt to any registry by looking for :_authToken= pattern
*/
const line = npmrcContent.split("\n").find((line) => {
return /^\s*\/\/.*\/:[_-]authToken=/i.test(line);
});
return line ? line.trim() : undefined;
};

0 comments on commit 8669687

Please sign in to comment.