Skip to content

Commit

Permalink
chore: Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored and actions-user committed Nov 8, 2021
1 parent 42ad05f commit 06f60eb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 55 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
Get available v8 and Node.js flags.

## Usage

```js
const v8flags = require('v8flags');

v8flags(function(err, results) {
v8flags(function (err, results) {
console.log(results);
// [ '--use_strict',
// '--es5_readonly',
Expand Down
2 changes: 1 addition & 1 deletion config-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function linux() {
return path.join(env.XDG_CACHE_HOME || path.join(userHome, '.cache'), name);
}

module.exports = function(platform) {
module.exports = function (platform) {
if (!userHome) {
return os.tmpdir();
}
Expand Down
36 changes: 21 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ var exclusions = ['--help', '--completion_bash'];
// This number must be incremented whenever the generated cache file changes.
var CACHE_VERSION = 2;

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

var failureMessage = [
'Unable to cache a config file for v8flags to your home directory',
Expand All @@ -32,8 +39,8 @@ function fail(err) {
}

function openConfig(cb) {
fs.mkdir(configPath, function() {
tryOpenConfig(path.join(configPath, configfile), function(err, fd) {
fs.mkdir(configPath, function () {
tryOpenConfig(path.join(configPath, configfile), function (err, fd) {
if (err) {
return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
}
Expand All @@ -48,15 +55,15 @@ function tryOpenConfig(configpath, cb) {
// node should be able to require it directly. if this doesn't
// throw, we're done!
var content = require(configpath);
process.nextTick(function() {
process.nextTick(function () {
cb(null, content);
});
} catch (e) {
// if requiring the config file failed, maybe it doesn't exist, or
// perhaps it has become corrupted. instead of calling back with the
// content of the file, call back with a file descriptor that we can
// write the cached data to
fs.open(configpath, 'w+', function(err, fd) {
fs.open(configpath, 'w+', function (err, fd) {
if (err) {
return cb(err);
}
Expand Down Expand Up @@ -87,7 +94,7 @@ function getFlags(cb) {

function runNode(option) {
pending++;
execFile(process.execPath, [option], function(execErr, result) {
execFile(process.execPath, [option], function (execErr, result) {
if (execErr || errored) {
if (!errored) {
errored = true;
Expand All @@ -102,9 +109,8 @@ function getFlags(cb) {
regexp.lastIndex = index;
var matchedFlags = result.match(regexp);
if (matchedFlags) {
flags = flags.concat(matchedFlags
.map(normalizeFlagName)
.filter(function(name) {
flags = flags.concat(
matchedFlags.map(normalizeFlagName).filter(function (name) {
return exclusions.indexOf(name) === -1;
})
);
Expand Down Expand Up @@ -135,8 +141,8 @@ function writeConfig(fd, flags, cb) {
}
buf = new Buffer(json);
}
return fs.write(fd, buf, 0, buf.length, 0 , function(writeErr) {
fs.close(fd, function(closeErr) {
return fs.write(fd, buf, 0, buf.length, 0, function (writeErr) {
fs.close(fd, function (closeErr) {
var err = writeErr || closeErr;
if (err) {
return cb(fail(err), flags);
Expand All @@ -146,23 +152,23 @@ function writeConfig(fd, flags, cb) {
});
}

module.exports = function(cb) {
module.exports = function (cb) {
// bail early if this is not node
var isElectron = process.versions && process.versions.electron;
if (isElectron) {
return process.nextTick(function() {
return process.nextTick(function () {
cb(null, []);
});
}

// attempt to open/read cache file
openConfig(function(openErr, result) {
openConfig(function (openErr, result) {
if (!openErr && typeof result !== 'number') {
return cb(null, result);
}
// if the result is not an array, we need to go fetch
// the flags by invoking node with `--v8-options`
getFlags(function(flagsErr, flags) {
getFlags(function (flagsErr, flags) {
// if there was an error fetching the flags, bail immediately
if (flagsErr) {
return cb(flagsErr);
Expand Down
74 changes: 36 additions & 38 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function cleanup() {
path.resolve(v8flags.configPath, v8flags.configfile),
path.resolve(os.tmpdir(), v8flags.configfile),
];
files.forEach(function(file) {
files.forEach(function (file) {
try {
fs.unlinkSync(file);
} catch (e) {
Expand All @@ -64,60 +64,60 @@ function cleanup() {
delete process.versions.electron;
}

describe('v8flags', function() {
describe('v8flags', function () {
before(makeHomeCacheDir);
beforeEach(cleanup);
afterEach(cleanup);

it('should cache and call back with the v8 flags for the running process', function(done) {
it('should cache and call back with the v8 flags for the running process', function (done) {
var v8flags = require('../');
var configfile = path.resolve(v8flags.configPath, v8flags.configfile);
v8flags(function(err, flags) {
v8flags(function (err, flags) {
expect(Array.isArray(flags)).toEqual(true);
expect(fs.existsSync(configfile)).toEqual(true);
fs.unlinkSync(configfile);
fs.writeFileSync(configfile, JSON.stringify({ cached: true }));
v8flags(function(cacheErr, cachedFlags) {
v8flags(function (cacheErr, cachedFlags) {
expect(cachedFlags).toEqual({ cached: true });
done();
});
});
});

it('should not append the file when multiple calls happen concurrently and the config file does not yet exist', function(done) {
it('should not append the file when multiple calls happen concurrently and the config file does not yet exist', function (done) {
var v8flags = require('../');
async.parallel([v8flags, v8flags, v8flags], function() {
v8flags(function() {
async.parallel([v8flags, v8flags, v8flags], function () {
v8flags(function () {
done();
});
});
});

it('should fall back to writing to a temp dir if user home is unwriteable', function(done) {
it('should fall back to writing to a temp dir if user home is unwriteable', function (done) {
eraseHome();
env.HOME = env.LOCALAPPDATA = path.join(__dirname, 'does-not-exist');
var v8flags = require('../');
var configfile = path.resolve(os.tmpdir(), v8flags.configfile);
v8flags(function() {
v8flags(function () {
expect(fs.existsSync(configfile)).toEqual(true);
done();
});
});

it('should return flags even if an error is thrown', function(done) {
it('should return flags even if an error is thrown', function (done) {
eraseHome();
setTemp('/nope');
env.HOME = env.LOCALAPPDATA = null;
var v8flags = require('../');
v8flags(function(err, flags) {
v8flags(function (err, flags) {
resetTemp();
expect(err).not.toBeNull();
expect(flags).not.toBeUndefined();
done();
});
});

it('will not throw on non-matching return from node --v8-options', function(done) {
it('will not throw on non-matching return from node --v8-options', function (done) {
if (os.platform() === 'win32') {
this.skip();
}
Expand All @@ -130,7 +130,7 @@ describe('v8flags', function() {
// Set execPath to our fake-bin
process.execPath = __dirname + '/fake-bin';

v8flags(function(err, flags) {
v8flags(function (err, flags) {
expect(err).toBeNull();
expect(flags).toEqual([]);
// Restore original execPath
Expand All @@ -139,61 +139,61 @@ describe('v8flags', function() {
});
});

it('should back with an empty array if the runtime is electron', function(done) {
it('should back with an empty array if the runtime is electron', function (done) {
process.versions.electron = 'set';
var v8flags = require('../');
v8flags(function(err, flags) {
v8flags(function (err, flags) {
expect(flags.length).toEqual(0);
expect(Array.isArray(flags)).toEqual(true);
done();
});
});

it('should handle usernames which are invalid file paths', function(done) {
it('should handle usernames which are invalid file paths', function (done) {
eraseHome();
env.USER = 'invalid/user\\name';
var v8flags = require('../');
v8flags(function(err) {
v8flags(function (err) {
expect(err).toBe(null);
done();
});
});

it('should handle option names with multiple words', function(done) {
it('should handle option names with multiple words', function (done) {
if (parseInt(process.versions.node) < 4) {
this.skip();
}

eraseHome();
var v8flags = require('../');
v8flags(function(err, flags) {
v8flags(function (err, flags) {
expect(flags).toContain('--expose_gc_as');
done();
});
});

it('should handle undefined usernames', function(done) {
it('should handle undefined usernames', function (done) {
eraseHome();
var v8flags = require('../');
v8flags(function(err) {
v8flags(function (err) {
expect(err).toBe(null);
done();
});
});

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

it('does not detect colliding flags from node', function(done) {
it('does not detect colliding flags from node', function (done) {
eraseHome();
var v8flags = require('../');
v8flags(function(err, flags) {
v8flags(function (err, flags) {
expect(flags).not.toContain('--exec');
expect(flags).not.toContain('--print');
expect(flags).not.toContain('--interactive');
Expand All @@ -204,25 +204,23 @@ describe('v8flags', function() {
});
});

describe('config-path', function() {
describe('config-path', function () {
var moduleName = 'js-v8flags';

before(function() {
before(function () {
env.HOME = env.USERPROFILE = 'somehome';
});

after(cleanup);

it('should return default linux path in other environments', function(done) {
it('should return default linux path in other environments', function (done) {
var configPath = require('../config-path.js')('other');

expect(configPath).toEqual(
path.join(env.HOME, '.cache', moduleName)
);
expect(configPath).toEqual(path.join(env.HOME, '.cache', moduleName));
done();
});

it('should return default macos path in darwin environment', function(done) {
it('should return default macos path in darwin environment', function (done) {
var configPath = require('../config-path.js')('darwin');

expect(configPath).toEqual(
Expand All @@ -231,7 +229,7 @@ describe('config-path', function() {
done();
});

it('should return default windows path in win32 environment', function(done) {
it('should return default windows path in win32 environment', function (done) {
var configPath = require('../config-path.js')('win32');

expect(configPath).toEqual(
Expand All @@ -240,9 +238,9 @@ describe('config-path', function() {
done();
});

it('should return fallback path when homedir is falsy', function(done) {
it('should return fallback path when homedir is falsy', function (done) {
var configPath = proxyquire('../config-path.js', {
'homedir-polyfill': function() {
'homedir-polyfill': function () {
return null;
},
})('win32');
Expand All @@ -252,10 +250,10 @@ describe('config-path', function() {
});
});

describe('platform specific tests', function() {
describe('platform specific tests', function () {
before(cleanup);

it('should return fallback path when no home is found under windows', function(done) {
it('should return fallback path when no home is found under windows', function (done) {
if (os.platform() !== 'win32' || !process.version.match(/0\.10|0\.12/)) {
this.skip();
}
Expand Down

0 comments on commit 06f60eb

Please sign in to comment.