Simple utility implemented on top of child_process to run shell commands with many configurable runtime options
npm i --save @boltkit/node-shell-cmd
// inside MJS module
import {ShellCommand} from "@boltkit/node-shell-cmd"
// inside CJS module
const {ShellCommand} = require("@boltkit/node-shell-cmd");
shellis the shell binary you want ot usescriptis the script path you want to runcwdis the directory from which you want to run the scriptoptsis an array of arguments you want to passredirectStdErrlet you rediect all stderr to stdout (off by default)
const cmd = new ShellCommand({
shell: "/bin/sh",
script: "test_scripts/trigger_error.sh",
cwd: "",
opts: ["abc"],
redirectStdErr: false
});
try {
await cmd.run();
console.log(cmd.stdout);
} catch (err) {
console.log(err.message);
console.log(cmd.stderr);
}
const cmd = new ShellCommand({
script: "test_scripts/trigger_error.sh",
redirectStdErr: true
});
try {
await cmd.run();
console.log(cmd.stdout);
} catch (err) {
console.log(err.message);
console.log(cmd.stderr);
}
stderr will not contain the script error output, but stdout will
Exited with code 2
const cmd = new ShellCommand({
script: "test_scripts/trigger_error.sh",
redirectStdErr: false
});
try {
await cmd.run();
console.log(cmd.stdout);
} catch (err) {
console.log(err.message);
console.log(cmd.stderr);
}
stderr will contain the script error output
Exited with code 2
ls: cannot access 'somefilethatneverexistedhereee': No such file or directory