Bachir Soussi Chiadmi cefd1c2ad0 updated sys and created publi | 6 年之前 | |
---|---|---|
.. | ||
lib | 7 年之前 | |
test | 7 年之前 | |
.npmignore | 7 年之前 | |
.travis.yml | 7 年之前 | |
LICENSE | 7 年之前 | |
README.md | 7 年之前 | |
index.js | 7 年之前 | |
package.json | 6 年之前 |
A tool for respawning node binaries when special flags are present.
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
.
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
.
It doesn't work. After digging around for a bit, you realize this produces a process.argv
of:
['node', '/usr/local/bin/test', '--harmony', 'spec', 'tests.js']
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:
['node', '--harmony', '/usr/local/bin/test', 'spec', 'tests.js']
Flagged-respawn fixes this problem and handles all the edge cases respawning creates, such as:
To see it in action, clone this repository and run npm install
/ npm run respawn
/ npm run nospawn
.
#!/usr/bin/env node
const flaggedRespawn = require('flagged-respawn');
// get a list of all possible v8 flags for the running version of node
const v8flags = require('v8flags').fetch();
flaggedRespawn(v8flags, process.argv, function (ready, child) {
if (ready) {
console.log('Running!');
// your cli code here
} else {
console.log('Special flags found, respawning.');
}
if (process.pid !== child.pid) {
console.log('Respawned to PID:', child.pid);
}
});
{ stdio: 'inherit' }
for spawn to maintain colors