(
name: string,
opts: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {},
)
| 44 | * - On POSIX we walk PATH looking for `<dir>/<name>` as a regular file. |
| 45 | */ |
| 46 | export function findExecutableOnPath( |
| 47 | name: string, |
| 48 | opts: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {}, |
| 49 | ): string | null { |
| 50 | const platform = opts.platform ?? process.platform; |
| 51 | const env = opts.env ?? process.env; |
| 52 | if (!name) return null; |
| 53 | |
| 54 | // Caller passed a path or an explicit extension — check it directly. |
| 55 | if (name.includes('/') || name.includes('\\') || /\.[^.\\/]+$/.test(name)) { |
| 56 | return isFile(name) ? name : null; |
| 57 | } |
| 58 | |
| 59 | // Windows env var casing is unstable across hosts; check both. |
| 60 | const dirs = (env['PATH'] ?? env['Path'] ?? '').split(delimiter).filter(Boolean); |
| 61 | |
| 62 | if (platform === 'win32') { |
| 63 | const exts = (env['PATHEXT'] ?? DEFAULT_PATHEXT) |
| 64 | .split(';') |
| 65 | .map((e) => e.trim()) |
| 66 | .filter(Boolean); |
| 67 | for (const dir of dirs) { |
| 68 | for (const ext of exts) { |
| 69 | const candidate = join(dir, name + ext.toLowerCase()); |
| 70 | if (isFile(candidate)) return candidate; |
| 71 | } |
| 72 | } |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | for (const dir of dirs) { |
| 77 | const candidate = join(dir, name); |
| 78 | if (isFile(candidate)) return candidate; |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | /** Detect whether a single binary is installed. */ |
| 84 | export function detectBinary( |
no test coverage detected