Skip to content

Commit 8669687

Browse files
committed
fix: #424 - Publish in custom artifactory
1 parent 31ff97e commit 8669687

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "fs-extra";
33
import * as gitUtils from "./gitUtils";
44
import { runPublish, runVersion } from "./run";
55
import readChangesetState from "./readChangesetState";
6+
import { extractAuthTokenLine } from "./utils";
67

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

@@ -59,10 +60,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
5960
if (fs.existsSync(userNpmrcPath)) {
6061
core.info("Found existing user .npmrc file");
6162
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
62-
const authLine = userNpmrcContent.split("\n").find((line) => {
63-
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
64-
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
65-
});
63+
const authLine = extractAuthTokenLine(userNpmrcContent);
6664
if (authLine) {
6765
core.info(
6866
"Found existing auth token for the npm registry in the user .npmrc file"

src/utils.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getChangelogEntry, BumpLevels, sortTheThings } from "./utils";
1+
import { getChangelogEntry, BumpLevels, sortTheThings, extractAuthTokenLine } from "./utils";
22

33
let changelog = `# @keystone-alpha/email
44
@@ -99,3 +99,62 @@ test("it sorts the things right", () => {
9999
];
100100
expect(things.sort(sortTheThings)).toMatchSnapshot();
101101
});
102+
103+
/**
104+
* Test the extractAuthTokenLine function for various registries.
105+
*/
106+
describe("extractAuthTokenLine", () => {
107+
it("should correctly find the auth token line for multiple registries", () => {
108+
const testCases = [
109+
{
110+
name: "Custom private registry",
111+
npmrc: `
112+
registry=https://custom.private-registry.com/api/npm/npm/
113+
//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234
114+
always-auth=true
115+
`,
116+
expected: "//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234",
117+
},
118+
{
119+
name: "NPM default registry",
120+
npmrc: `
121+
registry=https://registry.npmjs.org/
122+
//registry.npmjs.org/:_authToken=efgh5678
123+
`,
124+
expected: "//registry.npmjs.org/:_authToken=efgh5678",
125+
},
126+
{
127+
name: "AWS CodeArtifact registry",
128+
npmrc: `
129+
registry=https://mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/
130+
//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012
131+
`,
132+
expected:
133+
"//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012",
134+
},
135+
{
136+
name: "Azure DevOps registry",
137+
npmrc: `
138+
registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/
139+
//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456
140+
`,
141+
expected:
142+
"//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456",
143+
},
144+
];
145+
146+
testCases.forEach(({ name, npmrc, expected }) => {
147+
const result = extractAuthTokenLine(npmrc);
148+
expect(result).toBe(expected);
149+
});
150+
});
151+
152+
it("should return undefined if no auth token line is present", () => {
153+
const npmrcContent = `
154+
registry=https://custom.private-registry.com/api/npm/npm/
155+
always-auth=true
156+
`;
157+
const result = extractAuthTokenLine(npmrcContent);
158+
expect(result).toBeUndefined();
159+
});
160+
});

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ export function sortTheThings(
9898
}
9999
return -1;
100100
}
101+
102+
/**
103+
* Extracts the line containing the auth token from .npmrc content.
104+
*
105+
* @param {string} npmrcContent - The content of the .npmrc file as a string.
106+
* @returns {string | undefined} - The line containing the auth token or undefined if not found.
107+
*/
108+
export function extractAuthTokenLine(npmrcContent:string) {
109+
/**
110+
* @see https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
111+
* Also dynamically adapt to any registry by looking for :_authToken= pattern
112+
*/
113+
const line = npmrcContent.split("\n").find((line) => {
114+
return /^\s*\/\/.*\/:[_-]authToken=/i.test(line);
115+
});
116+
return line ? line.trim() : undefined;
117+
};

0 commit comments

Comments
 (0)