# cmd-spawn

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

Latest version **1.4.0** (published 2016-11-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install cmd-spawn
pnpm add cmd-spawn
yarn add cmd-spawn
bun add cmd-spawn
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2016-11-17 |
| First published | 2016-11-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.9.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Linken Dinh |
| Maintainers | linken |
| Keywords | typescript, node, buffered, buffered-spawn, buffer, spawn, buffered, exec, execute, path_ext |

## Links

- npm: https://www.npmjs.com/package/cmd-spawn
- Repository: https://github.com/beckend/cmd-spawn
- Homepage: https://github.com/beckend/cmd-spawn#readme
- Issues: https://github.com/beckend/cmd-spawn/issues
- npm.io page: https://npm.io/package/cmd-spawn

## Dependencies (3)

- [bluebird](https://npm.io/package/bluebird.md) ^3.4.6
- [cross-spawn](https://npm.io/package/cross-spawn.md) ^5.0.1
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.4.0 (latest) — 2016-11-17
- 1.3.0 — 2016-11-15
- 1.2.0 — 2016-11-14
- 1.1.0 — 2016-11-14
- 1.0.0 — 2016-11-14

## README

[![Build Status](https://travis-ci.org/beckend/cmd-spawn.svg?branch=master)](https://travis-ci.org/beckend/cmd-spawn)
[![Coverage Status](https://coveralls.io/repos/github/beckend/cmd-spawn/badge.svg?branch=master)](https://coveralls.io/github/beckend/cmd-spawn?branch=master)
[![npm](https://img.shields.io/npm/v/cmd-spawn.svg??maxAge=2592000)](https://www.npmjs.com/package/cmd-spawn)

# 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](https://github.com/IndigoUnited/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](https://github.com/IndigoUnited/node-cross-spawn) by default, can be disabled.

## Install

`npm -S i cmd-spawn`


# Usage

### unbuffered(normal 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');
  }
});
```

### buffered spawn
```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');
  });
```

### More usage examples
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<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
```js
{
  // 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.

---
_Source: https://npm.io/package/cmd-spawn · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
