Initial commit
[yaffs-website] / node_modules / flagged-respawn / README.md
1 # flagged-respawn [![Build Status](https://secure.travis-ci.org/js-cli/js-flagged-respawn.svg)](http://travis-ci.org/js-cli/js-flagged-respawn)
2 > A tool for respawning node binaries when special flags are present.
3
4 [![NPM](https://nodei.co/npm/flagged-respawn.png)](https://nodei.co/npm/flagged-respawn/)
5
6 ## What is it?
7
8 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`.
9
10 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`.
11
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
14 `['node', '/usr/local/bin/test', '--harmony', 'spec', 'tests.js']`
15
16 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:
17
18 `['node', '--harmony', '/usr/local/bin/test', 'spec', 'tests.js']`
19
20 Flagged-respawn fixes this problem and handles all the edge cases respawning creates, such as:
21 - Providing a method to determine if a respawn is needed.
22 - Piping stderr/stdout from the child into the parent.
23 - Making the parent process exit with the same code as the child.
24 - If the child is killed, making the parent exit with the same signal.
25
26 To see it in action, clone this repository and run `npm install` / `npm run respawn` / `npm run nospawn`.
27
28 ## Sample Usage
29
30 ```js
31 #!/usr/bin/env node
32
33 const flaggedRespawn = require('flagged-respawn');
34
35 // get a list of all possible v8 flags for the running version of node
36 const v8flags = require('v8flags').fetch();
37
38 flaggedRespawn(v8flags, process.argv, function (ready, child) {
39   if (ready) {
40     console.log('Running!');
41     // your cli code here
42   } else {
43     console.log('Special flags found, respawning.');
44   }
45   if (process.pid !== child.pid) {
46     console.log('Respawned to PID:', child.pid);
47   }
48 });
49
50 ```
51
52 ## Release History
53
54 * 2016-03-22 - v0.3.2 - fix issue with v8 flags values being dropped
55 * 2014-09-12 - v0.3.1 - use `{ stdio: 'inherit' }` for spawn to maintain colors
56 * 2014-09-11 - v0.3.0 - for real this time
57 * 2014-09-11 - v0.2.0 - cleanup
58 * 2014-09-04 - v0.1.1 - initial release