Skip to content

Commit d1b197b

Browse files
authored
Merge pull request #637 from akv-platform/v-sdolin/npmrc-dup
Fix scoped registries are duplicated in npmrc
2 parents 3ae886e + 069a4f8 commit d1b197b

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

__tests__/authutil.test.ts

+81
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ describe('authutil tests', () => {
123123
expect(rc['registry']).toBe('https://registry.npmjs.org/');
124124
expect(rc['always-auth']).toBe('true');
125125
});
126+
126127
it('It is already set the NODE_AUTH_TOKEN export it ', async () => {
127128
process.env.NODE_AUTH_TOKEN = 'foobar';
128129
await auth.configAuthentication('npm.pkg.github.com', 'false');
@@ -132,4 +133,84 @@ describe('authutil tests', () => {
132133
expect(rc['always-auth']).toBe('false');
133134
expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar');
134135
});
136+
137+
it('configAuthentication should overwrite non-scoped with non-scoped', async () => {
138+
fs.writeFileSync(rcFile, 'registry=NNN');
139+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
140+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
141+
expect(contents).toBe(
142+
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
143+
);
144+
});
145+
146+
it('configAuthentication should overwrite only non-scoped', async () => {
147+
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
148+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
149+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
150+
expect(contents).toBe(
151+
`@myscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
152+
);
153+
});
154+
155+
it('configAuthentication should add non-scoped to scoped', async () => {
156+
fs.writeFileSync(rcFile, '@myscope:registry=NNN');
157+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
158+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
159+
expect(contents).toBe(
160+
`@myscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
161+
);
162+
});
163+
164+
it('configAuthentication should overwrite scoped with scoped', async () => {
165+
process.env['INPUT_SCOPE'] = 'myscope';
166+
fs.writeFileSync(rcFile, `@myscope:registry=NNN`);
167+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
168+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
169+
expect(contents).toBe(
170+
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
171+
);
172+
});
173+
174+
it('configAuthentication should overwrite only scoped', async () => {
175+
process.env['INPUT_SCOPE'] = 'myscope';
176+
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
177+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
178+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
179+
expect(contents).toBe(
180+
`registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
181+
);
182+
});
183+
184+
it('configAuthentication should add scoped to non-scoped', async () => {
185+
process.env['INPUT_SCOPE'] = 'myscope';
186+
fs.writeFileSync(rcFile, `registry=MMM`);
187+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
188+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
189+
expect(contents).toBe(
190+
`registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
191+
);
192+
});
193+
194+
it('configAuthentication should overwrite only one scoped', async () => {
195+
process.env['INPUT_SCOPE'] = 'myscope';
196+
fs.writeFileSync(
197+
rcFile,
198+
`@otherscope:registry=NNN${os.EOL}@myscope:registry=MMM`
199+
);
200+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
201+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
202+
expect(contents).toBe(
203+
`@otherscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
204+
);
205+
});
206+
207+
it('configAuthentication should add scoped to another scoped', async () => {
208+
process.env['INPUT_SCOPE'] = 'myscope';
209+
fs.writeFileSync(rcFile, `@otherscope:registry=MMM`);
210+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
211+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
212+
expect(contents).toBe(
213+
`@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
214+
);
215+
});
135216
});

dist/setup/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -72945,24 +72945,22 @@ function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
7294572945
scope = '@' + scope;
7294672946
}
7294772947
if (scope) {
72948-
scope = scope.toLowerCase();
72948+
scope = scope.toLowerCase() + ':';
7294972949
}
7295072950
core.debug(`Setting auth in ${fileLocation}`);
7295172951
let newContents = '';
7295272952
if (fs.existsSync(fileLocation)) {
7295372953
const curContents = fs.readFileSync(fileLocation, 'utf8');
7295472954
curContents.split(os.EOL).forEach((line) => {
7295572955
// Add current contents unless they are setting the registry
72956-
if (!line.toLowerCase().startsWith('registry')) {
72956+
if (!line.toLowerCase().startsWith(`${scope}registry`)) {
7295772957
newContents += line + os.EOL;
7295872958
}
7295972959
});
7296072960
}
7296172961
// Remove http: or https: from front of registry.
7296272962
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
72963-
const registryString = scope
72964-
? `${scope}:registry=${registryUrl}`
72965-
: `registry=${registryUrl}`;
72963+
const registryString = `${scope}registry=${registryUrl}`;
7296672964
const alwaysAuthString = `always-auth=${alwaysAuth}`;
7296772965
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
7296872966
fs.writeFileSync(fileLocation, newContents);

src/authutil.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function writeRegistryToFile(
2929
scope = '@' + scope;
3030
}
3131
if (scope) {
32-
scope = scope.toLowerCase();
32+
scope = scope.toLowerCase() + ':';
3333
}
3434

3535
core.debug(`Setting auth in ${fileLocation}`);
@@ -38,17 +38,15 @@ function writeRegistryToFile(
3838
const curContents: string = fs.readFileSync(fileLocation, 'utf8');
3939
curContents.split(os.EOL).forEach((line: string) => {
4040
// Add current contents unless they are setting the registry
41-
if (!line.toLowerCase().startsWith('registry')) {
41+
if (!line.toLowerCase().startsWith(`${scope}registry`)) {
4242
newContents += line + os.EOL;
4343
}
4444
});
4545
}
4646
// Remove http: or https: from front of registry.
4747
const authString: string =
4848
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
49-
const registryString: string = scope
50-
? `${scope}:registry=${registryUrl}`
51-
: `registry=${registryUrl}`;
49+
const registryString: string = `${scope}registry=${registryUrl}`;
5250
const alwaysAuthString: string = `always-auth=${alwaysAuth}`;
5351
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
5452
fs.writeFileSync(fileLocation, newContents);

0 commit comments

Comments
 (0)