Construct and execute shell commands
Install:
yarn add shell-command-builder
# OR
npm install shell-command-builder
Require or import the module:
const { CommandBuilder, run, interactive } = require("shell-command-builder");
// OR
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",
]); //=> 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); //=> foo --hello
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); // added
command.arg("--quiet", options.quiet); // not added
console.log(command); //=> foo --watch
Run commands:
const command = new CommandBuilder("echo 'hello, world!'");
const { stdout } = await command.run(); //=> hello, world!
// OR
const { stdout } = await run(command); //=> hello, world!
// OR
await command.interactive(); //=[shell]=> hello, world!
// OR
await interactive(command); //=[shell]=> hello, world!