Skip to content

Commit 90f882e

Browse files
committed
Revert "tools: simplify tools/doc/addon-verify.js"
This reverts commit c668263. Reverted along with d9b59de as this breaks compilation from downloadable source tarballs. Ref: #17407 PR-URL: #18287 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 57bd27e commit 90f882e

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

tools/doc/addon-verify.js

+59-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const { strictEqual } = require('assert');
43
const fs = require('fs');
54
const path = require('path');
65
const marked = require('marked');
@@ -9,36 +8,52 @@ const rootDir = path.resolve(__dirname, '..', '..');
98
const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md');
109
const verifyDir = path.resolve(rootDir, 'test', 'addons');
1110

11+
const contents = fs.readFileSync(doc).toString();
12+
13+
const tokens = marked.lexer(contents);
1214
let id = 0;
13-
let currentHeader;
1415

16+
let currentHeader;
1517
const addons = {};
16-
const content = fs.readFileSync(doc, 'utf8');
17-
for (const { text, type } of marked.lexer(content)) {
18-
if (type === 'heading' && text) {
19-
currentHeader = text;
18+
tokens.forEach((token) => {
19+
if (token.type === 'heading' && token.text) {
20+
currentHeader = token.text;
2021
addons[currentHeader] = {
2122
files: {}
2223
};
2324
}
24-
if (type === 'code') {
25-
const match = text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
25+
if (token.type === 'code') {
26+
var match = token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
2627
if (match !== null) {
27-
addons[currentHeader].files[match[1]] = text;
28+
addons[currentHeader].files[match[1]] = token.text;
2829
}
2930
}
31+
});
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; });
3037
}
3138

32-
for (const header in addons) {
33-
let { files } = addons[header];
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+
}
3448

49+
function verifyFiles(files, blockName, onprogress, ondone) {
3550
// must have a .cc and a .js to be a valid test
3651
if (!Object.keys(files).some((name) => /\.cc$/.test(name)) ||
3752
!Object.keys(files).some((name) => /\.js$/.test(name))) {
38-
continue;
53+
return;
3954
}
4055

41-
const blockName = header
56+
blockName = blockName
4257
.toLowerCase()
4358
.replace(/\s/g, '_')
4459
.replace(/[^a-z\d_]/g, '');
@@ -47,17 +62,29 @@ for (const header in addons) {
4762
`${(++id < 10 ? '0' : '') + id}_${blockName}`
4863
);
4964

50-
files = Object.entries(files).map(([name, content]) => {
51-
if (name === 'test.js') content = boilerplate(name, content);
52-
return { name, content, path: path.resolve(dir, name) };
65+
files = Object.keys(files).map(function(name) {
66+
if (name === 'test.js') {
67+
files[name] = `'use strict';
68+
const common = require('../../common');
69+
${files[name].replace(
70+
"'./build/Release/addon'",
71+
// eslint-disable-next-line no-template-curly-in-string
72+
'`./build/${common.buildType}/addon`')}
73+
`;
74+
}
75+
return {
76+
path: path.resolve(dir, name),
77+
name: name,
78+
content: files[name]
79+
};
5380
});
5481

5582
files.push({
5683
path: path.resolve(dir, 'binding.gyp'),
5784
content: JSON.stringify({
5885
targets: [
5986
{
60-
target_name: 'binding',
87+
target_name: 'addon',
6188
defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
6289
sources: files.map(function(file) {
6390
return file.name;
@@ -67,34 +94,22 @@ for (const header in addons) {
6794
})
6895
});
6996

70-
try {
71-
fs.mkdirSync(dir);
72-
} catch (e) {
73-
strictEqual(e.code, 'EEXIST');
74-
}
97+
fs.mkdir(dir, function() {
98+
// Ignore errors
7599

76-
for (const file of files) {
77-
let content;
78-
try {
79-
content = fs.readFileSync(file.path, 'utf8');
80-
} catch (e) {
81-
strictEqual(e.code, 'ENOENT');
82-
}
100+
const done = once(ondone);
101+
var waiting = files.length;
102+
files.forEach(function(file) {
103+
fs.writeFile(file.path, file.content, function(err) {
104+
if (err)
105+
return done(err);
83106

84-
// Only update when file content has changed to prevent unneeded rebuilds.
85-
if (content !== file.content) {
86-
fs.writeFileSync(file.path, file.content);
87-
console.log('wrote', file.path);
88-
}
89-
}
90-
}
107+
if (onprogress)
108+
onprogress(file.path);
91109

92-
function boilerplate(name, content) {
93-
return `'use strict';
94-
const common = require('../../common');
95-
${content.replace(
96-
"'./build/Release/binding'",
97-
// eslint-disable-next-line no-template-curly-in-string
98-
'`./build/${common.buildType}/binding`')}
99-
`;
110+
if (--waiting === 0)
111+
done();
112+
});
113+
});
114+
});
100115
}

0 commit comments

Comments
 (0)