spawn2 v1.1.3
spawn2
const { communicate, execute, spawn, wait } = require('spawn2');
Like the NodeJS child_process
module, this module provides the ability to spawn children processes.
In addition to a spawn()
function very similar to the NodeJS one, it also provides an higher level interface for more common tasks.
Main differences with child_process
are:
- The IPC interface (
subprocess.send
andprocess.on("message")
) are not provided - Higer level API, based on promises
exitCode
andsignalCode
are exposed. Withchild_process
, if.on("exit")
is called after the process has exited, the callback will never be called. ExposingexitCode
andsignalCode
escapes this pitfall.- More convenient stdio management in common cases (reading or writing
Buffer
/string
) - More convenient
exitCode
/signalCode
in common cases (throws an error in case of a non-zero exit code/non-null signal code) - Allows you to use child processes as stream
Transform
(for streampipe
/pipeline
) (see:makePipe
)
class ChildProcessError
Thrown by wait()
, communicate()
and execute()
when:
- the exit code is not zero, and
checkExitCode
istrue
, or - the status code is not null, and
checkStatusCode
istrue
interface SpawnOptions
Options when spawning a new processes. They are the same as child_process
, but with some additions.
interface ChildProcess
Similar to child_process.ChildProcess
, it is returned by spawn()
on success.
function spawn
function spawn(command: string|string[], options?: SpawnOptions): ChildProcess
Spawn a new process. The executable is command[0]
, the rest of the array is used to provide arguments.
If command
is a string, it will be assumed to be a shell command to be interpreted by /bin/sh
.
Example:
spawn("cat", "/etc/fstab")
function wait
function wait(cp: ChildProcess): Promise\<{ exitCode: number | null, signalCode: string | null }>
Wait for the process to terminate, and returns the exit code and signal code.
Note that this may throws an error instead of returning if checkExitCode
or checkStatusCode
is true.
function communicate
function communicate(cp: ChildProcess, stdin?: intoStream.Input | RedableStream): Promise\<{ stdout: Buffer | string | null, stderr: Buffer | string | null, exitCode: number | null, signalCode: string | null }>
Sends stdin to a running process, reads stdout and stderr and waits for the process to terminate.
The return type of stdout and stderr is determined by the encoding
option.
interface ExecuteOptions
Additional options for execute()
function execute
function execute(command: string | string[], options: ExecuteOptions): Promise\<{ stdout: Buffer | string | null, stderr: Buffer | string | null, exitCode: number | null, signalCode: string | null }>
Shorthand for execute(spawn(command, options), options.stdin)
. Closes spawned stdin if not provided so it will not wait on non-existent input.
function makePipe
function makePipe(cp: ChildProcess): NodeJS.ReadWriteStream
Create a ReadWriteStream (suitable to be used by ReadableStream.pipe()
) from child process's
stdin and stdout.
The checkExitCode and checkSignalsCode will be used to control wether the "error" event is triggered on the stream on bad exit/signal status.