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
import { cmdSpawn } from 'cmd-spawn';
// 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');
}
});
// 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');
});
Can be found in src/__test__/cmd-spawn.spec.ts
.
import { cmdSpawn } from 'cmd-spawn';
usage: cmdSpawn(cmd, options)
Parameter | Default | Type | Description |
---|---|---|---|
cmd | undefined | string or Array<string> |
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 passed to spawn
spawnOpts?: SpawnOptions;
// buffer output flag, default false
buffer?: boolean;
// crossSpawn flag, default enabled
crossSpawn?: boolean;
}
node@6+
npm@4.x
because ofpackage.json
-prepare
script. (only required to run hook when publish)npm -g i gulp-cli jest-cli
.
gulp --tasks
to get going.
jest --watchAll
to watch recompiled files and rerun tests.
Supports:
jest
, needsjest-cli
installed. it will execute the transpiled files from typescript.
gulp
will run default task which consist of running tasks:lint
,clean
,build
,minify
thenjest
and collect coverage.
Note: All minified
files are only ES5.