Skip to content

Commit

Permalink
fix: Publish in custom artifactory changesets#424 with auth or `aut…
Browse files Browse the repository at this point in the history
…hToken`
  • Loading branch information
gethari committed Nov 21, 2024
1 parent 8669687 commit c2796d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-lies-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

fix: Publish in custom artifactory #424 with `auth` or `authToken`
45 changes: 40 additions & 5 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("extractAuthTokenLine", () => {
it("should correctly find the auth token line for multiple registries", () => {
const testCases = [
{
name: "Custom private registry",
name: "Custom private registry with _authToken",
npmrc: `
registry=https://custom.private-registry.com/api/npm/npm/
//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234
Expand All @@ -116,15 +116,32 @@ describe("extractAuthTokenLine", () => {
expected: "//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234",
},
{
name: "NPM default registry",
name: "Custom private registry with _auth",
npmrc: `
registry=https://custom.private-registry.com/api/npm/npm/
//custom.private-registry.com/api/npm/npm/:_auth=abcd1234
always-auth=true
`,
expected: "//custom.private-registry.com/api/npm/npm/:_auth=abcd1234",
},
{
name: "NPM default registry with _authToken",
npmrc: `
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=efgh5678
`,
expected: "//registry.npmjs.org/:_authToken=efgh5678",
},
{
name: "AWS CodeArtifact registry",
name: "NPM default registry with _auth",
npmrc: `
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_auth=efgh5678
`,
expected: "//registry.npmjs.org/:_auth=efgh5678",
},
{
name: "AWS CodeArtifact registry with _authToken",
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
Expand All @@ -133,14 +150,32 @@ describe("extractAuthTokenLine", () => {
"//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012",
},
{
name: "Azure DevOps registry",
name: "AWS CodeArtifact registry with _auth",
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/:_auth=ijkl9012
`,
expected:
"//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_auth=ijkl9012",
},
{
name: "Azure DevOps registry with _authToken",
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",
},
{
name: "Azure DevOps registry with _auth",
npmrc: `
registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/
//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_auth=mnop3456
`,
expected:
"//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_auth=mnop3456",
},
];

testCases.forEach(({ name, npmrc, expected }) => {
Expand All @@ -157,4 +192,4 @@ describe("extractAuthTokenLine", () => {
const result = extractAuthTokenLine(npmrcContent);
expect(result).toBeUndefined();
});
});
});
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ export function sortTheThings(
* @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) {
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
* This regex dynamically adapts to any registry by looking for the :_auth or :_authToken= pattern.
*/
const line = npmrcContent.split("\n").find((line) => {
return /^\s*\/\/.*\/:[_-]authToken=/i.test(line);
const line = npmrcContent.split("\n").find((line) => {
return /^\s*\/\/.*\/:(_auth|_authToken)=/i.test(line); // Match both _auth and _authToken
});
return line ? line.trim() : undefined;
return line ? line.trim() : undefined;
};

0 comments on commit c2796d4

Please sign in to comment.