Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@npmcli/promise-spawn

npm26.3mISC8.0.2TypeScript support: definitely-typed

spawn processes the way the npm cli likes to do

readme

@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: true, // 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 true. 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.
  • shell Boolean or String. If false, no shell is used during spawn. If true, the system default shell is used. If a String, that specific shell is used. When a shell is used, the given command runs from within that shell by concatenating the command and its escaped arguments and running the result. This option is not passed through to child_process.spawn.
  • Any other options for child_process.spawn can be passed as well.

promiseSpawn.open(arg, opts, extra) -> Promise

Use the operating system to open arg with a default program. This is useful for things like opening the user's default browser to a specific URL.

Depending on the platform in use this will use start (win32), open (darwin) or xdg-open (everything else). In the case of Windows Subsystem for Linux we use the default win32 behavior as it is much more predictable to open the arg using the host operating system.

Options

Options are identical to promiseSpawn except for the following:

  • command String, the command to use to open the file in question. Default is one of start, open or xdg-open depending on platform in use.

changelog

Changelog

8.0.2 (2024-10-18)

Bug Fixes

  • 5ecf301 #128 open URL in browser on WSL (#128) (@mbtools, @wraithgar)

    Chores

  • ef4ba09 #127 bump @npmcli/template-oss from 4.23.3 to 4.23.4 (#127) (@dependabot[bot], @npm-cli-bot)

8.0.1 (2024-10-02)

Dependencies

8.0.0 (2024-09-03)

⚠️ BREAKING CHANGES

  • @npmcli/promise-spawn now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • eeaf662 #122 align to npm 10 node engine range (@hashtagchris)

    Chores

  • 392dc76 #122 run template-oss-apply (@hashtagchris)
  • 9ed7cbe #120 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 63cc0e6 #121 postinstall for dependabot template-oss PR (@hashtagchris)
  • a784586 #121 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

7.0.2 (2024-05-04)

Bug Fixes

  • 4912015 #102 reject with error from parent context on close (#102) (@lukekarrys)

Chores

  • 09872d7 #105 linting: no-unused-vars (@lukekarrys)
  • 70f0eb7 #105 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • 82ae2a7 #105 postinstall for dependabot template-oss PR (@lukekarrys)
  • 2855879 #104 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

7.0.1 (2023-12-21)

Bug Fixes

  • 46fad5a #98 parse options.env more similarly to process.env (#98) (@thecodrr)

Chores

  • d3ba687 #97 postinstall for dependabot template-oss PR (@lukekarrys)
  • cf18492 #97 bump @npmcli/template-oss from 4.21.1 to 4.21.3 (@dependabot[bot])
  • c72524e #95 postinstall for dependabot template-oss PR (@lukekarrys)
  • 8102197 #95 bump @npmcli/template-oss from 4.19.0 to 4.21.1 (@dependabot[bot])
  • 3d54f38 #76 postinstall for dependabot template-oss PR (@lukekarrys)
  • ca63a18 #76 bump @npmcli/template-oss from 4.18.1 to 4.19.0 (@dependabot[bot])
  • e3e359f #74 postinstall for dependabot template-oss PR (@lukekarrys)
  • cc8e9c9 #74 bump @npmcli/template-oss from 4.18.0 to 4.18.1 (@dependabot[bot])

7.0.0 (2023-08-30)

⚠️ BREAKING CHANGES

  • support for node 14 has been removed

Bug Fixes

Dependencies

6.0.2 (2022-12-12)

Bug Fixes

  • 38f272a #56 correctly identify more wsl distributions, closes npm/cli#5903 (#56) (@nlf)

6.0.1 (2022-11-01)

Dependencies

6.0.0 (2022-11-01)

⚠️ BREAKING CHANGES

  • stdout and stderr will now be returned as strings by default, for buffers set stdioString to false
  • when the shell option is set provided arguments will automatically be escaped

Features

  • 6ab90b8 #48 switch stdioString default to true (#48) (@nlf)
  • a854057 #47 add open method for using system default apps to open arguments (#47) (@nlf)
  • 723fc32 #44 implement argument escaping when the shell option is set (@nlf)

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)

4.0.0 (2022-10-10)

⚠️ BREAKING CHANGES

  • @npmcli/promise-spawn is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • 4fba970 #29 postinstall for dependabot template-oss PR (@lukekarrys)

3.0.0 (2022-04-05)

⚠ BREAKING CHANGES

  • this will drop support for node 10 and non-LTS versions of node 12 and node 14

Bug Fixes

Dependencies