| 12 | const req = createRequire(import.meta.url) |
| 13 | |
| 14 | export async function run(cmd: string[], options?: Bun.SpawnOptions.OptionsObject<any, any, any>) { |
| 15 | log.info("running", { |
| 16 | cmd: [which(), ...cmd], |
| 17 | ...options, |
| 18 | }) |
| 19 | const result = Bun.spawn([which(), ...cmd], { |
| 20 | ...options, |
| 21 | stdout: "pipe", |
| 22 | stderr: "pipe", |
| 23 | env: { |
| 24 | ...process.env, |
| 25 | ...options?.env, |
| 26 | BUN_BE_BUN: "1", |
| 27 | }, |
| 28 | }) |
| 29 | const code = await result.exited |
| 30 | const stdout = result.stdout |
| 31 | ? typeof result.stdout === "number" |
| 32 | ? result.stdout |
| 33 | : await readableStreamToText(result.stdout) |
| 34 | : undefined |
| 35 | const stderr = result.stderr |
| 36 | ? typeof result.stderr === "number" |
| 37 | ? result.stderr |
| 38 | : await readableStreamToText(result.stderr) |
| 39 | : undefined |
| 40 | log.info("done", { |
| 41 | code, |
| 42 | stdout, |
| 43 | stderr, |
| 44 | }) |
| 45 | if (code !== 0) { |
| 46 | throw new Error(`Command failed with exit code ${result.exitCode}`) |
| 47 | } |
| 48 | return result |
| 49 | } |
| 50 | |
| 51 | export function which() { |
| 52 | return process.execPath |