Skip to content

Commit

Permalink
Overwrite scoped registry
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMeah committed Oct 27, 2022
1 parent 16352bb commit 42e8ba5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
62 changes: 61 additions & 1 deletion __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ describe('authutil tests', () => {
for (const line of contents.split(os.EOL)) {
let parts = line.split('=');
if (parts.length == 2) {
rc[parts[0].trim()] = parts[1].trim();
let key = parts[0].trim();

if (rc[key]) {
throw new Error(`Duplicate key ${key} in ${rcFile}`);
}

rc[key] = parts[1].trim();
}
}
return rc;
Expand Down Expand Up @@ -132,4 +138,58 @@ describe('authutil tests', () => {
expect(rc['always-auth']).toBe('false');
expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar');
});

describe('Existing .npmrc', () => {
it('should overwrite registry', async () => {
fs.copyFileSync(path.join(__dirname, 'data/.npmrc'), rcFile);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['registry']).toBe('http://example.com');
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
let updatedRc = readRcFile(rcFile);
expect(updatedRc['registry']).toBe('https://registry.npmjs.org/');
expect(updatedRc['always-auth']).toBe('true');
});

it('should overwrite scoped registry', async () => {
process.env['INPUT_SCOPE'] = 'myScope';

fs.copyFileSync(path.join(__dirname, 'data/.npmrc-scoped'), rcFile);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('http://example.com');

await auth.configAuthentication('https://registry.npmjs.org/', 'true');

let updatedRc = readRcFile(rcFile);
expect(updatedRc['@myscope:registry']).toBe(
'https://registry.npmjs.org/'
);
expect(updatedRc['always-auth']).toBe('true');
});

it('should not delete registry when scoped', async () => {
process.env['INPUT_SCOPE'] = 'myScope';

fs.copyFileSync(
path.join(__dirname, 'data/.npmrc-scoped-with-registry'),
rcFile
);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('http://example.com');

await auth.configAuthentication('https://registry.npmjs.org/', 'true');

let updatedRc = readRcFile(rcFile);
expect(updatedRc['registry']).toBe('http://base.com');
expect(updatedRc['@myscope:registry']).toBe(
'https://registry.npmjs.org/'
);
expect(updatedRc['always-auth']).toBe('true');
});
});
});
1 change: 1 addition & 0 deletions __tests__/data/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=http://example.com
1 change: 1 addition & 0 deletions __tests__/data/.npmrc-scoped
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@myscope:registry=http://example.com
2 changes: 2 additions & 0 deletions __tests__/data/.npmrc-scoped-with-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
registry=http://base.com
@myscope:registry=http://example.com
8 changes: 4 additions & 4 deletions src/authutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ function writeRegistryToFile(
scope = scope.toLowerCase();
}

const registryKey: string = scope ? `${scope}:registry` : 'registry';

core.debug(`Setting auth in ${fileLocation}`);
let newContents: string = '';
if (fs.existsSync(fileLocation)) {
const curContents: string = fs.readFileSync(fileLocation, 'utf8');
curContents.split(os.EOL).forEach((line: string) => {
// Add current contents unless they are setting the registry
if (!line.toLowerCase().startsWith('registry')) {
if (!line.startsWith(registryKey)) {
newContents += line + os.EOL;
}
});
}
// Remove http: or https: from front of registry.
const authString: string =
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
const registryString: string = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
const registryString: string = `${registryKey}=${registryUrl}`;
const alwaysAuthString: string = `always-auth=${alwaysAuth}`;
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
fs.writeFileSync(fileLocation, newContents);
Expand Down

0 comments on commit 42e8ba5

Please sign in to comment.