You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # execa [![Build Status: Linux](https://travis-ci.org/sindresorhus/execa.svg?branch=master)](https://travis-ci.org/sindresorhus/execa) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/x5ajamxtjtt93cqv/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/execa/branch/master) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/execa/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/execa?branch=master)
  2. > A better [`child_process`](https://nodejs.org/api/child_process.html)
  3. ## Why
  4. - Promise interface.
  5. - [Strips EOF](https://github.com/sindresorhus/strip-eof) from the output so you don't have to `stdout.trim()`.
  6. - Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.
  7. - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)
  8. - Higher max buffer. 10 MB instead of 200 KB.
  9. - [Executes locally installed binaries by name.](#preferlocal)
  10. - [Cleans up spawned processes when the parent process dies.](#cleanup)
  11. ## Install
  12. ```
  13. $ npm install --save execa
  14. ```
  15. ## Usage
  16. ```js
  17. const execa = require('execa');
  18. execa('echo', ['unicorns']).then(result => {
  19. console.log(result.stdout);
  20. //=> 'unicorns'
  21. });
  22. // pipe the child process stdout to the current stdout
  23. execa('echo', ['unicorns']).stdout.pipe(process.stdout);
  24. execa.shell('echo unicorns').then(result => {
  25. console.log(result.stdout);
  26. //=> 'unicorns'
  27. });
  28. // example of catching an error
  29. execa.shell('exit 3').catch(error => {
  30. console.log(error);
  31. /*
  32. {
  33. message: 'Command failed: /bin/sh -c exit 3'
  34. killed: false,
  35. code: 3,
  36. signal: null,
  37. cmd: '/bin/sh -c exit 3',
  38. stdout: '',
  39. stderr: '',
  40. timedOut: false
  41. }
  42. */
  43. });
  44. ```
  45. ## API
  46. ### execa(file, [arguments], [options])
  47. Execute a file.
  48. Think of this as a mix of `child_process.execFile` and `child_process.spawn`.
  49. Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.
  50. ### execa.stdout(file, [arguments], [options])
  51. Same as `execa()`, but returns only `stdout`.
  52. ### execa.stderr(file, [arguments], [options])
  53. Same as `execa()`, but returns only `stderr`.
  54. ### execa.shell(command, [options])
  55. Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.
  56. Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).
  57. The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.
  58. ### execa.sync(file, [arguments], [options])
  59. Execute a file synchronously.
  60. Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
  61. This method throws an `Error` if the command fails.
  62. ### execa.shellSync(file, [options])
  63. Execute a command synchronously through the system shell.
  64. Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
  65. ### options
  66. Type: `Object`
  67. #### cwd
  68. Type: `string`<br>
  69. Default: `process.cwd()`
  70. Current working directory of the child process.
  71. #### env
  72. Type: `Object`<br>
  73. Default: `process.env`
  74. Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.
  75. #### extendEnv
  76. Type: `boolean`<br>
  77. Default: `true`
  78. Set to `false` if you don't want to extend the environment variables when providing the `env` property.
  79. #### argv0
  80. Type: `string`
  81. Explicitly set the value of `argv[0]` sent to the child process. This will be set to `command` or `file` if not specified.
  82. #### stdio
  83. Type: `Array` `string`<br>
  84. Default: `pipe`
  85. Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
  86. #### detached
  87. Type: `boolean`
  88. Prepare child to run independently of its parent process. Specific behavior [depends on the platform](https://nodejs.org/api/child_process.html#child_process_options_detached).
  89. #### uid
  90. Type: `number`
  91. Sets the user identity of the process.
  92. #### gid
  93. Type: `number`
  94. Sets the group identity of the process.
  95. #### shell
  96. Type: `boolean` `string`<br>
  97. Default: `false`
  98. If `true`, runs `command` inside of a shell. Uses `/bin/sh` on UNIX and `cmd.exe` on Windows. A different shell can be specified as a string. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
  99. #### stripEof
  100. Type: `boolean`<br>
  101. Default: `true`
  102. [Strip EOF](https://github.com/sindresorhus/strip-eof) (last newline) from the output.
  103. #### preferLocal
  104. Type: `boolean`<br>
  105. Default: `true`
  106. Prefer locally installed binaries when looking for a binary to execute.<br>
  107. If you `$ npm install foo`, you can then `execa('foo')`.
  108. #### localDir
  109. Type: `string`<br>
  110. Default: `process.cwd()`
  111. Preferred path to find locally installed binaries in (use with `preferLocal`).
  112. #### input
  113. Type: `string` `Buffer` `stream.Readable`
  114. Write some input to the `stdin` of your binary.<br>
  115. Streams are not allowed when using the synchronous methods.
  116. #### reject
  117. Type: `boolean`<br>
  118. Default: `true`
  119. Setting this to `false` resolves the promise with the error instead of rejecting it.
  120. #### cleanup
  121. Type: `boolean`<br>
  122. Default: `true`
  123. Keep track of the spawned process and `kill` it when the parent process exits.
  124. #### encoding
  125. Type: `string`<br>
  126. Default: `utf8`
  127. Specify the character encoding used to decode the `stdout` and `stderr` output.
  128. #### timeout
  129. Type: `number`<br>
  130. Default: `0`
  131. If timeout is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than timeout milliseconds.
  132. #### maxBuffer
  133. Type: `number`<br>
  134. Default: `10000000` (10MB)
  135. Largest amount of data in bytes allowed on `stdout` or `stderr`.
  136. #### killSignal
  137. Type: `string` `number`<br>
  138. Default: `SIGTERM`
  139. Signal value to be used when the spawned process will be killed.
  140. #### stdin
  141. Type: `string` `number` `Stream` `undefined` `null`<br>
  142. Default: `pipe`
  143. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  144. #### stdout
  145. Type: `string` `number` `Stream` `undefined` `null`<br>
  146. Default: `pipe`
  147. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  148. #### stderr
  149. Type: `string` `number` `Stream` `undefined` `null`<br>
  150. Default: `pipe`
  151. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  152. ## Tips
  153. ### Save and pipe output from a child process
  154. Let's say you want to show the output of a child process in real-time while also saving it to a variable.
  155. ```js
  156. const execa = require('execa');
  157. const getStream = require('get-stream');
  158. const stream = execa('echo', ['foo']).stdout;
  159. stream.pipe(process.stdout);
  160. getStream(stream).then(value => {
  161. console.log('child output:', value);
  162. });
  163. ```
  164. ## License
  165. MIT © [Sindre Sorhus](https://sindresorhus.com)