Skip to content

Commit a1a70a5

Browse files
committed
Add support for multiple service endpoints in npm task
1 parent 20fad4a commit a1a70a5

File tree

8 files changed

+105
-9
lines changed

8 files changed

+105
-9
lines changed

Tasks/Npm/Tests/L0.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ describe('Npm Task', function () {
6060
});
6161

6262
// custom
63+
it('custom command succeeds with single service endpoint', (done: MochaDone) => {
64+
this.timeout(1000);
65+
let tp = path.join(__dirname, 'custom-singleEndpoint.js');
66+
let tr = new ttm.MockTestRunner(tp);
67+
68+
tr.run();
69+
70+
assert(tr.stdOutContained('npm custom successful'), 'npm custom command should have run');
71+
assert(tr.stdOutContained('http://example.com/1/'), 'debug output should have contained endpoint');
72+
assert(tr.succeeded, 'task should have succeeded');
73+
74+
done();
75+
});
76+
6377
it('custom command should return npm version', (done: MochaDone) => {
6478
this.timeout(1000);
6579
let tp = path.join(__dirname, 'custom-version.js');
@@ -132,6 +146,21 @@ describe('Npm Task', function () {
132146
done();
133147
});
134148

149+
it('install using multiple endpoints', (done: MochaDone) => {
150+
this.timeout(1000);
151+
let tp = path.join(__dirname, 'install-multipleEndpoints.js');
152+
let tr = new ttm.MockTestRunner(tp);
153+
154+
tr.run();
155+
156+
assert(tr.stdOutContained('npm install successful'), 'npm should have installed the package');
157+
assert(tr.stdOutContained('http://example.com/1/'), 'debug output should have contained endpoint');
158+
assert(tr.stdOutContained('http://example.com/2/'), 'debug output should have contained endpoint');
159+
assert(tr.succeeded, 'task should have succeeded');
160+
161+
done();
162+
});
163+
135164
// publish
136165
it ('publish using feed', (done: MochaDone) => {
137166
this.timeout(1000);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from 'path';
2+
3+
import { TaskLibAnswerExecResult } from 'vsts-task-lib/mock-answer';
4+
import * as tmrm from 'vsts-task-lib/mock-run';
5+
6+
import { NpmCommand, NpmTaskInput, RegistryLocation } from '../constants';
7+
import { NpmMockHelper } from './NpmMockHelper';
8+
9+
let taskPath = path.join(__dirname, '..', 'npm.js');
10+
let tmr = new NpmMockHelper(taskPath);
11+
12+
tmr.setInput(NpmTaskInput.Command, NpmCommand.Custom);
13+
tmr.setInput(NpmTaskInput.CustomCommand, 'mockcmd');
14+
tmr.setInput(NpmTaskInput.WorkingDir, '');
15+
tmr.setInput(NpmTaskInput.CustomRegistry, RegistryLocation.Npmrc);
16+
tmr.setInput(NpmTaskInput.CustomEndpoint, '1');
17+
let auth = {
18+
scheme: 'Token',
19+
parameters: {
20+
'apitoken': 'AUTHTOKEN'
21+
}
22+
};
23+
tmr.mockServiceEndpoint('1', 'http://example.com/1/', auth);
24+
tmr.mockNpmCommand('mockcmd', {
25+
code: 0,
26+
stdout: 'npm custom successful'
27+
} as TaskLibAnswerExecResult);
28+
29+
tmr.run();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from 'path';
2+
3+
import { TaskLibAnswerExecResult } from 'vsts-task-lib/mock-answer';
4+
import * as tmrm from 'vsts-task-lib/mock-run';
5+
6+
import { NpmCommand, NpmTaskInput, RegistryLocation } from '../constants';
7+
import { NpmMockHelper } from './NpmMockHelper';
8+
9+
let taskPath = path.join(__dirname, '..', 'npm.js');
10+
let tmr = new NpmMockHelper(taskPath);
11+
12+
tmr.setInput(NpmTaskInput.Command, NpmCommand.Install);
13+
tmr.setInput(NpmTaskInput.WorkingDir, '');
14+
tmr.setInput(NpmTaskInput.CustomRegistry, RegistryLocation.Npmrc);
15+
tmr.setInput(NpmTaskInput.CustomEndpoint, '1,2');
16+
let auth = {
17+
scheme: 'Token',
18+
parameters: {
19+
'apitoken': 'AUTHTOKEN'
20+
}
21+
};
22+
tmr.mockServiceEndpoint('1', 'http://example.com/1/', auth);
23+
tmr.mockServiceEndpoint('2', 'http://example.com/2/', auth);
24+
tmr.mockNpmCommand('install', {
25+
code: 0,
26+
stdout: 'npm install successful'
27+
} as TaskLibAnswerExecResult);
28+
29+
tmr.run();

Tasks/Npm/npmcustom.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ export async function run(command?: string): Promise<void> {
2121
break;
2222
case RegistryLocation.Npmrc:
2323
tl.debug(tl.loc('UseNpmrc'));
24-
let endpointId = tl.getInput(NpmTaskInput.CustomEndpoint);
25-
if (endpointId) {
26-
npmRegistries.push(NpmRegistry.FromServiceEndpoint(endpointId, true));
24+
let endpointIds = tl.getInput(NpmTaskInput.CustomEndpoint);
25+
if (endpointIds) {
26+
let endpoints = endpointIds.split(',');
27+
let endpointRegistries = endpoints.map(e => NpmRegistry.FromServiceEndpoint(e, true));
28+
npmRegistries = npmRegistries.concat(endpointRegistries);
2729
}
2830
break;
2931
}

Tasks/Npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vsts-npm-task",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "VSTS NPM Task",
55
"main": "npmtask.js",
66
"scripts": {

Tasks/Npm/task.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 1,
1111
"Minor": 0,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"runsOn": [
1515
"Agent",
@@ -89,7 +89,10 @@
8989
"label": "Credentials for registries outside this account/collection",
9090
"helpMarkDown": "Credentials to use for external registries located in the project's .npmrc. For registries in this account/collection, leave this blank; the build’s credentials are used automatically.",
9191
"type": "connectedService:externalnpmregistry",
92-
"visibleRule": "customRegistry = useNpmrc"
92+
"visibleRule": "customRegistry = useNpmrc",
93+
"properties": {
94+
"MultiSelectFlatList": "true"
95+
}
9396
},
9497
{
9598
"groupName": "publishRegistries",

Tasks/Npm/task.loc.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 1,
1111
"Minor": 0,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"runsOn": [
1515
"Agent",
@@ -89,7 +89,10 @@
8989
"label": "ms-resource:loc.input.label.customEndpoint",
9090
"helpMarkDown": "ms-resource:loc.input.help.customEndpoint",
9191
"type": "connectedService:externalnpmregistry",
92-
"visibleRule": "customRegistry = useNpmrc"
92+
"visibleRule": "customRegistry = useNpmrc",
93+
"properties": {
94+
"MultiSelectFlatList": "true"
95+
}
9396
},
9497
{
9598
"groupName": "publishRegistries",

Tasks/Npm/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export async function getPackagingCollectionUrl(): Promise<string> {
7979
}
8080

8181
export function getTempNpmrcPath(): string {
82-
let tempUserNpmrcPath: string = path.join(getTempPath(), `${tl.getVariable('Build.BuildId')}.npmrc`);
82+
let id: string = tl.getVariable('Build.BuildId') || tl.getVariable('Release.ReleaseId');
83+
let tempUserNpmrcPath: string = path.join(getTempPath(), `${id}.npmrc`);
8384

8485
return tempUserNpmrcPath;
8586
}

0 commit comments

Comments
 (0)