Skip to content

Commit ac05c2b

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 14a5dd4 commit ac05c2b

File tree

1 file changed

+37
-61
lines changed

1 file changed

+37
-61
lines changed

‎tools/doc/addon-verify.js‎

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
'use strict';
22

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

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

11-
constcontents=fs.readFileSync(doc).toString();
12-
13-
consttokens=marked.lexer(contents);
11+
consttokens=lexer(readFileSync(doc,'utf8'));
12+
constaddons={};
1413
letid=0;
15-
1614
letcurrentHeader;
17-
constaddons={};
18-
tokens.forEach((token)=>{
19-
if(token.type==='heading'&&token.text){
20-
currentHeader=token.text;
21-
addons[currentHeader]={
22-
files: {}
23-
};
15+
16+
constvalidNames=/^\/\/\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-
varmatch=token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
22+
if(type==='code'){
23+
constmatch=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(varheaderinaddons){
33-
verifyFiles(addons[header].files,
34-
header,
35-
console.log.bind(null,'wrote'),
36-
function(err){if(err)throwerr;});
37-
}
3829

39-
functiononce(fn){
40-
varonce=false;
41-
returnfunction(){
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+
functionverifyFiles(files,blockName){
35+
constfileNames=Object.keys(files);
4836

49-
functionverifyFiles(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-
constdir=path.resolve(
43+
blockName=blockName.toLowerCase().replace(/\s/g,'_').replace(/\W/g,'');
44+
constdir=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-
returnfile.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-
constdone=once(ondone);
101-
varwaiting=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-
returndone(err);
106-
107-
if(onprogress)
108-
onprogress(file.path);
85+
throwerr;
10986

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

0 commit comments

Comments
(0)