1.1.3 • Published 4 years ago

spawn2 v1.1.3

Weekly downloads
62
License
ISC
Repository
github
Last release
4 years ago

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 and process.on("message")) are not provided
  • Higer level API, based on promises
  • exitCode and signalCode are exposed. With child_process, if .on("exit") is called after the process has exited, the callback will never be called. Exposing exitCode and signalCode 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 stream pipe/pipeline) (see: makePipe)

class ChildProcessError

Thrown by wait(), communicate() and execute() when:

  • the exit code is not zero, and checkExitCode is true, or
  • the status code is not null, and checkStatusCode is true

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.

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago