Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect non-v8 flags #55

Merged
merged 1 commit into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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