Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <img src="media/logo.svg" width="400">
  2. <br>
  3. [![Coverage Status](https://codecov.io/gh/sindresorhus/execa/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/execa)
  4. > Process execution for humans
  5. ## Why
  6. This package improves [`child_process`](https://nodejs.org/api/child_process.html) methods with:
  7. - Promise interface.
  8. - [Strips the final newline](#stripfinalnewline) from the output so you don't have to do `stdout.trim()`.
  9. - Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.
  10. - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)
  11. - Higher max buffer. 100 MB instead of 200 KB.
  12. - [Executes locally installed binaries by name.](#preferlocal)
  13. - [Cleans up spawned processes when the parent process dies.](#cleanup)
  14. - [Get interleaved output](#all) from `stdout` and `stderr` similar to what is printed on the terminal. [*(Async only)*](#execasyncfile-arguments-options)
  15. - [Can specify file and arguments as a single string without a shell](#execacommandcommand-options)
  16. - More descriptive errors.
  17. ## Install
  18. ```
  19. $ npm install execa
  20. ```
  21. ## Usage
  22. ```js
  23. const execa = require('execa');
  24. (async () => {
  25. const {stdout} = await execa('echo', ['unicorns']);
  26. console.log(stdout);
  27. //=> 'unicorns'
  28. })();
  29. ```
  30. ### Pipe the child process stdout to the parent
  31. ```js
  32. const execa = require('execa');
  33. execa('echo', ['unicorns']).stdout.pipe(process.stdout);
  34. ```
  35. ### Handling Errors
  36. ```js
  37. const execa = require('execa');
  38. (async () => {
  39. // Catching an error
  40. try {
  41. await execa('unknown', ['command']);
  42. } catch (error) {
  43. console.log(error);
  44. /*
  45. {
  46. message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  47. errno: -2,
  48. code: 'ENOENT',
  49. syscall: 'spawn unknown',
  50. path: 'unknown',
  51. spawnargs: ['command'],
  52. originalMessage: 'spawn unknown ENOENT',
  53. shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  54. command: 'unknown command',
  55. escapedCommand: 'unknown command',
  56. stdout: '',
  57. stderr: '',
  58. all: '',
  59. failed: true,
  60. timedOut: false,
  61. isCanceled: false,
  62. killed: false
  63. }
  64. */
  65. }
  66. })();
  67. ```
  68. ### Cancelling a spawned process
  69. ```js
  70. const execa = require('execa');
  71. (async () => {
  72. const subprocess = execa('node');
  73. setTimeout(() => {
  74. subprocess.cancel();
  75. }, 1000);
  76. try {
  77. await subprocess;
  78. } catch (error) {
  79. console.log(subprocess.killed); // true
  80. console.log(error.isCanceled); // true
  81. }
  82. })()
  83. ```
  84. ### Catching an error with the sync method
  85. ```js
  86. try {
  87. execa.sync('unknown', ['command']);
  88. } catch (error) {
  89. console.log(error);
  90. /*
  91. {
  92. message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
  93. errno: -2,
  94. code: 'ENOENT',
  95. syscall: 'spawnSync unknown',
  96. path: 'unknown',
  97. spawnargs: ['command'],
  98. originalMessage: 'spawnSync unknown ENOENT',
  99. shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
  100. command: 'unknown command',
  101. escapedCommand: 'unknown command',
  102. stdout: '',
  103. stderr: '',
  104. all: '',
  105. failed: true,
  106. timedOut: false,
  107. isCanceled: false,
  108. killed: false
  109. }
  110. */
  111. }
  112. ```
  113. ### Kill a process
  114. Using SIGTERM, and after 2 seconds, kill it with SIGKILL.
  115. ```js
  116. const subprocess = execa('node');
  117. setTimeout(() => {
  118. subprocess.kill('SIGTERM', {
  119. forceKillAfterTimeout: 2000
  120. });
  121. }, 1000);
  122. ```
  123. ## API
  124. ### execa(file, arguments, options?)
  125. Execute a file. Think of this as a mix of [`child_process.execFile()`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback) and [`child_process.spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
  126. No escaping/quoting is needed.
  127. Unless the [`shell`](#shell) option is used, no shell interpreter (Bash, `cmd.exe`, etc.) is used, so shell features such as variables substitution (`echo $PATH`) are not allowed.
  128. Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which:
  129. - is also a `Promise` resolving or rejecting with a [`childProcessResult`](#childProcessResult).
  130. - exposes the following additional methods and properties.
  131. #### kill(signal?, options?)
  132. Same as the original [`child_process#kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal) except: if `signal` is `SIGTERM` (the default value) and the child process is not terminated after 5 seconds, force it by sending `SIGKILL`.
  133. ##### options.forceKillAfterTimeout
  134. Type: `number | false`\
  135. Default: `5000`
  136. Milliseconds to wait for the child process to terminate before sending `SIGKILL`.
  137. Can be disabled with `false`.
  138. #### cancel()
  139. Similar to [`childProcess.kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal). This is preferred when cancelling the child process execution as the error is more descriptive and [`childProcessResult.isCanceled`](#iscanceled) is set to `true`.
  140. #### all
  141. Type: `ReadableStream | undefined`
  142. Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).
  143. This is `undefined` if either:
  144. - the [`all` option](#all-2) is `false` (the default value)
  145. - both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)
  146. ### execa.sync(file, arguments?, options?)
  147. Execute a file synchronously.
  148. Returns or throws a [`childProcessResult`](#childProcessResult).
  149. ### execa.command(command, options?)
  150. Same as [`execa()`](#execafile-arguments-options) except both file and arguments are specified in a single `command` string. For example, `execa('echo', ['unicorns'])` is the same as `execa.command('echo unicorns')`.
  151. If the file or an argument contains spaces, they must be escaped with backslashes. This matters especially if `command` is not a constant but a variable, for example with `__dirname` or `process.cwd()`. Except for spaces, no escaping/quoting is needed.
  152. The [`shell` option](#shell) must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.
  153. ### execa.commandSync(command, options?)
  154. Same as [`execa.command()`](#execacommand-command-options) but synchronous.
  155. Returns or throws a [`childProcessResult`](#childProcessResult).
  156. ### execa.node(scriptPath, arguments?, options?)
  157. Execute a Node.js script as a child process.
  158. Same as `execa('node', [scriptPath, ...arguments], options)` except (like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)):
  159. - the current Node version and options are used. This can be overridden using the [`nodePath`](#nodepath-for-node-only) and [`nodeOptions`](#nodeoptions-for-node-only) options.
  160. - the [`shell`](#shell) option cannot be used
  161. - an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio)
  162. ### childProcessResult
  163. Type: `object`
  164. Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.
  165. The child process [fails](#failed) when:
  166. - its [exit code](#exitcode) is not `0`
  167. - it was [killed](#killed) with a [signal](#signal)
  168. - [timing out](#timedout)
  169. - [being canceled](#iscanceled)
  170. - there's not enough memory or there are already too many child processes
  171. #### command
  172. Type: `string`
  173. The file and arguments that were run, for logging purposes.
  174. This is not escaped and should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execa.command()`](#execacommandcommand-options).
  175. #### escapedCommand
  176. Type: `string`
  177. Same as [`command`](#command) but escaped.
  178. This is meant to be copy and pasted into a shell, for debugging purposes.
  179. Since the escaping is fairly basic, this should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execa.command()`](#execacommandcommand-options).
  180. #### exitCode
  181. Type: `number`
  182. The numeric exit code of the process that was run.
  183. #### stdout
  184. Type: `string | Buffer`
  185. The output of the process on stdout.
  186. #### stderr
  187. Type: `string | Buffer`
  188. The output of the process on stderr.
  189. #### all
  190. Type: `string | Buffer | undefined`
  191. The output of the process with `stdout` and `stderr` interleaved.
  192. This is `undefined` if either:
  193. - the [`all` option](#all-2) is `false` (the default value)
  194. - `execa.sync()` was used
  195. #### failed
  196. Type: `boolean`
  197. Whether the process failed to run.
  198. #### timedOut
  199. Type: `boolean`
  200. Whether the process timed out.
  201. #### isCanceled
  202. Type: `boolean`
  203. Whether the process was canceled.
  204. #### killed
  205. Type: `boolean`
  206. Whether the process was killed.
  207. #### signal
  208. Type: `string | undefined`
  209. The name of the signal that was used to terminate the process. For example, `SIGFPE`.
  210. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.
  211. #### signalDescription
  212. Type: `string | undefined`
  213. A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.
  214. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.
  215. #### message
  216. Type: `string`
  217. Error message when the child process failed to run. In addition to the [underlying error message](#originalMessage), it also contains some information related to why the child process errored.
  218. The child process [stderr](#stderr) then [stdout](#stdout) are appended to the end, separated with newlines and not interleaved.
  219. #### shortMessage
  220. Type: `string`
  221. This is the same as the [`message` property](#message) except it does not include the child process stdout/stderr.
  222. #### originalMessage
  223. Type: `string | undefined`
  224. Original error message. This is the same as the `message` property except it includes neither the child process stdout/stderr nor some additional information added by Execa.
  225. This is `undefined` unless the child process exited due to an `error` event or a timeout.
  226. ### options
  227. Type: `object`
  228. #### cleanup
  229. Type: `boolean`\
  230. Default: `true`
  231. Kill the spawned process when the parent process exits unless either:
  232. - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)
  233. - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit
  234. #### preferLocal
  235. Type: `boolean`\
  236. Default: `false`
  237. Prefer locally installed binaries when looking for a binary to execute.\
  238. If you `$ npm install foo`, you can then `execa('foo')`.
  239. #### localDir
  240. Type: `string`\
  241. Default: `process.cwd()`
  242. Preferred path to find locally installed binaries in (use with `preferLocal`).
  243. #### execPath
  244. Type: `string`\
  245. Default: `process.execPath` (Current Node.js executable)
  246. Path to the Node.js executable to use in child processes.
  247. This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
  248. Requires [`preferLocal`](#preferlocal) to be `true`.
  249. For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.
  250. #### buffer
  251. Type: `boolean`\
  252. Default: `true`
  253. Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.
  254. If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.
  255. #### input
  256. Type: `string | Buffer | stream.Readable`
  257. Write some input to the `stdin` of your binary.\
  258. Streams are not allowed when using the synchronous methods.
  259. #### stdin
  260. Type: `string | number | Stream | undefined`\
  261. Default: `pipe`
  262. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  263. #### stdout
  264. Type: `string | number | Stream | undefined`\
  265. Default: `pipe`
  266. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  267. #### stderr
  268. Type: `string | number | Stream | undefined`\
  269. Default: `pipe`
  270. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  271. #### all
  272. Type: `boolean`\
  273. Default: `false`
  274. Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` interleaved.
  275. #### reject
  276. Type: `boolean`\
  277. Default: `true`
  278. Setting this to `false` resolves the promise with the error instead of rejecting it.
  279. #### stripFinalNewline
  280. Type: `boolean`\
  281. Default: `true`
  282. Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.
  283. #### extendEnv
  284. Type: `boolean`\
  285. Default: `true`
  286. Set to `false` if you don't want to extend the environment variables when providing the `env` property.
  287. ---
  288. Execa also accepts the below options which are the same as the options for [`child_process#spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)/[`child_process#exec()`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)
  289. #### cwd
  290. Type: `string`\
  291. Default: `process.cwd()`
  292. Current working directory of the child process.
  293. #### env
  294. Type: `object`\
  295. Default: `process.env`
  296. Environment key-value pairs. Extends automatically from `process.env`. Set [`extendEnv`](#extendenv) to `false` if you don't want this.
  297. #### argv0
  298. Type: `string`
  299. Explicitly set the value of `argv[0]` sent to the child process. This will be set to `file` if not specified.
  300. #### stdio
  301. Type: `string | string[]`\
  302. Default: `pipe`
  303. Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
  304. #### serialization
  305. Type: `string`\
  306. Default: `'json'`
  307. Specify the kind of serialization used for sending messages between processes when using the [`stdio: 'ipc'`](#stdio) option or [`execa.node()`](#execanodescriptpath-arguments-options):
  308. - `json`: Uses `JSON.stringify()` and `JSON.parse()`.
  309. - `advanced`: Uses [`v8.serialize()`](https://nodejs.org/api/v8.html#v8_v8_serialize_value)
  310. Requires Node.js `13.2.0` or later.
  311. [More info.](https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
  312. #### detached
  313. Type: `boolean`
  314. 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).
  315. #### uid
  316. Type: `number`
  317. Sets the user identity of the process.
  318. #### gid
  319. Type: `number`
  320. Sets the group identity of the process.
  321. #### shell
  322. Type: `boolean | string`\
  323. Default: `false`
  324. If `true`, runs `file` 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.
  325. We recommend against using this option since it is:
  326. - not cross-platform, encouraging shell-specific syntax.
  327. - slower, because of the additional shell interpretation.
  328. - unsafe, potentially allowing command injection.
  329. #### encoding
  330. Type: `string | null`\
  331. Default: `utf8`
  332. Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.
  333. #### timeout
  334. Type: `number`\
  335. Default: `0`
  336. 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.
  337. #### maxBuffer
  338. Type: `number`\
  339. Default: `100_000_000` (100 MB)
  340. Largest amount of data in bytes allowed on `stdout` or `stderr`.
  341. #### killSignal
  342. Type: `string | number`\
  343. Default: `SIGTERM`
  344. Signal value to be used when the spawned process will be killed.
  345. #### windowsVerbatimArguments
  346. Type: `boolean`\
  347. Default: `false`
  348. If `true`, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to `true` automatically when the `shell` option is `true`.
  349. #### windowsHide
  350. Type: `boolean`\
  351. Default: `true`
  352. On Windows, do not create a new console window. Please note this also prevents `CTRL-C` [from working](https://github.com/nodejs/node/issues/29837) on Windows.
  353. #### nodePath *(For `.node()` only)*
  354. Type: `string`\
  355. Default: [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath)
  356. Node.js executable used to create the child process.
  357. #### nodeOptions *(For `.node()` only)*
  358. Type: `string[]`\
  359. Default: [`process.execArgv`](https://nodejs.org/api/process.html#process_process_execargv)
  360. List of [CLI options](https://nodejs.org/api/cli.html#cli_options) passed to the Node.js executable.
  361. ## Tips
  362. ### Retry on error
  363. Gracefully handle failures by using automatic retries and exponential backoff with the [`p-retry`](https://github.com/sindresorhus/p-retry) package:
  364. ```js
  365. const pRetry = require('p-retry');
  366. const run = async () => {
  367. const results = await execa('curl', ['-sSL', 'https://sindresorhus.com/unicorn']);
  368. return results;
  369. };
  370. (async () => {
  371. console.log(await pRetry(run, {retries: 5}));
  372. })();
  373. ```
  374. ### Save and pipe output from a child process
  375. Let's say you want to show the output of a child process in real-time while also saving it to a variable.
  376. ```js
  377. const execa = require('execa');
  378. const subprocess = execa('echo', ['foo']);
  379. subprocess.stdout.pipe(process.stdout);
  380. (async () => {
  381. const {stdout} = await subprocess;
  382. console.log('child output:', stdout);
  383. })();
  384. ```
  385. ### Redirect output to a file
  386. ```js
  387. const execa = require('execa');
  388. const subprocess = execa('echo', ['foo'])
  389. subprocess.stdout.pipe(fs.createWriteStream('stdout.txt'))
  390. ```
  391. ### Redirect input from a file
  392. ```js
  393. const execa = require('execa');
  394. const subprocess = execa('cat')
  395. fs.createReadStream('stdin.txt').pipe(subprocess.stdin)
  396. ```
  397. ### Execute the current package's binary
  398. ```js
  399. const {getBinPathSync} = require('get-bin-path');
  400. const binPath = getBinPathSync();
  401. const subprocess = execa(binPath);
  402. ```
  403. `execa` can be combined with [`get-bin-path`](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the `package.json` `bin` field is correctly set up.
  404. ## Related
  405. - [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for `execa`
  406. - [nvexeca](https://github.com/ehmicky/nvexeca) - Run `execa` using any Node.js version
  407. - [sudo-prompt](https://github.com/jorangreef/sudo-prompt) - Run commands with elevated privileges.
  408. ## Maintainers
  409. - [Sindre Sorhus](https://github.com/sindresorhus)
  410. - [@ehmicky](https://github.com/ehmicky)
  411. ---
  412. <div align="center">
  413. <b>
  414. <a href="https://tidelift.com/subscription/pkg/npm-execa?utm_source=npm-execa&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  415. </b>
  416. <br>
  417. <sub>
  418. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  419. </sub>
  420. </div>