Nabbing Pleasant Monads

    cmd-spawn
    TypeScript icon, indicating that this package has built-in type declarations

    1.4.0 • Public • Published

    Build Status Coverage Status npm

    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.

    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 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 - ? 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.x because of package.json - prepare script. (only required to run hook when publish)
    • npm -g i gulp-cli jest-cli.

    Usage

    • gulp --tasks to get going.

    Developing

    • jest --watchAll to watch recompiled files and rerun tests.

    Testing

    Supports:

    • jest, needs jest-cli installed. it will execute the transpiled files from typescript.

    Dist

    • 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.

    Install

    npm i cmd-spawn

    DownloadsWeekly Downloads

    3

    Version

    1.4.0

    License

    ISC

    Last publish

    Collaborators

    • linken