Socket
Socket
Sign inDemoInstall

@npmcli/promise-spawn

Package Overview
Dependencies
0
Maintainers
5
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @npmcli/promise-spawn

spawn processes the way the npm cli likes to do


Version published
Weekly downloads
6M
decreased by-21.97%
Maintainers
5
Created
Weekly downloads
 

Package description

What is @npmcli/promise-spawn?

The @npmcli/promise-spawn package is designed to execute shell commands or scripts with a promise-based interface. It simplifies working with child processes in Node.js by providing a straightforward way to spawn processes and handle their output, errors, and exit codes asynchronously. This package is particularly useful for building Node.js applications that need to interact with the system's shell or execute external commands as part of their operation.

What are @npmcli/promise-spawn's main functionalities?

Executing a simple command

This feature allows you to execute a simple command (in this case, 'echo') and print its output, error output, and exit code. The 'stdioString' option is used to treat the output and error as strings.

const promiseSpawn = require('@npmcli/promise-spawn');

async function runCommand() {
  const { stdout, stderr, code } = await promiseSpawn('echo', ['Hello, world!'], { stdioString: true });
  console.log(`Output: ${stdout}`);
  console.error(`Error: ${stderr}`);
  console.log(`Exit code: ${code}`);
}

runCommand();

Executing a command with error handling

This feature demonstrates executing a command that is expected to fail (attempting to list a nonexistent directory) and handling the error gracefully. The catch block captures the error, allowing the application to respond appropriately.

const promiseSpawn = require('@npmcli/promise-spawn');

async function runCommandWithErrorHandling() {
  try {
    const { stdout } = await promiseSpawn('ls', ['-l', '/nonexistent'], { stdioString: true });
    console.log(`Output: ${stdout}`);
  } catch (err) {
    console.error(`Error: ${err.message}`);
  }
}

runCommandWithErrorHandling();

Other packages similar to @npmcli/promise-spawn

Changelog

Source

5.0.0 (2022-10-26)

⚠️ BREAKING CHANGES

  • leading and trailing whitespace is no longer preserved when stdioStrings is set
  • this module no longer attempts to infer a uid and gid for processes

Features

  • 422e1b6 #40 remove infer-owner (#40) (@nlf, @wraithgar)

Bug Fixes

  • 0f3dc07 #42 trim stdio strings before returning when stdioStrings is set (#42) (@nlf)

Readme

Source

@npmcli/promise-spawn

Spawn processes the way the npm cli likes to do. Give it some options, it'll give you a Promise that resolves or rejects based on the results of the execution.

USAGE

const promiseSpawn = require('@npmcli/promise-spawn')

promiseSpawn('ls', [ '-laF', 'some/dir/*.js' ], {
  cwd: '/tmp/some/path', // defaults to process.cwd()
  stdioString: false, // stdout/stderr as strings rather than buffers
  stdio: 'pipe', // any node spawn stdio arg is valid here
  // any other arguments to node child_process.spawn can go here as well,
}, {
  extra: 'things',
  to: 'decorate',
  the: 'result',
}).then(result => {
  // {code === 0, signal === null, stdout, stderr, and all the extras}
  console.log('ok!', result)
}).catch(er => {
  // er has all the same properties as the result, set appropriately
  console.error('failed!', er)
})

API

promiseSpawn(cmd, args, opts, extra) -> Promise

Run the command, return a Promise that resolves/rejects based on the process result.

Result or error will be decorated with the properties in the extra object. You can use this to attach some helpful info about why the command is being run, if it makes sense for your use case.

If stdio is set to anything other than 'inherit', then the result/error will be decorated with stdout and stderr values. If stdioString is set to true, these will be strings. Otherwise they will be Buffer objects.

Returned promise is decorated with the stdin stream if the process is set to pipe from stdin. Writing to this stream writes to the stdin of the spawned process.

Options
  • stdioString Boolean, default false. Return stdout/stderr output as strings rather than buffers.
  • cwd String, default process.cwd(). Current working directory for running the script. Also the argument to infer-owner to determine effective uid/gid when run as root on Unix systems.
  • Any other options for child_process.spawn can be passed as well.

FAQs

Last updated on 26 Oct 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc