0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/doc/addon-verify.js
Vse Mozhet Byt 6946812191 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: https://github.com/nodejs/node/pull/20188
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-04-24 10:30:21 +03:00

92 lines
2.2 KiB
JavaScript

'use strict';
const { mkdir, readFileSync, writeFile } = require('fs');
const { resolve } = require('path');
const { lexer } = require('marked');
const rootDir = resolve(__dirname, '..', '..');
const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
const verifyDir = resolve(rootDir, 'test', 'addons');
const tokens = lexer(readFileSync(doc, 'utf8'));
const addons = {};
let id = 0;
let currentHeader;
const validNames = /^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/;
tokens.forEach(({ type, text }) => {
if (type === 'heading') {
currentHeader = text;
addons[currentHeader] = { files: {} };
}
if (type === 'code') {
const match = text.match(validNames);
if (match !== null) {
addons[currentHeader].files[match[1]] = text;
}
}
});
Object.keys(addons).forEach((header) => {
verifyFiles(addons[header].files, header);
});
function verifyFiles(files, blockName) {
const fileNames = Object.keys(files);
// Must have a .cc and a .js to be a valid test.
if (!fileNames.some((name) => name.endsWith('.cc')) ||
!fileNames.some((name) => name.endsWith('.js'))) {
return;
}
blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
const dir = resolve(
verifyDir,
`${String(++id).padStart(2, '0')}_${blockName}`
);
files = fileNames.map((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: resolve(dir, name),
name: name,
content: files[name]
};
});
files.push({
path: resolve(dir, 'binding.gyp'),
content: JSON.stringify({
targets: [
{
target_name: 'addon',
defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
sources: files.map(({ name }) => name)
}
]
})
});
mkdir(dir, () => {
// Ignore errors.
files.forEach(({ path, content }) => {
writeFile(path, content, (err) => {
if (err)
throw err;
console.log(`Wrote ${path}`);
});
});
});
}