Skip to content

Commit

Permalink
fix: clean up TS types, README, keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Dec 11, 2019
1 parent 368d17a commit aa0b822
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
41 changes: 29 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@ make it promise-friendly.
npm install --save promisify-child-process
```

If you are using a old version of Node without build-in `Promise`s or
`Object.create`, you will need to use polyfills.
If you are using a old version of Node without built-in `Promise`s or
`Object.create`, you will need to use polyfills (e.g. `@babel/polyfill`).

```js
// OLD:
const { exec, spawn, fork, execFile } = require('child_process')
// NEW:
const { exec, spawn, fork, execFile } = require('promisify-child-process')
```

## Upgrading to v3

You must now pass `maxBuffer` or `encoding` to `spawn`/`fork` if you want to
capture `stdout` or `stderr`.

## Warning: capturing output
## Resolution/Rejection

The child process promise will only resolve if the process exits with a code of 0.
If it exits with any other code, is killed by a signal, or emits an `'error'` event,
the promise will reject.

## Capturing output

`exec` and `execFile` capture `stdout` and `stderr` by default. But `spawn` and
`fork` don't capture `stdout` and `stderr` unless you pass an `encoding` or
Expand All @@ -50,24 +63,28 @@ async function() {
}
```

## Usage
## Additional properties on rejection errors

```js
// OLD:
const { exec, spawn, fork, execFile } = require('child_process')
// NEW:
const { exec, spawn, fork, execFile } = require('promisify-child-process')
```
If the child process promise rejects, the error may have the following additional
properties:

- `code` - the process' exit code (if it exited)
- `signal` - the signal the process was killed with (if it was killed)
- `stdout` - the captured `stdout` (if output capturing was enabled)
- `stderr` - the captured `stderr` (if output capturing was enabled)

## Wrapper

If for any reason you need to wrap a `ChildProcess` you didn't create,
you can use the exported `promisifyChildProcess` function:

```js
const {promisifyChildProcess} = require('promisify-child-process');
const { promisifyChildProcess } = require('promisify-child-process');

async function() {
const { stdout, stderr } = await promisifyChildProcess(
some3rdPartyFunctionThatReturnsChildProcess()
some3rdPartyFunctionThatReturnsChildProcess(),
{ encoding: 'utf8' }
)
}
```
Expand Down
36 changes: 18 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,66 @@ import {
ExecOptions,
ExecFileOptions,
ForkOptions,
} from 'child_process';
} from 'child_process'

interface Output {
stdout?: string | Buffer | null | undefined;
stderr?: string | Buffer | null | undefined;
stdout?: string | Buffer | null | undefined
stderr?: string | Buffer | null | undefined
}

interface ExitReason {
code?: number;
signal?: string;
code?: number
signal?: string
}

type ErrorWithOutput = Error & Output & ExitReason;
type ErrorWithOutput = Error & Output & ExitReason

type ChildProcessPromise = ChildProcess & Promise<Output>;
type ChildProcessPromise = ChildProcess & Promise<Output>

interface PromisifyChildProcessOptions {
encoding?: string;
encoding?: string
}

type PromisifySpawnOptions = SpawnOptions & PromisifyChildProcessOptions;
type PromisifySpawnOptions = SpawnOptions & PromisifyChildProcessOptions

type PromisifyForkOptions = ForkOptions & PromisifyChildProcessOptions;
type PromisifyForkOptions = ForkOptions & PromisifyChildProcessOptions

export function promisifyChildProcess(
child: ChildProcess,
options?: PromisifyChildProcessOptions
): ChildProcessPromise;
): ChildProcessPromise

export function spawn(
command: string,
args: Array<string>,
options?: PromisifySpawnOptions
): ChildProcessPromise;
): ChildProcessPromise
export function spawn(
command: string,
options?: PromisifySpawnOptions
): ChildProcessPromise;
): ChildProcessPromise

export function fork(
module: string,
args: Array<string>,
options?: PromisifyForkOptions
): ChildProcessPromise;
): ChildProcessPromise
export function fork(
module: string,
options?: PromisifyForkOptions
): ChildProcessPromise;
): ChildProcessPromise

export function exec(
command: string,
options?: ExecOptions
): ChildProcessPromise;
): ChildProcessPromise

export function execFile(
file: string,
args: Array<string>,
options?: ExecFileOptions
): ChildProcessPromise;
): ChildProcessPromise
export function execFile(
file: string,
options?: ExecFileOptions
): ChildProcessPromise;
): ChildProcessPromise
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@
"url": "https://github.com/jcoreio/promisify-child-process.git"
},
"keywords": [
"es2015"
"child",
"process",
"child_process",
"exec",
"spawn",
"execFile",
"fork",
"promise",
"promises",
"async",
"await",
"thenable"
],
"author": "Andy Edwards",
"license": "MIT",
Expand All @@ -85,6 +96,7 @@
"@jedwards1211/commitlint-config": "^1.0.0",
"@jedwards1211/eslint-config": "^2.0.0",
"@jedwards1211/eslint-config-flow": "^1.0.2",
"@types/node": "^12.12.17",
"babel-eslint": "^10.0.1",
"babel-plugin-istanbul": "^5.1.0",
"chai": "^4.2.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,11 @@
dependencies:
defer-to-connect "^1.0.1"

"@types/node@^12.12.17":
version "12.12.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.17.tgz#191b71e7f4c325ee0fb23bc4a996477d92b8c39b"
integrity sha512-Is+l3mcHvs47sKy+afn2O1rV4ldZFU7W8101cNlOd+MRbjM4Onida8jSZnJdTe/0Pcf25g9BNIUsuugmE6puHA==

JSONStream@^1.0.4, JSONStream@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down

0 comments on commit aa0b822

Please sign in to comment.