@@ -183,6 +183,27 @@ 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 ( p : string , args : string [ ] , doLog ?: boolean , expected ?: string ) : Promise < boolean > {
196
+ const proc = new ChildProcess ( p , args )
197
+ const r = await proc . run ( )
198
+ if ( r . exitCode === 0 && ( expected === undefined || r . stdout . includes ( expected ) ) ) {
199
+ return true
200
+ }
201
+ if ( doLog ) {
202
+ getLogger ( ) . warn ( 'tryRun: failed: %s %O' , proc , proc . result ( ) )
203
+ }
204
+ return false
205
+ }
206
+
186
207
/**
187
208
* Gets the fullpath to `code` (VSCode CLI), or falls back to "code" (not
188
209
* absolute) if it works.
@@ -217,13 +238,10 @@ export class SystemUtilities {
217
238
if ( ! vsc || ( vsc !== 'code' && ! fs . existsSync ( vsc ) ) ) {
218
239
continue
219
240
}
220
- const proc = new ChildProcess ( vsc , [ '--version' ] )
221
- const r = await proc . run ( )
222
- if ( r . exitCode === 0 ) {
241
+ if ( await SystemUtilities . tryRun ( vsc , [ '--version' ] ) ) {
223
242
SystemUtilities . vscPath = vsc
224
243
return vsc
225
244
}
226
- getLogger ( ) . warn ( 'getVscodeCliPath: failed: %s %O' , proc , proc . result ( ) )
227
245
}
228
246
229
247
return undefined
@@ -245,8 +263,7 @@ export class SystemUtilities {
245
263
246
264
for ( const tsc of tscPaths ) {
247
265
// Try to run "tsc -v".
248
- const result = await new ChildProcess ( tsc , [ '-v' ] ) . run ( )
249
- if ( result . exitCode === 0 && result . stdout . includes ( 'Version' ) ) {
266
+ if ( await SystemUtilities . tryRun ( tsc , [ '-v' ] , false , 'Version' ) ) {
250
267
return tsc
251
268
}
252
269
}
@@ -275,13 +292,10 @@ export class SystemUtilities {
275
292
if ( ! p || ( 'ssh' !== p && ! fs . existsSync ( p ) ) ) {
276
293
continue
277
294
}
278
- const proc = new ChildProcess ( p , [ '-G' , 'x' ] )
279
- const r = await proc . run ( )
280
- if ( r . exitCode === 0 ) {
295
+ if ( await SystemUtilities . tryRun ( p , [ '-G' , 'x' ] ) ) {
281
296
SystemUtilities . sshPath = p
282
297
return p
283
298
}
284
- getLogger ( ) . warn ( 'findSshPath: failed: %s' , proc )
285
299
}
286
300
}
287
301
@@ -300,13 +314,10 @@ export class SystemUtilities {
300
314
if ( ! p || ( 'git' !== p && ! fs . existsSync ( p ) ) ) {
301
315
continue
302
316
}
303
- const proc = new ChildProcess ( p , [ '--version' ] )
304
- const r = await proc . run ( )
305
- if ( r . exitCode === 0 ) {
317
+ if ( await SystemUtilities . tryRun ( p , [ '--version' ] ) ) {
306
318
SystemUtilities . gitPath = p
307
319
return p
308
320
}
309
- getLogger ( ) . warn ( 'findGitPath: failed: %s' , proc )
310
321
}
311
322
}
312
323
@@ -323,13 +334,10 @@ export class SystemUtilities {
323
334
if ( ! p || ( 'bash' !== p && ! fs . existsSync ( p ) ) ) {
324
335
continue
325
336
}
326
- const proc = new ChildProcess ( p , [ '--version' ] )
327
- const r = await proc . run ( )
328
- if ( r . exitCode === 0 ) {
337
+ if ( await SystemUtilities . tryRun ( p , [ '--version' ] ) ) {
329
338
SystemUtilities . bashPath = p
330
339
return p
331
340
}
332
- getLogger ( ) . warn ( 'findBashPath: failed: %s' , proc )
333
341
}
334
342
}
335
343
0 commit comments