Skip to content

Commit b3e2bea

Browse files
Merge pull request #4599 from aldoms/users/aldoms/releasesM119
Override project .npmrc when install from a local feed
2 parents 2ee09dc + 09c29af commit b3e2bea

File tree

11 files changed

+60
-22
lines changed

11 files changed

+60
-22
lines changed

Tasks/Npm/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@
4343
"loc.messages.DebugLogNotFound": "Couldn't find a debug log in the cache or working directory",
4444
"loc.messages.NpmFailed": "Npm failed with return code: %s",
4545
"loc.messages.FoundNpmDebugLog": "Found npm debug log, make sure the path matches with the one in npm's output: %s",
46-
"loc.messages.TestDebugLog": "Trying debug log location: %s"
46+
"loc.messages.TestDebugLog": "Trying debug log location: %s",
47+
"loc.messages.OverridingProjectNpmrc": "Overriding project .npmrc: %s",
48+
"loc.messages.RestoringProjectNpmrc": "Restoring project .npmrc"
4749
}

Tasks/Npm/Tests/L0.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ describe('Npm Task', function () {
127127

128128
assert.equal(tr.invokedToolCount, 2, 'task should have run npm');
129129
assert(tr.stdOutContained('npm install successful'), 'npm should have installed the package');
130+
assert(tr.stdOutContained('OverridingProjectNpmrc'), 'install from feed shoud override project .npmrc');
131+
assert(tr.stdOutContained('RestoringProjectNpmrc'), 'install from .npmrc shoud restore project .npmrc');
130132
assert(tr.succeeded, 'task should have succeeded');
131133

132134
done();
@@ -141,6 +143,8 @@ describe('Npm Task', function () {
141143

142144
assert.equal(tr.invokedToolCount, 2, 'task should have run npm');
143145
assert(tr.stdOutContained('npm install successful'), 'npm should have installed the package');
146+
assert(!tr.stdOutContained('OverridingProjectNpmrc'), 'install from .npmrc shoud not override project .npmrc');
147+
assert(!tr.stdOutContained('RestoringProjectNpmrc'), 'install from .npmrc shoud not restore project .npmrc');
144148
assert(tr.succeeded, 'task should have succeeded');
145149

146150
done();
@@ -171,6 +175,8 @@ describe('Npm Task', function () {
171175

172176
assert.equal(tr.invokedToolCount, 2, 'task should have run npm');
173177
assert(tr.stdOutContained('npm publish successful'), 'npm should have installed the package');
178+
assert(tr.stdOutContained('OverridingProjectNpmrc'), 'publish should always ooverrideverride project .npmrc');
179+
assert(tr.stdOutContained('RestoringProjectNpmrc'), 'publish should always restore project .npmrc');
174180
assert(tr.succeeded, 'task should have succeeded');
175181

176182
done();

Tasks/Npm/Tests/install-feed.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tmr.mockNpmCommand('install', {
1717
code: 0,
1818
stdout: 'npm install successful'
1919
} as TaskLibAnswerExecResult);
20+
tmr.answers.rmRF[path.join(process.cwd(), '.npmrc')] = { success: true };
2021
tmr.RegisterLocationServiceMocks();
2122

2223
tmr.run();

Tasks/Npm/Tests/publish-external.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tmr.mockNpmCommand('publish', {
2424
code: 0,
2525
stdout: 'npm publish successful'
2626
} as TaskLibAnswerExecResult);
27-
tmr.answers.rmRF['workingDir\\.npmrc'] = { success: true };
27+
tmr.answers.rmRF[path.join('workingDir', '.npmrc')] = { success: true };
2828
tmr.RegisterLocationServiceMocks();
2929

3030
tmr.run();

Tasks/Npm/Tests/publish-feed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tmr.mockNpmCommand('publish', {
1717
code: 0,
1818
stdout: 'npm publish successful'
1919
} as TaskLibAnswerExecResult);
20-
tmr.answers.rmRF['workingDir\\.npmrc'] = { success: true };
20+
tmr.answers.rmRF[path.join('workingDir', '.npmrc')] = { success: true };
2121
tmr.RegisterLocationServiceMocks();
2222

2323
tmr.run();

Tasks/Npm/npmcustom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ export async function run(command?: string): Promise<void> {
1111
let workingDir = tl.getInput(NpmTaskInput.WorkingDir) || process.cwd();
1212
let npmrc = util.getTempNpmrcPath();
1313
let npmRegistries: INpmRegistry[] = await util.getLocalNpmRegistries(workingDir);
14+
let overrideNpmrc = false;
1415

1516
let registryLocation = tl.getInput(NpmTaskInput.CustomRegistry);
1617
switch (registryLocation) {
1718
case RegistryLocation.Feed:
1819
tl.debug(tl.loc('UseFeed'));
20+
overrideNpmrc = true;
1921
let feedId = tl.getInput(NpmTaskInput.CustomFeed, true);
2022
npmRegistries.push(await NpmRegistry.FromFeedId(feedId));
2123
break;
@@ -39,7 +41,7 @@ export async function run(command?: string): Promise<void> {
3941
util.appendToNpmrc(npmrc, `${registry.auth}\n`);
4042
}
4143

42-
let npm = new NpmToolRunner(workingDir, npmrc);
44+
let npm = new NpmToolRunner(workingDir, npmrc, overrideNpmrc);
4345
npm.line(command || tl.getInput(NpmTaskInput.CustomCommand, true));
4446

4547
await npm.exec();

Tasks/Npm/npmpublish.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ export async function run(command?: string): Promise<void> {
2828
util.appendToNpmrc(npmrc, `registry=${npmRegistry.url}\n`);
2929
util.appendToNpmrc(npmrc, `${npmRegistry.auth}\n`);
3030

31-
let npm = new NpmToolRunner(workingDir, npmrc);
31+
// For publish, always override their project .npmrc
32+
let npm = new NpmToolRunner(workingDir, npmrc, true);
3233
npm.line('publish');
3334

34-
// We delete their project .npmrc so it won't override our user .npmrc
35-
const projectNpmrc = `${workingDir}\\.npmrc`;
36-
util.saveFile(projectNpmrc);
37-
tl.rmRF(projectNpmrc);
38-
3935
await npm.exec();
40-
util.restoreFile(projectNpmrc);
4136

4237
tl.rmRF(npmrc);
4338
tl.rmRF(util.getTempPath());

Tasks/Npm/npmtoolrunner.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import * as Q from 'q';
66
import * as tl from 'vsts-task-lib/task';
77
import * as tr from 'vsts-task-lib/toolrunner';
88

9+
import * as util from './util';
10+
911
export class NpmToolRunner extends tr.ToolRunner {
1012
private cacheLocation: string;
1113
private dbg: boolean;
14+
private projectNpmrc: () => string = () => path.join(this.workingDirectory, '.npmrc');
1215

13-
constructor(private workingDirectory: string, private npmrc?: string) {
16+
constructor(private workingDirectory: string, private npmrc: string, private overrideProjectNpmrc: boolean) {
1417
super('npm');
1518

1619
this.on('debug', (message: string) => {
@@ -29,17 +32,27 @@ export class NpmToolRunner extends tr.ToolRunner {
2932
public exec(options?: tr.IExecOptions): Q.Promise<number> {
3033
options = this._prepareNpmEnvironment(options) as tr.IExecOptions;
3134

32-
return super.exec(options).catch((reason: any) => {
33-
return this._printDebugLog(this._getDebugLogPath(options)).then((value: void): number => {
34-
throw reason;
35-
});
36-
});
35+
this._saveProjectNpmrc();
36+
return super.exec(options).then(
37+
(code: number): number => {
38+
this._restoreProjectNpmrc();
39+
return code;
40+
},
41+
(reason: any) => {
42+
this._restoreProjectNpmrc();
43+
return this._printDebugLog(this._getDebugLogPath(options)).then((value: void): number => {
44+
throw reason;
45+
});
46+
}
47+
);
3748
}
3849

3950
public execSync(options?: tr.IExecSyncOptions): tr.IExecSyncResult {
4051
options = this._prepareNpmEnvironment(options);
4152

53+
this._saveProjectNpmrc();
4254
const execResult = super.execSync(options);
55+
this._restoreProjectNpmrc();
4356
if (execResult.code !== 0) {
4457
this._printDebugLogSync(this._getDebugLogPath(options));
4558
throw new Error(tl.loc('NpmFailed', execResult.code));
@@ -130,4 +143,19 @@ export class NpmToolRunner extends tr.ToolRunner {
130143

131144
console.log(fs.readFileSync(log, 'utf-8'));
132145
}
146+
147+
private _saveProjectNpmrc(): void {
148+
if (this.overrideProjectNpmrc) {
149+
tl.debug(tl.loc('OverridingProjectNpmrc', this.projectNpmrc()));
150+
util.saveFile(this.projectNpmrc());
151+
tl.rmRF(this.projectNpmrc());
152+
}
153+
}
154+
155+
private _restoreProjectNpmrc(): void {
156+
if (this.overrideProjectNpmrc) {
157+
tl.debug(tl.loc('RestoringProjectNpmrc'));
158+
util.restoreFile(this.projectNpmrc());
159+
}
160+
}
133161
}

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.2",
3+
"version": "1.0.3",
44
"description": "VSTS NPM Task",
55
"main": "npmtask.js",
66
"scripts": {

Tasks/Npm/task.json

Lines changed: 4 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": 2
12+
"Patch": 3
1313
},
1414
"runsOn": [
1515
"Agent",
@@ -167,6 +167,8 @@
167167
"DebugLogNotFound": "Couldn't find a debug log in the cache or working directory",
168168
"NpmFailed": "Npm failed with return code: %s",
169169
"FoundNpmDebugLog": "Found npm debug log, make sure the path matches with the one in npm's output: %s",
170-
"TestDebugLog": "Trying debug log location: %s"
170+
"TestDebugLog": "Trying debug log location: %s",
171+
"OverridingProjectNpmrc": "Overriding project .npmrc: %s",
172+
"RestoringProjectNpmrc": "Restoring project .npmrc"
171173
}
172174
}

0 commit comments

Comments
 (0)