版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. EZ Spawn
  2. =======================
  3. #### Simple, consistent process spawning
  4. [![Cross-Platform Compatibility](https://jstools.dev/img/badges/os-badges.svg)](https://github.com/JS-DevTools/ez-spawn/actions)
  5. [![Build Status](https://github.com/JS-DevTools/ez-spawn/workflows/CI-CD/badge.svg)](https://github.com/JS-DevTools/ez-spawn/actions)
  6. [![Coverage Status](https://coveralls.io/repos/github/JS-DevTools/ez-spawn/badge.svg?branch=master)](https://coveralls.io/github/JS-DevTools/ez-spawn?branch=master)
  7. [![Dependencies](https://david-dm.org/JS-DevTools/ez-spawn.svg)](https://david-dm.org/JS-DevTools/ez-spawn)
  8. [![npm](https://img.shields.io/npm/v/@jsdevtools/ez-spawn.svg)](https://www.npmjs.com/package/@jsdevtools/ez-spawn)
  9. [![License](https://img.shields.io/npm/l/@jsdevtools/ez-spawn.svg)](LICENSE)
  10. [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/JS-DevTools/ez-spawn)
  11. Features
  12. --------------------------
  13. - **Flexible input parameters**<br>
  14. Pass the program and arguments as a single string, an array of strings, or as separate parameters.
  15. - **Pick your async syntax**<br>
  16. Supports Promises, `async`/`await`, or callbacks.
  17. - **Simple, consistent error handling**<br>
  18. Non-zero exit codes are treated just like any other error. See [Error Handling](#error-handling) for more details.
  19. - **String output by default**<br>
  20. stdout and stderr output is automatically decoded as UTF-8 text by default. You can set the [`encoding` option](#options-object) for a different encoding, or even raw binary buffers.
  21. - **Windows Support**<br>
  22. Excellent Windows support, thanks to [cross-spawn](https://github.com/moxystudio/node-cross-spawn).
  23. Related Projects
  24. --------------------------
  25. - [chai-exec](https://github.com/JS-DevTools/chai-exec) - Chai assertion plugin for testing CLIs
  26. Examples
  27. --------------------------
  28. ```javascript
  29. const ezSpawn = require('@jsdevtools/ez-spawn');
  30. // These are all identical
  31. ezSpawn.sync(`git commit -am "Fixed a bug"`); // Pass program and args as a string
  32. ezSpawn.sync("git", "commit", "-am", "Fixed a bug"); // Pass program and args as separate params
  33. ezSpawn.sync(["git", "commit", "-am", "Fixed a bug"]); // Pass program and args as an array
  34. ezSpawn.sync("git", ["commit", "-am", "Fixed a bug"]); // Pass program as a string and args as an array
  35. // Make a synchronous call
  36. let process = ezSpawn.sync(`git commit -am "Fixed a bug"`);
  37. console.log(process.stdout);
  38. //Make an asynchronous call, using async/await syntax
  39. let process = await ezSpawn.async(`git commit -am "Fixed a bug"`);
  40. console.log(process.stdout);
  41. //Make an asynchronous call, using callback syntax
  42. ezSpawn.async(`git commit -am "Fixed a bug"`, (err, process) => {
  43. console.log(process.stdout);
  44. });
  45. //Make an asynchronous call, using Promise syntax
  46. ezSpawn.async(`git commit -am "Fixed a bug"`)
  47. .then((process) => {
  48. console.log(process.stdout);
  49. });
  50. ```
  51. Installation
  52. --------------------------
  53. Install using [npm](https://docs.npmjs.com/about-npm/):
  54. ```bash
  55. npm install @jsdevtools/ez-spawn
  56. ```
  57. Then require it in your code:
  58. ```javascript
  59. // Require the whole package
  60. const ezSpawn = require("@jsdevtools/ez-spawn");
  61. // Or require "sync" or "async" directly
  62. const ezSpawnSync = require("@jsdevtools/ez-spawn").sync;
  63. const ezSpawnAsync = require("@jsdevtools/ez-spawn").async;
  64. ```
  65. API
  66. --------------------------
  67. ### `ezSpawn.sync(command, [...arguments], [options])`
  68. Synchronously spawns a process. This function returns when the proecess exits.
  69. - The `command` and `arguments` parameters can be passed as a single space-separated string, or as an array of strings, or as separate parameters.
  70. - The [`options` object](#options-object) is optional.
  71. - Returns a [`Process` object](#process-object)
  72. ### `ezSpawn.async(command, [...arguments], [options], [callback])`
  73. Asynchronously spawns a process. The Promise resolves (or the callback is called) when the process exits.
  74. - The `command` and `arguments` parameters can be passed as a single space-separated string, or as an array of strings, or as separate parameters.
  75. - The [`options` object](#options-object) is optional.
  76. - If a `callback` is provided, then it will be called when the process exits. The first is either an `Error` or `null`. The second parameter is a [`Process` object](#process-object).
  77. - If **no** `callback` is provided, then a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) is returned. It will resolve with a [`Process` object](#process-object) when the process exits.
  78. ### `Options` object
  79. `ezSpawn.async()` and `ezSpawn.sync()` both accept an optional `options` object that closely mirrors the `options` parameter of Node's [`spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), [`exec`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback), [`spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options), and [`execSync`](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options) functions.
  80. - `cwd` (string)<br>
  81. The current working directory of the child process.
  82. - `env` (Object)<br>
  83. Environment variable key-value pairs.
  84. - `argv0` (string)<br>
  85. Explicitly set the value of `argv[0]` sent to the child process. This will be set to `command` if not specified.
  86. - `stdio` (Array or string)<br>
  87. The child process's stdio configuration (see [options.stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio)).
  88. - `input` (string, Buffer, TypedArray, or DataView)<br>
  89. The value which will be passed as stdin to the spawned process. Supplying this value will override `stdio[0]`.
  90. - `uid` (number)<br>
  91. Sets the user identity of the process.
  92. - `gid` (number)<br>
  93. Sets the group identity of the process.
  94. - `timeout` (number)<br>
  95. The maximum amount of time (in milliseconds) the process is allowed to run.
  96. - `killSignal` (string or integer)<br>
  97. The signal value to be used when the spawned process will be killed. Defaults to `"SIGTERM"`.
  98. - `maxBuffer` (number)<br>
  99. The largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated.
  100. - `encoding` (string)<br>
  101. The encoding used for all stdio inputs and outputs. Defaults to `"utf8"`. Set to `"buffer"` for raw binary output.
  102. - `shell` (boolean or string)<br>
  103. If `true`, then `command` will be run inside of a shell. Uses "/bin/sh" on UNIX, and `process.env.ComSpec` on Windows. A different shell can be specified as a string.
  104. - `windowsVerbatimArguments` (boolean)<br>
  105. No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when `shell` is `"CMD"`.
  106. - `windowsHide` (boolean)<br>
  107. Hide the subprocess console window that would normally be created on Windows systems.
  108. ### `Process` object
  109. `ezSpawn.async()` and `ezSpawn.sync()` both return a `Process` object that closely mirrors the object returned by Node's [`spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options) function.
  110. - `command` (string)<br>
  111. The command that was used to spawn the process.
  112. - `args` (array of strings)<br>
  113. The command-line arguments that were passed to the process.
  114. - `pid` (number)<br>
  115. The numeric process ID assigned by the operating system.
  116. - `stdout` (string or Buffer)<br>
  117. The process's standard output. This is the same value as `output[1]`.
  118. - `stderr` (string or Buffer)<br>
  119. The process's error output. This is the same value as `output[2]`.
  120. - `output` (array of strings or Buffers)<br>
  121. The process's stdio [stdin, stdout, stderr].
  122. - `status` (number)<br>
  123. The process's status code (a.k.a. "exit code").
  124. - `signal` (string or null)<br>
  125. The signal used to kill the process, if the process was killed by a signal.
  126. - `toString()`<br>
  127. Returns the `command` and `args` as a single string. Useful for console logging.
  128. Error Handling
  129. --------------------------
  130. All sorts of errors can occur when spawning processes. Node's built-in [`spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) and [`spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options) functions handle different types of errors in different ways. Sometimes they throw the error, somtimes they emit an ["error" event](https://nodejs.org/docs/latest/api/child_process.html#child_process_event_error), and sometimes they return an object with an `error` property. They also don't treat non-zero exit codes as errors. So it's up to you to handle all these different types of errors, and check the exit code too.
  131. EZ Spawn simplifies things by treating all errors the same. If any error occurs, or if the process exits with a non-zero exit code, then an error is thrown. The error will have all the same properties as the [`Process` object](#process-object), such as `status`, `stderr`, `signal`, etc.
  132. ```javascript
  133. try {
  134. let process = ezSpawn.sync(`git commit -am "Fixed a bug"`, { throwOnError: true });
  135. console.log("Everything worked great!", process.stdout);
  136. }
  137. catch (error) {
  138. console.error("Something went wrong!", error.status, error.stderr);
  139. }
  140. ```
  141. Contributing
  142. --------------------------
  143. Contributions, enhancements, and bug-fixes are welcome! [Open an issue](https://github.com/JS-DevTools/ez-spawn/issues) on GitHub and [submit a pull request](https://github.com/JS-DevTools/ez-spawn/pulls).
  144. #### Building/Testing
  145. To build/test the project locally on your computer:
  146. 1. __Clone this repo__<br>
  147. `git clone hhttps://github.com/JS-DevTools/ez-spawn.git`
  148. 2. __Install dependencies__<br>
  149. `npm install`
  150. 3. __Run the tests__<br>
  151. `npm test`
  152. License
  153. --------------------------
  154. EZ Spawn is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
  155. This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/JS-DevTools/ez-spawn) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
  156. Big Thanks To
  157. --------------------------
  158. Thanks to these awesome companies for their support of Open Source developers ❤
  159. [![Travis CI](https://jstools.dev/img/badges/travis-ci.svg)](https://travis-ci.com)
  160. [![SauceLabs](https://jstools.dev/img/badges/sauce-labs.svg)](https://saucelabs.com)
  161. [![Coveralls](https://jstools.dev/img/badges/coveralls.svg)](https://coveralls.io)