tinyspawn

tinyspawn is a minimalistic child_process
wrapper with following features:
- Small (~50 LOC, 600 bytes).
- Focus on performance.
- Zero dependencies.
- Meaningful errors.
- Easy to extend.
Install
$ npm install tinyspawn --save
Usage
Basic
It's recommended to bind tinyspawn to $
:
const $ = require('tinyspawn')
After that, pass the command (with arguments) to be executed as first argument:
const { stdout } = await $(`node -e 'console.log("hello world")'`)
console.log(stdout)
You can pass any spawn#options as second argument:
const { stdout } = $(`node -e 'console.log("hello world")'`, {
shell: true
})
The output is a ChildProcess instance:
const {
signalCode,
exitCode,
killed,
spawnfile,
spawnargs,
pid,
stdin,
stdout,
stderr,
stdin
} = await $('date')
Piping
Since tinyspawn returns a ChildProcess instance, you can use it for interacting with other Node.js streams:
const subprocess = $('echo 1234567890')
subprocess.stdout.pipe(process.stdout)
await subprocess
JSON parsing
tinyspawn has been designed to work with CLI commands that outputs json.
You can easily parse it calling $.json
or passing { json: true }
as option:
const { stdout } = await $.json(`curl https://geolocation.microlink.io`)
Extending
You can use $.extend
to pass any spawn#options to be used by default:
const $ = require('tinyspawn').extend({ shell: true })
Errors
tinyspawn is oriented to print meanigful errors:
const error = await $(`node -e 'require("notfound")'`).catch(error => error)
console.error(error)
The childProcess properties are also available as part of the error:
const { stdout: node } = await $('which node')
const error = await $(`${node} -e 'require("notfound")'`).catch(error => error)
const {
signalCode,
exitCode,
killed,
spawnfile,
spawnargs,
pid,
stdin,
stdout,
stderr,
stdin
} = error
License
tinyspawn © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.
kikobeats.com · GitHub Kiko Beats · Twitter @kikobeats