spawnCommand
![npm version](https://badge.fury.io/js/spawncommand.svg)
SpawnCommand will run the spawn
or fork
methods from the child_process
module, and add a promise
property to the returned process instance. The promise will be resolved on process exit with an object consisting of code
, stdout
and stderr
properties.
yarn add -E spawncommand
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/0.svg?sanitize=true)
Table Of Contents
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/1.svg?sanitize=true)
API
SpawnCommand can be used by importing the default spawn
and named fork
exports.
import spawn, { fork } from 'spawncommand'
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/2.svg?sanitize=true)
Types
The package's main type is ChildProcessWithPromise which enriches the standard ChildProcess with a promise
property.
import('child_process').ChildProcess
ChildProcess
ChildProcess
ChildProcessWithPromise
: A child process with an extra promise
property.
Name | Type | Description | Default |
---|
promise* | Promise.<PromiseResult> | A promise resolved when the process exits. | - |
PromiseResult
Name | Type | Description | Default |
---|
stdout* | string | The accumulated result of the stdout stream. | - |
stderr* | string | The accumulated result of the stderr stream. | - |
code* | number | The code with which the process exited. | - |
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/3.svg?sanitize=true)
spawn(
module: string,
args: string[],
options?: SpawnOptions,
): ChildProcessWithPromise
Spawns a command and returns a ChildProcess instance with the promise
property resolved on exit. The promise will be rejected if an error was encountered when trying to spawn the process.
import('child_process').SpawnOptions
SpawnOptions
import spawn from 'spawncommand'
(async () => {
const { promise } = spawn('echo', ['hello world'])
const { stderr, stdout, code } = await promise
console.log(JSON.stringify({
stderr, stdout, code,
}, null, 2))
})()
{
"stderr": "",
"stdout": "hello world\n",
"code": 0
}
The returned object is a ChildProcess
and all of its properties can be accessed in the standard way.
import spawnCommand from 'spawncommand'
(async () => {
const { stdout, promise } = spawnCommand('echo', ['hello world'])
stdout.pipe(process.stdout)
await promise
})()
hello world
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/4.svg?sanitize=true)
fork(
module: string,
args: string[],
options?: ForkOptions,
): ChildProcessWithPromise
Forks a Node.js module and adds a promise
property to the returned ChildProcess.
import { fork } from 'spawncommand'
(async () => {
const { promise } = fork('example/index.js', ['example/spawn.js'], {
stdio: 'pipe',
})
const { stdout } = await promise
console.log(stdout)
})()
{
"stderr": "",
"stdout": "hello world\n",
"code": 0
}
The pipe
option needs to be set in order to gather the output of the stderr
and stdout
streams (or an array for older versions of Node.js when this does not work).
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/5.svg?sanitize=true)
Copyright
(c) Art Deco 2018
![](https://github.com/artdecocode/spawncommand/raw/HEAD/.documentary/section-breaks/-1.svg?sanitize=true)