使用 Bun.spawn() 來啟動子程序。
const proc = Bun.spawn(["echo", "hello"]);
// await completion
await proc.exited;
第二個引數接受一個配置物件。
const proc = Bun.spawn(["echo", "Hello, world!"], {
cwd: "/tmp",
env: { FOO: "bar" },
onExit(proc, exitCode, signalCode, error) {
// exit handler
},
});
預設情況下,子程序的 stdout 可以透過 proc.stdout 作為 ReadableStream 進行讀取。
const proc = Bun.spawn(["echo", "hello"]);
const output = await proc.stdout.text();
output; // => "hello\n"
有關完整文件,請參閱 文件 > API > 子程序。