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

spawncommand

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spawncommand

Spawn a child process with a promise property resolved on exit with stdout, stderr and code

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

spawnCommand

npm version

yarn add -E spawncommand

This package is a wrapper around child_process.spawn methods to set .promise property on the returned ChildProcess instances. The promise will be fulfilled on process exit with an object consisting of code, stdout and stderr properties, where:

  • code is the exit code
  • stdout is all data the process wrote to stdout
  • stderr is all data the process wrote to stderr

The promise will be rejected if an error was encountered when trying to spawn the process.

/* yarn example/spawncommand.js */
import spawnCommand from 'spawncommand'

(async () => {
  const { promise } = spawnCommand('echo', ['hello world'])
  const { stderr, stdout, code } =  await promise

  console.log(stderr) // undefined
  console.log(stdout) // hello world\n
  console.log(code) // 0
})()

Because the returned object is a ChildProcess, all its properties can be accessed.

/* yarn example/pipe.js */
import spawnCommand from 'spawncommand'

(async () => {
  const { stdout, promise } = spawnCommand('echo', ['hello world'])

  stdout.pipe(process.stdout)
  await promise
})()

fork

It is also possible to fork a Node.js process and execute a module in it.

/* yarn example/fork.js */
import { resolve } from 'path'
import { fork } from 'spawncommand'

const MODULE_PATH = resolve(__dirname, 'example/spawn.js')

;(async () => {
  const { promise } = fork('example/spawn.js', [], {
    stdio: 'pipe',
  })
  const { stdout } =  await promise
  console.log(stdout) // same output as example/spawn.js
})()

Make sure to pass pipe option to be able to gather stderr and stdout streams (or an array for versions of Node.js when this does not work).


(c) Art Deco Code 2018

Keywords

FAQs

Package last updated on 14 May 2018

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