11'use strict';
22
3+ const { strictEqual } = require('assert');
34const fs = require('fs');
45const path = require('path');
56const marked = require('marked');
@@ -8,52 +9,36 @@ const rootDir = path.resolve(__dirname, '..', '..');
89const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md');
910const verifyDir = path.resolve(rootDir, 'test', 'addons');
1011
11- const contents = fs.readFileSync(doc).toString();
12-
13- const tokens = marked.lexer(contents);
1412let id = 0;
15-
1613let currentHeader;
14+
1715const addons = {};
18- tokens.forEach((token) => {
19- if (token.type === 'heading' && token.text) {
20- currentHeader = token.text;
16+ const content = fs.readFileSync(doc, 'utf8');
17+ for (const { text, type } of marked.lexer(content)) {
18+ if (type === 'heading' && text) {
19+ currentHeader = text;
2120 addons[currentHeader] = {
2221 files: {}
2322 };
2423 }
25- if (token. type === 'code') {
26- var match = token. text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
24+ if (type === 'code') {
25+ const match = text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
2726 if (match !== null) {
28- addons[currentHeader].files[match[1]] = token. text;
27+ addons[currentHeader].files[match[1]] = text;
2928 }
3029 }
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; });
3730}
3831
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- }
32+ for (const header in addons) {
33+ let { files } = addons[header];
4834
49- function verifyFiles(files, blockName, onprogress, ondone) {
5035 // must have a .cc and a .js to be a valid test
5136 if (!Object.keys(files).some((name) => /\.cc$/.test(name)) ||
5237 !Object.keys(files).some((name) => /\.js$/.test(name))) {
53- return ;
38+ continue ;
5439 }
5540
56- blockName = blockName
41+ const blockName = header
5742 .toLowerCase()
5843 .replace(/\s/g, '_')
5944 .replace(/[^a-z\d_]/g, '');
@@ -62,29 +47,17 @@ function verifyFiles(files, blockName, onprogress, ondone) {
6247 `${(++id < 10 ? '0' : '') + id}_${blockName}`
6348 );
6449
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- };
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) };
8053 });
8154
8255 files.push({
8356 path: path.resolve(dir, 'binding.gyp'),
8457 content: JSON.stringify({
8558 targets: [
8659 {
87- target_name: 'addon ',
60+ target_name: 'binding ',
8861 defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
8962 sources: files.map(function(file) {
9063 return file.name;
@@ -94,22 +67,34 @@ ${files[name].replace(
9467 })
9568 });
9669
97- fs.mkdir(dir, function() {
98- // Ignore errors
70+ try {
71+ fs.mkdirSync(dir);
72+ } catch (e) {
73+ strictEqual(e.code, 'EEXIST');
74+ }
9975
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);
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+ }
10683
107- if (onprogress)
108- onprogress(file.path);
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+ }
10991
110- if (--waiting === 0)
111- done();
112- });
113- });
114- });
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+ `;
115100}
0 commit comments