( bin: string, env: NodeJS.ProcessEnv = process.env, platform = process.platform, )
| 48 | // ── Detection ────────────────────────────────────────────────────────── |
| 49 | |
| 50 | export function hasCommandOnPath( |
| 51 | bin: string, |
| 52 | env: NodeJS.ProcessEnv = process.env, |
| 53 | platform = process.platform, |
| 54 | ): boolean { |
| 55 | const searchPath = env.PATH ?? ''; |
| 56 | const pathDirs = searchPath.split(path.delimiter).filter(Boolean); |
| 57 | const pathext = (env.PATHEXT ?? '.EXE;.CMD;.BAT;.COM') |
| 58 | .split(';') |
| 59 | .map((ext) => ext.trim()) |
| 60 | .filter(Boolean); |
| 61 | |
| 62 | const hasPathSeparator = /[\\/]/.test(bin); |
| 63 | const baseCandidates = hasPathSeparator |
| 64 | ? [bin] |
| 65 | : pathDirs.map((dir) => path.join(dir, bin)); |
| 66 | const candidates = platform === 'win32' |
| 67 | ? baseCandidates.flatMap((candidate) => { |
| 68 | if (path.extname(candidate)) return [candidate]; |
| 69 | return pathext.map((ext) => `${candidate}${ext}`); |
| 70 | }) |
| 71 | : baseCandidates; |
| 72 | |
| 73 | return candidates.some((candidate) => { |
| 74 | try { |
| 75 | if (platform === 'win32') return fs.statSync(candidate).isFile(); |
| 76 | fs.accessSync(candidate, fs.constants.X_OK); |
| 77 | return true; |
| 78 | } catch { |
| 79 | return false; |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | export function detectAvailableEngines(): string[] { |
| 85 | return PREFERENCE_ORDER.filter((name) => hasCommandOnPath(KNOWN_ENGINES[name].bin)); |
no outgoing calls
no test coverage detected