UNPKG

2.78 kBMarkdownView Raw
1# spawn-async [![Tests](https://github.com/expo/spawn-async/actions/workflows/main.yml/badge.svg)](https://github.com/expo/spawn-async/actions/workflows/main.yml)
2
3A cross-platform version of Node's `child_process.spawn` as an async function that returns a promise. Supports Node 12 LTS and up.
4
5## Usage:
6```js
7import spawnAsync from '@expo/spawn-async';
8
9(async function () {
10 let resultPromise = spawnAsync('echo', ['hello', 'world']);
11 let spawnedChildProcess = resultPromise.child;
12 try {
13 let {
14 pid,
15 output: [stdout, stderr],
16 stdout,
17 stderr,
18 status,
19 signal,
20 } = await resultPromise;
21 } catch (e) {
22 console.error(e.stack);
23 // The error object also has the same properties as the result object
24 }
25})();
26```
27
28## API
29
30`spawnAsync` takes the same arguments as [`child_process.spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options). Its options are the same as those of `child_process.spawn` plus:
31
32- `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`.
33
34It returns a promise whose result is an object with these properties:
35
36- `pid`: the process ID of the spawned child process
37- `output`: an array with stdout and stderr's output
38- `stdout`: a string of what the child process wrote to stdout
39- `stderr`: a string of what the child process wrote to stderr
40- `status`: the exit code of the child process
41- `signal`: the signal (ex: `SIGTERM`) used to stop the child process if it did not exit on its own
42
43If 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.
44
45### Accessing the child process
46
47Sometimes 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.
48
49You can do this by accessing `.child` on the Promise that is returned by `spawnAsync`.
50
51Here is an example:
52```js
53(async () => {
54 let ffmpeg$ = spawnAsync('ffmpeg', ['-i', 'path/to/source.flac', '-codec:a', 'libmp3lame', '-b:a', '320k', '-ar', '44100', 'path/to/output.mp3']);
55 let childProcess = ffmpeg$.child;
56 childProcess.stdout.on('data', (data) => {
57 console.log(`ffmpeg stdout: ${data}`);
58 });
59 childProcess.stderr.on('data', (data) => {
60 console.error(`ffmpeg stderr: ${data}`);
61 });
62 let result = await ffmpeg$;
63 console.log(`ffmpeg pid ${result.pid} exited with code ${result.code}`);
64})();
65
66```