New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tinyspawn

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinyspawn

minimal wrapping around Node.js child_process

  • 0.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25K
decreased by-26.07%
Maintainers
1
Weekly downloads
 
Created
Source

tinyspawn

Last version Coverage Status NPM Status

*tinyspawn is a minimalistic child_process wrapper with following features:

  • Small (~50 lines of code).
  • 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 execute as first argument:

const { stdout: node }  = await $('which node')
const { stdout } = await $(`${node} -e 'console.log("hello world")'`)
console.log(stdout) // => 'hello world'

Additionally, you can pass spawn#options as second argument:

const { stdout } = $(`${node} -e 'console.log("hello world")'`, { shell: true })

Any of the ChildProcess instance properties are available as part of the output:

const {
  signalCode,
  exitCode,
  killed,
  spawnfile,
  spawnargs,
  pid,
  stdin,
  stdout,
  stderr,
  stdin
} = await $('date')

JSON parsing

tinyspawn has been designed to work with CLI commands that outputs json.

You can easily parse it calling $.json:

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 })

Errors

tinyspawn is oriented to print meanigful errors:

const { stdout: node } = await $('which node')

const error = await $(`${node} -e 'require("notfound")'`).catch(error => error)

console.error(error)
// The command spawned as:

//   /Users/kikobeats/.n/bin/node -e 'require("notfound")'

// failed with code 1:

//   node:internal/modules/cjs/loader:1147
//     throw err;
//     ^

//   Error: Cannot find module 'notfound'
//   Require stack:
//   - /Users/kikobeats/Downloads/tinyspawn/[eval]
//       at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
//       at Module._load (node:internal/modules/cjs/loader:985:27)
//       at Module.require (node:internal/modules/cjs/loader:1235:19)
//       at require (node:internal/modules/helpers:176:18)
//       at [eval]:1:1
//       at runScriptInThisContext (node:internal/vm:144:10)
//       at node:internal/process/execution:109:14
//       at [eval]-wrapper:6:24
//       at runScript (node:internal/process/execution:92:62)
//       at evalScript (node:internal/process/execution:123:10) {
//     code: 'MODULE_NOT_FOUND',
//     requireStack: [ '/Users/kikobeats/Downloads/tinyspawn/[eval]' ]
//   }

//   Node.js v20.10.0

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

FAQs

Package last updated on 12 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc