(
program: string,
env: { [key: string]: string | null | undefined },
)
| 11 | * Lookup the given program on the PATH and return its absolute path on success and undefined otherwise. |
| 12 | */ |
| 13 | export function findInPath( |
| 14 | program: string, |
| 15 | env: { [key: string]: string | null | undefined }, |
| 16 | ): string | undefined { |
| 17 | let locator: string; |
| 18 | if (process.platform === 'win32') { |
| 19 | const windir = env['WINDIR'] || 'C:\\Windows'; |
| 20 | locator = path.join(windir, 'System32', 'where.exe'); |
| 21 | } else { |
| 22 | locator = '/usr/bin/which'; |
| 23 | } |
| 24 | |
| 25 | try { |
| 26 | if (fs.existsSync(locator)) { |
| 27 | const lines = childProcess |
| 28 | .execSync(`${locator} ${program}`, { env: removeNulls(env) }) |
| 29 | .toString() |
| 30 | .split(/\r?\n/); |
| 31 | |
| 32 | if (process.platform === 'win32') { |
| 33 | // return the first path that has a executable extension |
| 34 | const executableExtensions = String(env['PATHEXT'] || '.exe') |
| 35 | .toUpperCase() |
| 36 | .split(';'); |
| 37 | |
| 38 | for (const candidate of lines) { |
| 39 | const ext = path.extname(candidate).toUpperCase(); |
| 40 | if (ext && executableExtensions.includes(ext)) { |
| 41 | return candidate; |
| 42 | } |
| 43 | } |
| 44 | } else { |
| 45 | // return the first path |
| 46 | if (lines.length > 0) { |
| 47 | return lines[0]; |
| 48 | } |
| 49 | } |
| 50 | return undefined; |
| 51 | } else { |
| 52 | // do not report failure if 'locator' app doesn't exist |
| 53 | } |
| 54 | return program; |
| 55 | } catch (err) { |
| 56 | // fall through |
| 57 | } |
| 58 | |
| 59 | // fail |
| 60 | return undefined; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Ensures the program exists, adding its executable as necessary on Windows. |
no test coverage detected