版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

README.md 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # flagged-respawn
  7. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]
  8. A tool for respawning node binaries when special flags are present.
  9. ## What is it?
  10. Say you wrote a command line tool that runs arbitrary javascript (e.g. task runner, test framework, etc). For the sake of discussion, let's pretend it's a testing harness you've named `testify`.
  11. Everything is going splendidly until one day you decide to test some code that relies on a feature behind a v8 flag in node (`--harmony`, for example). Without much thought, you run `testify --harmony spec tests.js`.
  12. It doesn't work. After digging around for a bit, you realize this produces a [`process.argv`](http://nodejs.org/docs/latest/api/process.html#process_process_argv) of:
  13. `['node', '/usr/local/bin/test', '--harmony', 'spec', 'tests.js']`
  14. Crap. The `--harmony` flag is in the wrong place! It should be applied to the **node** command, not our binary. What we actually wanted was this:
  15. `['node', '--harmony', '/usr/local/bin/test', 'spec', 'tests.js']`
  16. Flagged-respawn fixes this problem and handles all the edge cases respawning creates, such as:
  17. - Providing a method to determine if a respawn is needed.
  18. - Piping stderr/stdout from the child into the parent.
  19. - Making the parent process exit with the same code as the child.
  20. - If the child is killed, making the parent exit with the same signal.
  21. To see it in action, clone this repository and run `npm install` / `npm run respawn` / `npm run nospawn`.
  22. ## Sample Usage
  23. ```js
  24. #!/usr/bin/env node
  25. const flaggedRespawn = require('flagged-respawn');
  26. // get a list of all possible v8 flags for the running version of node
  27. const v8flags = require('v8flags').fetch();
  28. flaggedRespawn(v8flags, process.argv, function (ready, child) {
  29. if (ready) {
  30. console.log('Running!');
  31. // your cli code here
  32. } else {
  33. console.log('Special flags found, respawning.');
  34. }
  35. if (process.pid !== child.pid) {
  36. console.log('Respawned to PID:', child.pid);
  37. }
  38. });
  39. ```
  40. ## API
  41. ### <u>flaggedRespawn(flags, argv, [ forcedFlags, ] callback) : Void</u>
  42. Respawns the script itself when _argv_ has special flag contained in _flags_ and/or _forcedFlags_ is not empty. Because members of _flags_ and _forcedFlags_ are passed to `node` command, each of them needs to be a node flag or a V8 flag.
  43. #### Forbid respawning
  44. If `--no-respawning` flag is given in _argv_, this function does not respawned even if _argv_ contains members of flags or _forcedFlags_ is not empty. (This flag is also used internally to prevent from respawning more than once).
  45. #### Parameter:
  46. | Parameter | Type | Description |
  47. | :------------ | :-------------: | :--------------------------------------------------------------------------------------- |
  48. | _flags_ | Array | An array of node flags and V8 flags which are available when present in _argv_. |
  49. | _argv_ | Array | Command line arguments to respawn. |
  50. | _forcedFlags_ | Array or String | An array of node flags or a string of a single flag and V8 flags for respawning forcely. |
  51. | _callback_ | function | A called function when not respawning or after respawned. |
  52. - **<u><i>callback</i>(ready, proc, argv) : Void</u>**
  53. _callback_ function is called both when respawned or not, and it can be distinguished by callback's argument: _ready_. (_ready_ indicates whether a process spawned its child process (false) or not (true), but it does not indicate whether a process is a spawned child process or not. _ready_ for a spawned child process is true.)
  54. _argv_ is an array of command line arguments which is respawned (when _ready_ is false) or is passed current process except flags within _flags_ and `--no-respawning` (when _ready_ is true).
  55. **Parameter:**
  56. | Parameter | Type | Description |
  57. | :-------- | :-----: | :------------------------------------------------------------------- |
  58. | _ready_ | boolean | True, if not respawning and is ready to execute main function. |
  59. | _proc_ | object | Child process object if respawned, otherwise current process object. |
  60. | _argv_ | Array | An array of command line arguments. |
  61. ## License
  62. MIT
  63. <!-- prettier-ignore-start -->
  64. [downloads-image]: https://img.shields.io/npm/dm/flagged-respawn.svg?style=flat-square
  65. [npm-url]: https://www.npmjs.com/package/flagged-respawn
  66. [npm-image]: https://img.shields.io/npm/v/flagged-respawn.svg?style=flat-square
  67. [ci-url]: https://github.com/gulpjs/flagged-respawn/actions?query=workflow:dev
  68. [ci-image]: https://img.shields.io/github/workflow/status/gulpjs/flagged-respawn/dev?style=flat-square
  69. [coveralls-url]: https://coveralls.io/r/gulpjs/flagged-respawn
  70. [coveralls-image]: https://img.shields.io/coveralls/gulpjs/flagged-respawn/master.svg?style=flat-square
  71. <!-- prettier-ignore-end -->