npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spawncommand

v2.2.0

Published

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

Downloads

519

Readme

spawnCommand

npm version

SpawnCommand will run the spawn or fork methods from the child_process module, and add a promise property to the returned process instance. The promise will be resolved on process exit with an object consisting of code, stdout and stderr properties.

yarn add spawncommand

Table Of Contents

API

SpawnCommand can be used by importing the default spawn and named fork exports.

import spawn, { fork } from 'spawncommand'

Types

The package's main type is ChildProcessWithPromise which enriches the standard ChildProcess with a promise property.

import('child_process').ChildProcess child_process.ChildProcess

_spawncommand.ChildProcessWithPromise: A child process with an extra promise property.

| Name | Type | Description | | ----------------- | ----------------------------------------------------- | ------------------------------------------ | | promise* | !Promise<!_spawncommand.PromiseResult> | A promise resolved when the process exits. | | spawnCommand* | string | The spawn arguments joined by whitespace. |

_spawncommand.PromiseResult

| Name | Type | Description | | ----------- | --------------- | ---------------------------------------------- | | stdout* | string | The accumulated result of the stdout stream. | | stderr* | string | The accumulated result of the stderr stream. | | code* | number | The code with which the process exited. |

spawn(  module: string,  args: string[],  options?: SpawnOptions,): ChildProcessWithPromise

Spawns a command and returns a ChildProcess instance with the promise property resolved on exit. The promise will be rejected if an error was encountered when trying to spawn the process.

import('child_process').SpawnOptions child_process.SpawnOptions

import spawn from 'spawncommand'

(async () => {
  const { promise } = spawn('echo', ['hello world'])
  const { stderr, stdout, code } =  await promise
  console.log(JSON.stringify({
    stderr, stdout, code,
  }, null, 2))
})()
{
  "stderr": "",
  "stdout": "hello world\n",
  "code": 0
}

The returned object is a ChildProcess and all of its properties can be accessed in the standard way.

import spawnCommand from 'spawncommand'

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

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

fork(  module: string,  args: string[],  options?: ForkOptions,): ChildProcessWithPromise

Forks a Node.js module and adds a promise property to the returned ChildProcess.

import('child_process').ForkOptions child_process.ForkOptions

import { fork } from 'spawncommand'

(async () => {
  const { promise } = fork('node_modules/.bin/alanode',
    ['example/spawn.js'], {
      stdio: 'pipe',
    })
  const { stdout } =  await promise
  console.log(stdout)
})()
{
  "stderr": "",
  "stdout": "hello world\n",
  "code": 0
}

The pipe option needs to be set in order to gather the output of the stderr and stdout streams (or an array for older versions of Node.js when this does not work).

Copyright