shell-command-builder
Construct and execute shell commands
Install:
yarn add shell-command-builder
npm install shell-command-builder
Require or import the module:
const { CommandBuilder, run, interactive } = require("shell-command-builder");
import { CommandBuilder, run, interactive } from "shell-command-builder";
Build shell commands with arguments:
const command = new CommandBuilder("foo", [
"file.text",
"--flag-1",
"--flag-2",
"-a",
]);
Convert command to a string:
String(command);
command.toString();
`command = ${command}`;
Use fluent API to add args/flags after initial command is created:
const command = new CommandBuilder("foo");
command.arg("--hello");
console.log(command);
Pass in a truthy/falsy condition to optionally add args/flags:
const command = new CommandBuilder("foo");
const options = {
watch: true,
quiet: false,
};
command.arg("--watch", options.watch);
command.arg("--quiet", options.quiet);
console.log(command);
Run commands:
const command = new CommandBuilder("echo 'hello, world!'");
const { stdout } = await command.run();
const { stdout } = await run(command);
await command.interactive();
await interactive(command);