Skip to content

exec

Portable subprocess helper built on child_process.spawn and line streaming.

Ported from dbx-tools-js/packages/cli/src/exec.ts. Each stdio fd defaults to "inherit". spawn streams output line-by-line into ExecResult.stdoutLines / ExecResult.stderrLines; its stdout / stderr getters join those lines. spawnSync keeps the captured string; its stdout / stderr getters read that string directly (line arrays split lazily on read). Omitted trim (default) applies adaptive normalization: spawnSync drops at most one trailing empty line / newline spawnSync adds; spawn does not (readline never emits that extra line). trim: true strips all leading/ trailing whitespace in both modes; trim: false leaves output unchanged.

Capture command output

const { stdout } = await exec("git", ["rev-parse", "--show-toplevel"], {
stdout: "capture",
stderr: "ignore",
stdin: "ignore",
});

Stream and capture together

await exec("pnpm", ["install"], {
stdout: [(line) => console.log(line), "capture"],
check: true,
});

Synchronous capture (no line callbacks)

const { stdout } = execSync("git", ["rev-parse", "--show-toplevel"], {
stdout: "capture",
stderr: "ignore",
stdin: "ignore",
});