Skip to content

Commit 4192e7d

Browse files
committed
refactor: SystemUtilities.tryRun()
1 parent ad31eb8 commit 4192e7d

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/shared/sam/cli/samCliSettings.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ export class SamCliSettings extends fromExtensionManifest('aws.samcli', descript
9494
}
9595

9696
/**
97-
* Gets location of `sam` from user config, or tries to find `sam` on the
98-
* system if the user config is invalid.
97+
* Gets location of `sam` from:
98+
* 1. previous saved location (if valid), or
99+
* 2. user config (if valid), or
100+
* 3. tries to find `sam` on the system if the user config is invalid.
99101
*
100102
* @returns `autoDetected=true` if auto-detection was _attempted_.
101103
*/

src/shared/systemUtilities.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ export class SystemUtilities {
183183
// TODO: implement this by checking the file mode
184184
// public static async checkExactPerms(file: string | vscode.Uri, perms: `${PermissionsTriplet}${PermissionsTriplet}${PermissionsTriplet}`)
185185

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+
186207
/**
187208
* Gets the fullpath to `code` (VSCode CLI), or falls back to "code" (not
188209
* absolute) if it works.
@@ -217,13 +238,10 @@ export class SystemUtilities {
217238
if (!vsc || (vsc !== 'code' && !fs.existsSync(vsc))) {
218239
continue
219240
}
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'])) {
223242
SystemUtilities.vscPath = vsc
224243
return vsc
225244
}
226-
getLogger().warn('getVscodeCliPath: failed: %s %O', proc, proc.result())
227245
}
228246

229247
return undefined
@@ -245,8 +263,7 @@ export class SystemUtilities {
245263

246264
for (const tsc of tscPaths) {
247265
// 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')) {
250267
return tsc
251268
}
252269
}
@@ -275,13 +292,10 @@ export class SystemUtilities {
275292
if (!p || ('ssh' !== p && !fs.existsSync(p))) {
276293
continue
277294
}
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'])) {
281296
SystemUtilities.sshPath = p
282297
return p
283298
}
284-
getLogger().warn('findSshPath: failed: %s', proc)
285299
}
286300
}
287301

@@ -300,13 +314,10 @@ export class SystemUtilities {
300314
if (!p || ('git' !== p && !fs.existsSync(p))) {
301315
continue
302316
}
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'])) {
306318
SystemUtilities.gitPath = p
307319
return p
308320
}
309-
getLogger().warn('findGitPath: failed: %s', proc)
310321
}
311322
}
312323

@@ -323,13 +334,10 @@ export class SystemUtilities {
323334
if (!p || ('bash' !== p && !fs.existsSync(p))) {
324335
continue
325336
}
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'])) {
329338
SystemUtilities.bashPath = p
330339
return p
331340
}
332-
getLogger().warn('findBashPath: failed: %s', proc)
333341
}
334342
}
335343

0 commit comments

Comments
 (0)