Skip to content

Commit

Permalink
Breaking: Rename API to fetch & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kellen authored and phated committed Dec 17, 2018
1 parent fce62fc commit e91dede
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.flags*
*.flags.json
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
# v8flags
> List available v8 flags.
> Get available v8 flags.
[![NPM](https://nodei.co/npm/v8flags.png)](https://nodei.co/npm/v8flags/)

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

v8flags(function(err, flags) {
console.log(flags);
});
v8flags.fetch(); // [ '--use_strict',
// '--es5_readonly',
// '--es52_globals',
// '--harmony_typeof',
// '--harmony_scoping',
// '--harmony_modules',
// '--harmony_proxies',
// '--harmony_collections',
// '--harmony',
// ...
```

## Release History

* 2014-05-09 - v0.1.0 - initial release
* 2014-09-02 - v0.2.0 - cache flags
* 2014-09-02 - v0.3.0 - keep -- in flag names
* 2014-09-03 - v1.0.0 - first major version release
30 changes: 30 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');

const exec = require('child_process').exec;
const version = process.versions.v8;
const tmpfile = path.join(__dirname, version+'.flags.json');

if (!fs.existsSync(tmpfile)) {
exec('node --v8-options', function (execErr, result) {
var flags;
if (execErr) {
throw new Error(execErr);
} else {
flags = result.match(/\s\s--(\w+)/gm).map(function (match) {
return match.substring(2);
});
fs.writeFile(tmpfile, JSON.stringify(flags), { encoding:'utf8' },
function (writeErr) {
if (writeErr) {
throw new Error(writeErr);
} else {
console.log('v8flags for node '+version+' cached.');
}
}
);
}
});
}

module.exports = require.bind(null, tmpfile);
35 changes: 1 addition & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');

const version = process.versions.v8;
const tmpfile = path.join(__dirname, '.flags-'+version);

module.exports = function (cb) {
fs.exists(tmpfile, function (exists) {
if (exists) {
fs.readFile(tmpfile, function (err, flags) {
if (err) {
cb(err);
}
cb(null, JSON.parse(flags));
});
} else {
exec('node --v8-options', function (err, result) {
var flags = result.match(/\s\s--(\w+)/gm).map(function (match) {
return match.substring(2);
});
fs.writeFile(tmpfile, JSON.stringify(flags), { encoding:'utf8' },
function (writeErr) {
if (writeErr) {
cb(writeErr);
} else {
cb(null, flags);
}
}
);
});
}
});
};
exports.fetch = require('./fetch');
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v8flags",
"description": "List available v8 flags.",
"description": "Get available v8 flags.",
"version": "0.3.0",
"homepage": "https://github.com/tkellen/node-v8flags",
"author": {
Expand All @@ -20,12 +20,20 @@
"url": "https://github.com/tkellen/node-v8flags/blob/master/LICENSE"
}
],
"scripts": {
"install": "node fetch.js",
"test": "mocha -R spec test/index.js"
},
"main": "index.js",
"engines": {
"node": ">= 0.8.0"
},
"keywords": [
"v8 flags",
"harmony flags"
]
],
"devDependencies": {
"chai": "~1.9.1",
"mocha": "~1.21.4"
}
}
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');
const fs = require('fs');

const expect = require('chai').expect;

const v8flags = require('../');

const tmpfile = path.resolve(process.versions.v8+'.flags.json');

describe('v8flags', function () {

it('should have created a temp file during installation', function() {
expect(fs.existsSync(tmpfile)).to.be.true;
});

describe('::fetch', function () {
it('should require v8 flags temp file', function () {
expect(require(tmpfile)).to.deep.equal(v8flags.fetch());
})
});

});

0 comments on commit e91dede

Please # to comment.