@@ -29,13 +29,17 @@ namespace ts.server.typingsInstaller {
2929 }
3030
3131 /** Used if `--npmLocation` is not passed. */
32- function getDefaultNPMLocation ( processName : string ) {
32+ function getDefaultNPMLocation ( processName : string , validateDefaultNpmLocation : boolean , host : InstallTypingHost ) : string {
3333 if ( path . basename ( processName ) . indexOf ( "node" ) === 0 ) {
34- return `"${ path . join ( path . dirname ( process . argv [ 0 ] ) , "npm" ) } "` ;
35- }
36- else {
37- return "npm" ;
34+ const npmPath = path . join ( path . dirname ( process . argv [ 0 ] ) , "npm" ) ;
35+ if ( ! validateDefaultNpmLocation ) {
36+ return npmPath ;
37+ }
38+ if ( host . fileExists ( npmPath ) ) {
39+ return `"${ npmPath } "` ;
40+ }
3841 }
42+ return "npm" ;
3943 }
4044
4145 interface TypesRegistryFile {
@@ -79,15 +83,15 @@ namespace ts.server.typingsInstaller {
7983
8084 private delayedInitializationError : InitializationFailedResponse | undefined ;
8185
82- constructor ( globalTypingsCacheLocation : string , typingSafeListLocation : string , typesMapLocation : string , npmLocation : string | undefined , throttleLimit : number , log : Log ) {
86+ constructor ( globalTypingsCacheLocation : string , typingSafeListLocation : string , typesMapLocation : string , npmLocation : string | undefined , validateDefaultNpmLocation : boolean , throttleLimit : number , log : Log ) {
8387 super (
8488 sys ,
8589 globalTypingsCacheLocation ,
8690 typingSafeListLocation ? toPath ( typingSafeListLocation , "" , createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) ) : toPath ( "typingSafeList.json" , __dirname , createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) ) ,
8791 typesMapLocation ? toPath ( typesMapLocation , "" , createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) ) : toPath ( "typesMap.json" , __dirname , createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) ) ,
8892 throttleLimit ,
8993 log ) ;
90- this . npmPath = npmLocation !== undefined ? npmLocation : getDefaultNPMLocation ( process . argv [ 0 ] ) ;
94+ this . npmPath = npmLocation !== undefined ? npmLocation : getDefaultNPMLocation ( process . argv [ 0 ] , validateDefaultNpmLocation , this . installTypingHost ) ;
9195
9296 // If the NPM path contains spaces and isn't wrapped in quotes, do so.
9397 if ( stringContains ( this . npmPath , " " ) && this . npmPath [ 0 ] !== `"` ) {
@@ -96,6 +100,7 @@ namespace ts.server.typingsInstaller {
96100 if ( this . log . isEnabled ( ) ) {
97101 this . log . writeLine ( `Process id: ${ process . pid } ` ) ;
98102 this . log . writeLine ( `NPM location: ${ this . npmPath } (explicit '${ Arguments . NpmLocation } ' ${ npmLocation === undefined ? "not " : "" } provided)` ) ;
103+ this . log . writeLine ( `validateDefaultNpmLocation: ${ validateDefaultNpmLocation } ` ) ;
99104 }
100105 ( { execSync : this . nodeExecSync } = require ( "child_process" ) ) ;
101106
@@ -229,6 +234,7 @@ namespace ts.server.typingsInstaller {
229234 const typingSafeListLocation = findArgument ( Arguments . TypingSafeListLocation ) ;
230235 const typesMapLocation = findArgument ( Arguments . TypesMapLocation ) ;
231236 const npmLocation = findArgument ( Arguments . NpmLocation ) ;
237+ const validateDefaultNpmLocation = hasArgument ( Arguments . ValidateDefaultNpmLocation ) ;
232238
233239 const log = new FileLog ( logFilePath ) ;
234240 if ( log . isEnabled ( ) ) {
@@ -242,7 +248,7 @@ namespace ts.server.typingsInstaller {
242248 }
243249 process . exit ( 0 ) ;
244250 } ) ;
245- const installer = new NodeTypingsInstaller ( globalTypingsCacheLocation ! , typingSafeListLocation ! , typesMapLocation ! , npmLocation , /*throttleLimit*/ 5 , log ) ; // TODO: GH#18217
251+ const installer = new NodeTypingsInstaller ( globalTypingsCacheLocation ! , typingSafeListLocation ! , typesMapLocation ! , npmLocation , validateDefaultNpmLocation , /*throttleLimit*/ 5 , log ) ; // TODO: GH#18217
246252 installer . listen ( ) ;
247253
248254 function indent ( newline : string , str : string ) : string {
0 commit comments