@@ -6,11 +6,14 @@ import * as Q from 'q';
66import * as tl from 'vsts-task-lib/task' ;
77import * as tr from 'vsts-task-lib/toolrunner' ;
88
9+ import * as util from './util' ;
10+
911export 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}
0 commit comments