@@ -8,7 +8,8 @@ import * as path from 'path'
88import fs from '../../shared/fs/fs'
99import { GitExtension } from '../extensions/git'
1010import { Settings } from '../settings'
11- import * as programUtils from './programUtils'
11+ import { ChildProcess , ChildProcessOptions } from './processUtils'
12+ import { getLogger } from '..'
1213
1314/** Full path to VSCode CLI. */
1415let vscPath : string
@@ -50,7 +51,7 @@ export async function getVscodeCliPath(): Promise<string | undefined> {
5051 if ( ! vsc || ( vsc !== 'code' && ! ( await fs . exists ( vsc ) ) ) ) {
5152 continue
5253 }
53- if ( await programUtils . tryRun ( vsc , [ '--version' ] ) ) {
54+ if ( await tryRun ( vsc , [ '--version' ] ) ) {
5455 vscPath = vsc
5556 return vsc
5657 }
@@ -75,7 +76,7 @@ export async function findTypescriptCompiler(): Promise<string | undefined> {
7576
7677 for ( const tsc of tscPaths ) {
7778 // Try to run "tsc -v".
78- if ( await programUtils . tryRun ( tsc , [ '-v' ] , 'yes' , 'Version' ) ) {
79+ if ( await tryRun ( tsc , [ '-v' ] , 'yes' , 'Version' ) ) {
7980 return tsc
8081 }
8182 }
@@ -104,7 +105,7 @@ export async function findSshPath(useCache: boolean = true): Promise<string | un
104105 if ( ! p || ( 'ssh' !== p && ! ( await fs . exists ( p ) ) ) ) {
105106 continue
106107 }
107- if ( await programUtils . tryRun ( p , [ '-G' , 'x' ] , 'noresult' /* "ssh -G" prints quasi-sensitive info. */ ) ) {
108+ if ( await tryRun ( p , [ '-G' , 'x' ] , 'noresult' /* "ssh -G" prints quasi-sensitive info. */ ) ) {
108109 sshPath = p
109110 return p
110111 }
@@ -126,7 +127,7 @@ export async function findGitPath(): Promise<string | undefined> {
126127 if ( ! p || ( 'git' !== p && ! ( await fs . exists ( p ) ) ) ) {
127128 continue
128129 }
129- if ( await programUtils . tryRun ( p , [ '--version' ] ) ) {
130+ if ( await tryRun ( p , [ '--version' ] ) ) {
130131 gitPath = p
131132 return p
132133 }
@@ -146,9 +147,36 @@ export async function findBashPath(): Promise<string | undefined> {
146147 if ( ! p || ( 'bash' !== p && ! ( await fs . exists ( p ) ) ) ) {
147148 continue
148149 }
149- if ( await programUtils . tryRun ( p , [ '--version' ] ) ) {
150+ if ( await tryRun ( p , [ '--version' ] ) ) {
150151 bashPath = p
151152 return p
152153 }
153154 }
154155}
156+
157+ /**
158+ * Tries to execute a program at path `p` with the given args and
159+ * optionally checks the output for `expected`.
160+ *
161+ * @param p path to a program to execute
162+ * @param args program args
163+ * @param doLog log failures
164+ * @param expected output must contain this string
165+ */
166+ export async function tryRun (
167+ p : string ,
168+ args : string [ ] ,
169+ logging : 'yes' | 'no' | 'noresult' = 'yes' ,
170+ expected ?: string ,
171+ opt ?: ChildProcessOptions
172+ ) : Promise < boolean > {
173+ const proc = new ChildProcess ( p , args , { logging : 'no' } )
174+ const r = await proc . run ( opt )
175+ const ok = r . exitCode === 0 && ( expected === undefined || r . stdout . includes ( expected ) )
176+ if ( logging === 'noresult' ) {
177+ getLogger ( ) . info ( 'tryRun: %s: %s' , ok ? 'ok' : 'failed' , proc )
178+ } else if ( logging !== 'no' ) {
179+ getLogger ( ) . info ( 'tryRun: %s: %s %O' , ok ? 'ok' : 'failed' , proc , proc . result ( ) )
180+ }
181+ return ok
182+ }
0 commit comments