Run shell commands as string. Typescript(+typings) ES6 module. Node 6+ spawn with promises, buffered and unbuffered output
npm install cmd-spawn


Run shell commands as string. Typescript(+typings) ES2015/ES2017 module. Node 6+ spawn with promises, buffered and unbuffered output, inspired by node-buffered-spawn.
Features:
- Written in typescript and typings are auto generated.
- Promise based (Bluebird instance returned).
- child process exposed in returned promise as cp property.
- If shell option is passed to spawn, the shell commands can be run as is, even with pipes.
- Auto passed process.env if spawn env not overriden.
- Normal version and buffered(collects all output and then resolve) option.
- Uses cross-spawn by default, can be disabled.
npm -S i cmd-spawn
``js
import { cmdSpawn } from 'cmd-spwawn';
// Inherit process.env auto if not overriden
// Told to run the command as is in a shell
// Returns Bluebird Promise
const promise = cmdSpawn('GITHUB_TOKEN=$TOKEN_ENV git clone git@github.com:beckend/cmd-spawn.git', {
spawnOpts:
shell: true
}
});
// child process is always in property p
promise.cp.on('data', (data: Buffer) => {
console.log(data.toString());
});
promise.cp.once('close', (code) => {
if (code === 0) {
console.log('success');
} else {
console.log('fail');
}
});
`
js
// compile typescript project
const promise = cmdSpawn('tsc --p src/tsconfig-es2015.json', { buffer: true });
// Bluebird
promise
.then((result) => {
console.log(result.stdout);
console.log(result.stderr);
})
.catch((er) => {
// child process error
console.log(er);
})
.finally(() => {
console.log('done');
});
`$3
Can be found in src/__test__/cmd-spawn.spec.ts.
API
`js
import { cmdSpawn } from 'cmd-spawn';
`
usage: cmdSpawn(cmd, options)| Parameter | Default | Type | Description |
|:---|:---|:---|:---|
| cmd | undefined |
string or Array | command to run, if array is given, the first index is the command and rest becomes arguments. |
| options | { spawnOpts: {}, crossSpawn: true, buffer: false } | object | Options described below. |options - ? means optional
`js
{
// Options passed to spawn
spawnOpts?: SpawnOptions;
// buffer output flag, default false
buffer?: boolean;
// crossSpawn flag, default enabled
crossSpawn?: boolean;
}
`Contributing
$3
- node@6+
- npm@4.x because of package.json - prepare script. (only required to run hook when publish)
- npm -g i gulp-cli jest-cli.$3
- gulp --tasks to get going.$3
- jest --watchAll to watch recompiled files and rerun tests.$3
Supports:
- jest, needs jest-cli installed. it will execute the transpiled files from typescript.$3
- gulp will run default task which consist of running tasks:
- lint, clean, build, minify then jest and collect coverage.Note: All
minified` files are only ES5.