This package is an adaptation of Go's simple and elegant os/exec.Cmd package. It tries to provide a reliable and simple way to run sub-processes.
The API and semantics prioritize clarity and simplicity over ease.
-
Create a new command structure for running a sub-process
class Cmd(command :string, ...args :string[])
implementsCmdOptions
-
Create & start a command. Thin wrapper around new Cmd... with strong TypeScript typings.
function startCmd(command :string, args? :string[], options? :CmdOptions)
interface CmdOptions {
dir?: string; // working directory. If empty, uses current working directory
env?: { [name: string]: string | undefined }; // process environment variable
shell?: boolean | string; // run command in the system-default shell
stdin?: Readable | 'inherit' | 'pipe' | Buffer | Reader | null; // fd 0
stdout?: Writable | 'inherit' | 'pipe' | null; // fd 1
stderr?: Writable | 'inherit' | 'pipe' | null; // fd 2
extraFiles?: (Readable | 'pipe' | null)[]; // more fds 3...
windowsHide?: boolean;
}
const [, { stdout: dateout }] = startCmd('date', [], { stdout: 'pipe' });
const [cmd, stdio] = startCmd('cat', [], { stdin: dateout, stdout: 'inherit' });
await cmd.wait();
const cmd = new Cmd('top');
cmd.stdout = process.stdout;
await cmd.run();
console.log(await(new Cmd('uname', '-a')).output('utf8'));
const cmd = new Cmd('tr', '[:lower:]', '[:upper:]');
cmd.stdin = Buffer.from('Hello world\n');
console.log(await cmd.output('utf8'));
const cmd = new Cmd('tr', '[:upper:]', '[:lower:]');
cmd.stdin = io.createFileReader('jokes.txt');
console.log(await cmd.output('utf8'));
const cmd = new Cmd('if [ -d . ]; then ls; fi');
cmd.shell = true;
cmd.dir = '~';
console.log(await cmd.output('utf8'));
const date = new Cmd('date');
date.stdout = 'pipe';
const { stdout: dateout } = date.start();
const cmd = new Cmd('cat');
cmd.stdin = dateout;
cmd.stdout = process.stdout;
await cmd.run();