Skip to content

Commit

Permalink
New: Also detect node-specific flags (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoack committed May 31, 2020
1 parent 5290b7a commit c77b2c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Travis Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]

Get available v8 flags.
Get available v8 and Node.js flags.

## Usage
```js
Expand Down
56 changes: 40 additions & 16 deletions index.js
Expand Up @@ -11,10 +11,10 @@ var configPath = require('./config-path.js')(process.platform);
var version = require('./package.json').version;
var env = process.env;
var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME || '';
var exclusions = ['--help'];
var exclusions = ['--help', '--completion_bash'];

// This number must be incremented whenever the generated cache file changes.
var CACHE_VERSION = 1;
var CACHE_VERSION = 2;

var configfile = '.v8flags-' + CACHE_VERSION + '-' + process.versions.v8 + '.' + crypto.createHash('md5').update(user).digest('hex') + '.json';

Expand Down Expand Up @@ -79,20 +79,44 @@ function normalizeFlagName(flag) {
// `--v8-options` and parses the result, returning an array of command
// line flags.
function getFlags(cb) {
execFile(process.execPath, ['--v8-options'], function(execErr, result) {
if (execErr) {
return cb(execErr);
}
// If the binary doesn't return flags in the way we expect, default to an empty array
// Reference https://github.com/gulpjs/v8flags/issues/53
var matchedFlags = result.match(/\s\s--[\w-]+/gm) || [];
var flags = matchedFlags
.map(normalizeFlagName)
.filter(function(name) {
return exclusions.indexOf(name) === -1;
});
return cb(null, flags);
});
var errored = false;
var pending = 0;
var flags = [];

runNode('--help');
runNode('--v8-options');

function runNode(option) {
pending++;
execFile(process.execPath, [option], function(execErr, result) {
if (execErr || errored) {
if (!errored) {
errored = true;
cb(execErr);
}
return;
}

var index = result.indexOf('\nOptions:');
if (index >= 0) {
var regexp = /^\s\s--[\w-]+/gm;
regexp.lastIndex = index;
var matchedFlags = result.match(regexp);
if (matchedFlags) {
flags = flags.concat(matchedFlags
.map(normalizeFlagName)
.filter(function(name) {
return exclusions.indexOf(name) === -1;
})
);
}
}

if (--pending === 0) {
cb(null, flags);
}
});
}
}

// write some json to a file descriptor. if this fails, call back
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Expand Up @@ -173,6 +173,15 @@ describe('v8flags', function() {
done();
});
});

it('should detect non-v8 flags', function(done) {
eraseHome();
var v8flags = require('../');
v8flags(function(err, flags) {
expect(flags).toInclude('--no_deprecation');
done();
});
});
});

describe('config-path', function() {
Expand Down

0 comments on commit c77b2c8

Please sign in to comment.