openai/codex-action
Publicmirrored from https://github.com/openai/codex-actionAvailable
src/which.ts
32lines · modeblame
53fdba22Michael Bolin10 months ago | 1 | import { spawn } from "child_process"; |
| 2 | | |
| 3 | export function which(command: string): Promise<string | null> { | |
| 4 | return new Promise<string>((resolve, reject) => { | |
| 5 | const which = spawn("which", [command], { | |
| 6 | env: process.env, | |
| 7 | stdio: ["ignore", "pipe", "inherit"], | |
| 8 | }); | |
| 9 | | |
| 10 | which.on("error", reject); | |
| 11 | | |
| 12 | let output = ""; | |
| 13 | which.stdout.on("data", (chunk) => { | |
| 14 | output += chunk.toString(); | |
| 15 | }); | |
| 16 | | |
| 17 | which.on("close", (code) => { | |
| 18 | if (code !== 0) { | |
| 19 | reject(new Error(`which exited with code ${code}`)); | |
| 20 | return; | |
| 21 | } | |
| 22 | | |
| 23 | const path = output.trim(); | |
| 24 | if (path === "") { | |
| 25 | reject(new Error("could not find 'codex' in PATH")); | |
| 26 | return; | |
| 27 | } | |
| 28 | | |
| 29 | resolve(path); | |
| 30 | }); | |
| 31 | }); | |
| 32 | } |