Skip to content

Commit

Permalink
feat(node): add child_process.execSync()
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Sep 22, 2022
1 parent c0aaa17 commit 4566ad5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
1 change: 0 additions & 1 deletion node/_tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@
"test-dns-resolveany.js",
"test-fs-access.js",
"test-fs-readdir-stack-overflow.js",
"test-fs-rm.js",
"test-fs-watchfile.js",
"test-fs-write-file-invalid-path.js",
"test-fs-write-file-sync.js",
Expand Down
38 changes: 36 additions & 2 deletions node/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import {
ERR_CHILD_PROCESS_STDIO_MAXBUFFER,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
genericNodeError,
} from "./internal/errors.ts";
import {
ArrayPrototypeJoin,
ArrayPrototypePush,
ObjectAssign,
StringPrototypeSlice,
} from "./internal/primordials.mjs";
import { getSystemErrorName } from "./util.ts";
Expand Down Expand Up @@ -564,8 +567,39 @@ export function execFile(
return child;
}

export function execSync() {
throw new Error("execSync is currently not supported");
function checkExecSyncError(ret, args, cmd) {
let err;
if (ret.error) {
err = ret.error;
ObjectAssign(err, ret);
} else if (ret.status !== 0) {
let msg = "Command failed: ";
msg += cmd || ArrayPrototypeJoin(args, " ");
if (ret.stderr && ret.stderr.length > 0) {
msg += `\n${ret.stderr.toString()}`;
}
err = genericNodeError(msg, ret);
}
return err;
}

export function execSync(command, options) {
const opts = normalizeExecArgs(command, options, null);
const inheritStderr = !opts.options.stdio;

const ret = spawnSync(opts.file, opts.options);

if (inheritStderr && ret.stderr) {
process.stderr.write(ret.stderr);
}

const err = checkExecSyncError(ret, opts.args, command);

if (err) {
throw err;
}

return ret.stdout;
}

export default {
Expand Down

0 comments on commit 4566ad5

Please sign in to comment.