41 lines
996 B
TypeScript
41 lines
996 B
TypeScript
import { Log } from "../util/log"
|
|
import { text } from "node:stream/consumers"
|
|
import { Process } from "../util/process"
|
|
|
|
export namespace BunProc {
|
|
const log = Log.create({ service: "bun" })
|
|
|
|
export async function run(cmd: string[], options?: Process.Options) {
|
|
log.info("running", {
|
|
cmd: [which(), ...cmd],
|
|
...options,
|
|
})
|
|
const result = Process.spawn([which(), ...cmd], {
|
|
...options,
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
env: {
|
|
...process.env,
|
|
...options?.env,
|
|
BUN_BE_BUN: "1",
|
|
},
|
|
})
|
|
const code = await result.exited
|
|
const stdout = result.stdout ? await text(result.stdout) : undefined
|
|
const stderr = result.stderr ? await text(result.stderr) : undefined
|
|
log.info("done", {
|
|
code,
|
|
stdout,
|
|
stderr,
|
|
})
|
|
if (code !== 0) {
|
|
throw new Error(`Command failed with exit code ${code}`)
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function which() {
|
|
return process.execPath
|
|
}
|
|
}
|