From c6682636be4955e2181c00e7d4868fbf6682a5c5 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 20 Jan 2018 01:00:34 +0100 Subject: [PATCH] tools: simplify tools/doc/addon-verify.js Make the script synchronous and clean it up. This is preparatory work for follow-ups commits. PR-URL: https://github.com/nodejs/node/pull/17407 Reviewed-By: Richard Lau --- tools/doc/addon-verify.js | 103 ++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 59 deletions(-) diff --git a/tools/doc/addon-verify.js b/tools/doc/addon-verify.js index 2e72abb77f925f..4da99d64d72da1 100644 --- a/tools/doc/addon-verify.js +++ b/tools/doc/addon-verify.js @@ -1,5 +1,6 @@ 'use strict'; +const { strictEqual } = require('assert'); const fs = require('fs'); const path = require('path'); const marked = require('marked'); @@ -8,52 +9,36 @@ const rootDir = path.resolve(__dirname, '..', '..'); const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md'); const verifyDir = path.resolve(rootDir, 'test', 'addons'); -const contents = fs.readFileSync(doc).toString(); - -const tokens = marked.lexer(contents); let id = 0; - let currentHeader; + const addons = {}; -tokens.forEach((token) => { - if (token.type === 'heading' && token.text) { - currentHeader = token.text; +const content = fs.readFileSync(doc, 'utf8'); +for (const { text, type } of marked.lexer(content)) { + if (type === 'heading' && text) { + currentHeader = text; addons[currentHeader] = { files: {} }; } - if (token.type === 'code') { - var match = token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/); + if (type === 'code') { + const match = text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/); if (match !== null) { - addons[currentHeader].files[match[1]] = token.text; + addons[currentHeader].files[match[1]] = text; } } -}); -for (var header in addons) { - verifyFiles(addons[header].files, - header, - console.log.bind(null, 'wrote'), - function(err) { if (err) throw err; }); } -function once(fn) { - var once = false; - return function() { - if (once) - return; - once = true; - fn.apply(this, arguments); - }; -} +for (const header in addons) { + let { files } = addons[header]; -function verifyFiles(files, blockName, onprogress, ondone) { // must have a .cc and a .js to be a valid test if (!Object.keys(files).some((name) => /\.cc$/.test(name)) || !Object.keys(files).some((name) => /\.js$/.test(name))) { - return; + continue; } - blockName = blockName + const blockName = header .toLowerCase() .replace(/\s/g, '_') .replace(/[^a-z\d_]/g, ''); @@ -62,21 +47,9 @@ function verifyFiles(files, blockName, onprogress, ondone) { `${(++id < 10 ? '0' : '') + id}_${blockName}` ); - files = Object.keys(files).map(function(name) { - if (name === 'test.js') { - files[name] = `'use strict'; -const common = require('../../common'); -${files[name].replace( - "'./build/Release/addon'", - // eslint-disable-next-line no-template-curly-in-string - '`./build/${common.buildType}/addon`')} -`; - } - return { - path: path.resolve(dir, name), - name: name, - content: files[name] - }; + files = Object.entries(files).map(([name, content]) => { + if (name === 'test.js') content = boilerplate(name, content); + return { name, content, path: path.resolve(dir, name) }; }); files.push({ @@ -84,7 +57,7 @@ ${files[name].replace( content: JSON.stringify({ targets: [ { - target_name: 'addon', + target_name: 'binding', defines: [ 'V8_DEPRECATION_WARNINGS=1' ], sources: files.map(function(file) { return file.name; @@ -94,22 +67,34 @@ ${files[name].replace( }) }); - fs.mkdir(dir, function() { - // Ignore errors + try { + fs.mkdirSync(dir); + } catch (e) { + strictEqual(e.code, 'EEXIST'); + } - const done = once(ondone); - var waiting = files.length; - files.forEach(function(file) { - fs.writeFile(file.path, file.content, function(err) { - if (err) - return done(err); + for (const file of files) { + let content; + try { + content = fs.readFileSync(file.path, 'utf8'); + } catch (e) { + strictEqual(e.code, 'ENOENT'); + } - if (onprogress) - onprogress(file.path); + // Only update when file content has changed to prevent unneeded rebuilds. + if (content !== file.content) { + fs.writeFileSync(file.path, file.content); + console.log('wrote', file.path); + } + } +} - if (--waiting === 0) - done(); - }); - }); - }); +function boilerplate(name, content) { + return `'use strict'; +const common = require('../../common'); +${content.replace( + "'./build/Release/binding'", + // eslint-disable-next-line no-template-curly-in-string + '`./build/${common.buildType}/binding`')} +`; }