Skip to content

Commit b5584c4

Browse files
vsemozhetbytMylesBorins
authored andcommitted
tools: modernize and optimize doc/addon-verify.js
Modernize: * Replace `var` with `const` / `let`. * Replace common functions with arrow functions. * Use destructuring. * Use `String.prototype.padStart()`, `String.prototype.endsWith()`. Optimize: * Reduce function calls. * Reduce intermediate variables. * Cache retrieved object properties. * Move RegExp declaration out of a cycle. * Simplify RegExps. * Replace RegExp with string when string suffices. * Remove conditions that cannot be false. * Replace for..in with `Object.keys().forEach()`. Also, eliminate needlessly complicated function chains: * `ondone` callback only checks errors; * if there is an error, it is called once and throws, then script exits; * if there are no errors, it is noop; * so there is no need to wrap it into `once()` function * and there is no need to call it without errors; * we can eliminate it and replace with `throw` where an error occurs; * we can also replace `onprogress` callback with `console.log` in place; * at last, we can eliminate `waiting` counter and `once()` utility. The new script produces results identical to the old ones. PR-URL: #20188 Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9eacd66 commit b5584c4

File tree

1 file changed

+37
-61
lines changed

1 file changed

+37
-61
lines changed

tools/doc/addon-verify.js

+37-61
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
'use strict';
22

3-
const fs = require('fs');
4-
const path = require('path');
5-
const marked = require('marked');
3+
const { mkdir, readFileSync, writeFile } = require('fs');
4+
const { resolve } = require('path');
5+
const { lexer } = require('marked');
66

7-
const rootDir = path.resolve(__dirname, '..', '..');
8-
const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md');
9-
const verifyDir = path.resolve(rootDir, 'test', 'addons');
7+
const rootDir = resolve(__dirname, '..', '..');
8+
const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
9+
const verifyDir = resolve(rootDir, 'test', 'addons');
1010

11-
const contents = fs.readFileSync(doc).toString();
12-
13-
const tokens = marked.lexer(contents);
11+
const tokens = lexer(readFileSync(doc, 'utf8'));
12+
const addons = {};
1413
let id = 0;
15-
1614
let currentHeader;
17-
const addons = {};
18-
tokens.forEach((token) => {
19-
if (token.type === 'heading' && token.text) {
20-
currentHeader = token.text;
21-
addons[currentHeader] = {
22-
files: {}
23-
};
15+
16+
const validNames = /^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/;
17+
tokens.forEach(({ type, text }) => {
18+
if (type === 'heading') {
19+
currentHeader = text;
20+
addons[currentHeader] = { files: {} };
2421
}
25-
if (token.type === 'code') {
26-
var match = token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
22+
if (type === 'code') {
23+
const match = text.match(validNames);
2724
if (match !== null) {
28-
addons[currentHeader].files[match[1]] = token.text;
25+
addons[currentHeader].files[match[1]] = text;
2926
}
3027
}
3128
});
32-
for (var header in addons) {
33-
verifyFiles(addons[header].files,
34-
header,
35-
console.log.bind(null, 'wrote'),
36-
function(err) { if (err) throw err; });
37-
}
3829

39-
function once(fn) {
40-
var once = false;
41-
return function() {
42-
if (once)
43-
return;
44-
once = true;
45-
fn.apply(this, arguments);
46-
};
47-
}
30+
Object.keys(addons).forEach((header) => {
31+
verifyFiles(addons[header].files, header);
32+
});
33+
34+
function verifyFiles(files, blockName) {
35+
const fileNames = Object.keys(files);
4836

49-
function verifyFiles(files, blockName, onprogress, ondone) {
5037
// Must have a .cc and a .js to be a valid test.
51-
if (!Object.keys(files).some((name) => /\.cc$/.test(name)) ||
52-
!Object.keys(files).some((name) => /\.js$/.test(name))) {
38+
if (!fileNames.some((name) => name.endsWith('.cc')) ||
39+
!fileNames.some((name) => name.endsWith('.js'))) {
5340
return;
5441
}
5542

56-
blockName = blockName
57-
.toLowerCase()
58-
.replace(/\s/g, '_')
59-
.replace(/[^a-z\d_]/g, '');
60-
const dir = path.resolve(
43+
blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
44+
const dir = resolve(
6145
verifyDir,
62-
`${(++id < 10 ? '0' : '') + id}_${blockName}`
46+
`${String(++id).padStart(2, '0')}_${blockName}`
6347
);
6448

65-
files = Object.keys(files).map(function(name) {
49+
files = fileNames.map((name) => {
6650
if (name === 'test.js') {
6751
files[name] = `'use strict';
6852
const common = require('../../common');
@@ -73,42 +57,34 @@ ${files[name].replace(
7357
`;
7458
}
7559
return {
76-
path: path.resolve(dir, name),
60+
path: resolve(dir, name),
7761
name: name,
7862
content: files[name]
7963
};
8064
});
8165

8266
files.push({
83-
path: path.resolve(dir, 'binding.gyp'),
67+
path: resolve(dir, 'binding.gyp'),
8468
content: JSON.stringify({
8569
targets: [
8670
{
8771
target_name: 'addon',
8872
defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
89-
sources: files.map(function(file) {
90-
return file.name;
91-
})
73+
sources: files.map(({ name }) => name)
9274
}
9375
]
9476
})
9577
});
9678

97-
fs.mkdir(dir, function() {
79+
mkdir(dir, () => {
9880
// Ignore errors.
9981

100-
const done = once(ondone);
101-
var waiting = files.length;
102-
files.forEach(function(file) {
103-
fs.writeFile(file.path, file.content, function(err) {
82+
files.forEach(({ path, content }) => {
83+
writeFile(path, content, (err) => {
10484
if (err)
105-
return done(err);
106-
107-
if (onprogress)
108-
onprogress(file.path);
85+
throw err;
10986

110-
if (--waiting === 0)
111-
done();
87+
console.log(`Wrote ${path}`);
11288
});
11389
});
11490
});

0 commit comments

Comments
 (0)