mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
exec(command, callback) and exec(command, { encoding, maxBuffer },
callback) lower like execFile's callback slice. The command line runs
with /bin/sh -c, as execSync runs it, the call returns the live
ChildProcess, and the error-first callback receives the captured utf8
stdout and stderr. A non-zero exit, including the shell's 127 for a
missing command, reports Node's "Command failed: <command>\n<stderr>":
scr_exec_file takes a shell flag so the message shows the command line.
Under @types/node, child_process.ExecException maps to Error, as
ExecFileException does. Its code member is the child's numeric exit
status, which the runtime error's string code slot does not hold, so
reading it, testing it with `in`, or testing its typeof is a named
refusal.
37 lines
1.9 KiB
TypeScript
37 lines
1.9 KiB
TypeScript
// child_process.exec's callback forms: the command line runs with
|
|
// /bin/sh -c, the call returns a live ChildProcess immediately, utf8
|
|
// stdout/stderr are captured, and the error-first callback fires after the
|
|
// current turn. A non-zero exit (a missing command is the shell's 127)
|
|
// reports "Command failed: <command>\n<stderr>".
|
|
import { exec } from "node:child_process";
|
|
import * as childProcess from "node:child_process";
|
|
|
|
const child = exec("printf out; printf err >&2", (error, stdout, stderr) => {
|
|
console.log("ok", error === null, JSON.stringify(stdout), JSON.stringify(stderr));
|
|
exec("printf 'a\\nb\\nc\\n' | wc -l", (pipeError, lines) => {
|
|
console.log("pipe", pipeError === null, lines.trim());
|
|
exec(`printf '%s|' "a b" 'c d' e`, { encoding: "utf8", maxBuffer: 1024 * 1024 }, (_, quoted) => {
|
|
console.log("quoting", quoted);
|
|
exec("printf partial; printf failed >&2; exit 3", (failed, failedOut, failedErr) => {
|
|
console.log("fail", failed !== null, JSON.stringify(failedOut), JSON.stringify(failedErr));
|
|
console.log("fail-message", JSON.stringify(failed === null ? "" : failed.message));
|
|
exec("exit 1", (silent) => {
|
|
console.log("silent", JSON.stringify(silent === null ? "" : silent.message));
|
|
exec("scriptc-definitely-not-a-command", (missing, missingOut) => {
|
|
const message = missing === null ? "" : missing.message;
|
|
console.log("missing", missing !== null, JSON.stringify(missingOut));
|
|
console.log("missing-message", message.split("\n")[0]);
|
|
exec("true", () => {
|
|
console.log("zero-parameter callback");
|
|
childProcess.exec("printf namespace", (_, out) => console.log(out));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
console.log("returned", child.pid !== undefined, child.stdout !== null, child.stderr !== null);
|
|
console.log("scheduled");
|