From c77b2c8485e0e1d29077fc3fca57d5f9756d89c5 Mon Sep 17 00:00:00 2001 From: Sebastian Noack Date: Sun, 31 May 2020 18:14:56 -0400 Subject: [PATCH] New: Also detect node-specific flags (#55) --- README.md | 2 +- index.js | 56 ++++++++++++++++++++++++++++++++++++--------------- test/index.js | 9 +++++++++ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2760f8e..19551fd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index f8ebcc1..0058c5a 100644 --- a/index.js +++ b/index.js @@ -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'; @@ -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 diff --git a/test/index.js b/test/index.js index bc2f5d6..7c4e7d3 100644 --- a/test/index.js +++ b/test/index.js @@ -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() {