@@ -183,6 +183,32 @@ export class SystemUtilities {
183
183
// TODO: implement this by checking the file mode
184
184
// public static async checkExactPerms(file: string | vscode.Uri, perms: `${PermissionsTriplet}${PermissionsTriplet}${PermissionsTriplet}`)
185
185
186
+ /**
187
+ * Tries to execute a program at path `p` with the given args and
188
+ * optionally checks the output for `expected`.
189
+ *
190
+ * @param p path to a program to execute
191
+ * @param args program args
192
+ * @param doLog log failures
193
+ * @param expected output must contain this string
194
+ */
195
+ public static async tryRun (
196
+ p : string ,
197
+ args : string [ ] ,
198
+ logging : 'yes' | 'no' | 'noresult' = 'yes' ,
199
+ expected ?: string
200
+ ) : Promise < boolean > {
201
+ const proc = new ChildProcess ( p , args , { logging : 'no' } )
202
+ const r = await proc . run ( )
203
+ const ok = r . exitCode === 0 && ( expected === undefined || r . stdout . includes ( expected ) )
204
+ if ( logging === 'noresult' ) {
205
+ getLogger ( ) . info ( 'tryRun: %s: %s' , ok ? 'ok' : 'failed' , proc )
206
+ } else if ( logging !== 'no' ) {
207
+ getLogger ( ) . info ( 'tryRun: %s: %s %O' , ok ? 'ok' : 'failed' , proc , proc . result ( ) )
208
+ }
209
+ return ok
210
+ }
211
+
186
212
/**
187
213
* Gets the fullpath to `code` (VSCode CLI), or falls back to "code" (not
188
214
* absolute) if it works.
@@ -217,13 +243,10 @@ export class SystemUtilities {
217
243
if ( ! vsc || ( vsc !== 'code' && ! fs . existsSync ( vsc ) ) ) {
218
244
continue
219
245
}
220
- const proc = new ChildProcess ( vsc , [ '--version' ] )
221
- const r = await proc . run ( )
222
- if ( r . exitCode === 0 ) {
246
+ if ( await SystemUtilities . tryRun ( vsc , [ '--version' ] ) ) {
223
247
SystemUtilities . vscPath = vsc
224
248
return vsc
225
249
}
226
- getLogger ( ) . warn ( 'getVscodeCliPath: failed: %s %O' , proc , proc . result ( ) )
227
250
}
228
251
229
252
return undefined
@@ -245,8 +268,7 @@ export class SystemUtilities {
245
268
246
269
for ( const tsc of tscPaths ) {
247
270
// Try to run "tsc -v".
248
- const result = await new ChildProcess ( tsc , [ '-v' ] ) . run ( )
249
- if ( result . exitCode === 0 && result . stdout . includes ( 'Version' ) ) {
271
+ if ( await SystemUtilities . tryRun ( tsc , [ '-v' ] , 'yes' , 'Version' ) ) {
250
272
return tsc
251
273
}
252
274
}
@@ -275,13 +297,10 @@ export class SystemUtilities {
275
297
if ( ! p || ( 'ssh' !== p && ! fs . existsSync ( p ) ) ) {
276
298
continue
277
299
}
278
- const proc = new ChildProcess ( p , [ '-G' , 'x' ] )
279
- const r = await proc . run ( )
280
- if ( r . exitCode === 0 ) {
300
+ if ( await SystemUtilities . tryRun ( p , [ '-G' , 'x' ] , 'noresult' /* "ssh -G" prints quasi-sensitive info. */ ) ) {
281
301
SystemUtilities . sshPath = p
282
302
return p
283
303
}
284
- getLogger ( ) . warn ( 'findSshPath: failed: %s' , proc )
285
304
}
286
305
}
287
306
@@ -300,13 +319,10 @@ export class SystemUtilities {
300
319
if ( ! p || ( 'git' !== p && ! fs . existsSync ( p ) ) ) {
301
320
continue
302
321
}
303
- const proc = new ChildProcess ( p , [ '--version' ] )
304
- const r = await proc . run ( )
305
- if ( r . exitCode === 0 ) {
322
+ if ( await SystemUtilities . tryRun ( p , [ '--version' ] ) ) {
306
323
SystemUtilities . gitPath = p
307
324
return p
308
325
}
309
- getLogger ( ) . warn ( 'findGitPath: failed: %s' , proc )
310
326
}
311
327
}
312
328
@@ -323,13 +339,10 @@ export class SystemUtilities {
323
339
if ( ! p || ( 'bash' !== p && ! fs . existsSync ( p ) ) ) {
324
340
continue
325
341
}
326
- const proc = new ChildProcess ( p , [ '--version' ] )
327
- const r = await proc . run ( )
328
- if ( r . exitCode === 0 ) {
342
+ if ( await SystemUtilities . tryRun ( p , [ '--version' ] ) ) {
329
343
SystemUtilities . bashPath = p
330
344
return p
331
345
}
332
- getLogger ( ) . warn ( 'findBashPath: failed: %s' , proc )
333
346
}
334
347
}
335
348
}
0 commit comments