1.4.0 • Published 9 years ago
cmd-spawn v1.4.0
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 cpproperty.
- 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.
Install
npm -S i cmd-spawn
Usage
unbuffered(normal spawn)
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');
  }
});buffered spawn
// 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');
  });More usage examples
Can be found in src/__test__/cmd-spawn.spec.ts.
API
import { cmdSpawn } from 'cmd-spawn';usage: cmdSpawn(cmd, options)
| Parameter | Default | Type | Description | 
|---|---|---|---|
| cmd | undefined | stringorArray<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 - ? means optional
{
  // Options passed to spawn
  spawnOpts?: SpawnOptions;
  // buffer output flag, default false
  buffer?: boolean;
  // crossSpawn flag, default enabled
  crossSpawn?: boolean;
}Contributing
Requires
- node@6+
- npm@4.xbecause of- package.json-- preparescript. (only required to run hook when publish)
- npm -g i gulp-cli jest-cli.
Usage
- gulp --tasksto get going.
Developing
- jest --watchAllto watch recompiled files and rerun tests.
Testing
Supports:
- jest, needs- jest-cliinstalled. it will execute the transpiled files from typescript.
Dist
- gulpwill run default task which consist of running tasks:
- lint,- clean,- build,- minifythen- jestand collect coverage.
Note: All minified files are only ES5.