1.7.2 • Published 2 years ago

@expo/spawn-async v1.7.2

Weekly downloads
127,292
License
MIT
Repository
github
Last release
2 years ago

spawn-async Tests

A cross-platform version of Node's child_process.spawn as an async function that returns a promise. Supports Node 12 LTS and up.

Usage:

import spawnAsync from '@expo/spawn-async';

(async function () {
  let resultPromise = spawnAsync('echo', ['hello', 'world']);
  let spawnedChildProcess = resultPromise.child;
  try {
    let {
      pid,
      output: [stdout, stderr],
      stdout,
      stderr,
      status,
      signal,
    } = await resultPromise;
  } catch (e) {
    console.error(e.stack);
    // The error object also has the same properties as the result object
  }
})();

API

spawnAsync takes the same arguments as child_process.spawn. Its options are the same as those of child_process.spawn plus:

  • ignoreStdio: whether to ignore waiting for the child process's stdio streams to close before resolving the result promise. When ignoring stdio, the returned values for stdout and stderr will be empty strings. The default value of this option is false.

It returns a promise whose result is an object with these properties:

  • pid: the process ID of the spawned child process
  • output: an array with stdout and stderr's output
  • stdout: a string of what the child process wrote to stdout
  • stderr: a string of what the child process wrote to stderr
  • status: the exit code of the child process
  • signal: the signal (ex: SIGTERM) used to stop the child process if it did not exit on its own

If there's an error running the child process or it exits with a non-zero status code, spawnAsync rejects the returned promise. The Error object also has the properties listed above.

Accessing the child process

Sometimes you may want to access the child process object--for example, if you wanted to attach event handlers to stdio or stderr and process data as it is available instead of waiting for the process to be resolved.

You can do this by accessing .child on the Promise that is returned by spawnAsync.

Here is an example:

(async () => {
  let ffmpeg$ = spawnAsync('ffmpeg', ['-i', 'path/to/source.flac', '-codec:a', 'libmp3lame', '-b:a', '320k', '-ar', '44100', 'path/to/output.mp3']);
  let childProcess = ffmpeg$.child;
  childProcess.stdout.on('data', (data) => {
    console.log(`ffmpeg stdout: ${data}`);
  });
  childProcess.stderr.on('data', (data) => {
    console.error(`ffmpeg stderr: ${data}`);
  });
  let result = await ffmpeg$;
  console.log(`ffmpeg pid ${result.pid} exited with code ${result.code}`);
})();
arno-front-clido-it-yourself@essentialz/ecp-clisentry-expo-mminflint@infinitebrahmanuniverse/nolb-_expo_@everything-registry/sub-chunk-307dsokal-eas-clixdlsakushipprroboto-clisentry-exposentry-expo-updatesmexpo-climmexpo-climendix-front-end-clihexo-renderer-pdfgithub-url-for-filegit-command-helperopswalrusplasmoprisma-enginepatchkit-launcher2-main-package-dev-toolspatch-projectnew-archnpm-preparespawn-node-processturtle-clitroy-package-dev-toolstroy-ts-standard-package-dev-toolstodo2issue@archmagelive/plasmo@ailo/sentry-expo@ajhenry/goto@allboatsrise/sentry-expo@batiq/cli@batiq/generator@bycedric/some-cli-test@abdullahceylan/expo-cli@brandingbrand/code-core@brandingbrand/kernel-corewatchinharrison-expo-cliwatchinharrison-xdl@fobos531/sentry-expo@hover/transforms@leonardodino/appr@lunks/sentry-expo@mpetrunic/sentry-expo@mathieudutour/plasmo@expo/cli@expo/dev-server@expo/fingerprint@expo/image-utils@expo/electron-adapter@exlint.io/inflint@expo/xdl@expo/swiftlint@expo/turtle-spawn@expo/metro-config@expo/steps@expo/osascript@expo/package-manager@joelbot/build@jduarter/expo-cli@dvvolynkin/plasmo@streampot/server@phawk/expo-cli@plasmohq/mystic-box@swmansion/xdl@splitmedialabs/devctl@seekingmini/lrn-cli@seekingmini/lrn-dev-server@dothq/build@customd/deploycreate-saas-startereas-clieas-cli-local-build-pluginelectroy-app-bundles-package-dev-toolselectroy-app-main-package-dev-toolselectroy-app-runtime-bundles-package-dev-toolsexpo-cliexpo-cli-directexpo-codemodexpo-updatesexpexpo-modules-autolinkingexpo-test-runnerethern-cli@teselagen/dbmgr@upsoft/patchkit-launcher2-runtime-bundles-package-dev-tools@upsoft/electroy-app-runtime-bundles-package-dev-tools@upsoft/troy-ts-standard-package-dev-toolsepub-check@yeutech-lab/expo-cliandroid-builder-forkgenerator-go-creategenerator-go-langgenerator-create-goaven-core
1.7.2

2 years ago

1.7.1

2 years ago

1.7.0-rc.0

2 years ago

1.7.0

2 years ago

1.6.0

3 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.0

6 years ago

1.3.0

7 years ago

1.2.8

8 years ago