node/tools/doc/addon-verify.js

123 lines
3.3 KiB
JavaScript

'use strict';
const { strictEqual } = require('assert');
const fs = require('fs');
const path = require('path');
const marked = require('marked');
const auto = 'Auto-generated by `node tools/doc/addon-verify.js`';
const rootDir = path.resolve(__dirname, '..', '..');
const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md');
const verifyDir = path.resolve(rootDir, 'test', 'addons');
const changed = [];
const checkOnly = process.argv.includes('--check');
let id = 0;
let currentHeader;
const addons = {};
const content = fs.readFileSync(doc, 'utf8');
for (const { text, type } of marked.lexer(content)) {
if (type === 'heading' && text) {
currentHeader = text;
addons[currentHeader] = {
files: {}
};
}
if (type === 'code') {
const match = text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
if (match !== null) {
addons[currentHeader].files[match[1]] = text;
}
}
}
for (const header in addons) {
let { files } = addons[header];
// 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))) {
continue;
}
const blockName = header
.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^a-z\d_]/g, '');
const dir = path.resolve(
verifyDir,
`${(++id < 10 ? '0' : '') + id}_${blockName}`
);
files = Object.entries(files).map(([name, content]) => {
if (name === 'test.js') content = boilerplate(name, content);
content = `// ${auto}\n${content}`;
if (name.endsWith('.h')) {
content = content.replace(/(#(ifndef|define) \w+_H)/g,
'$1 // NOLINT(build/header_guard)');
content = content.replace(/(#endif)$/,
'$1 // NOLINT(build/header_guard)');
}
if (!content.endsWith('\n')) content += '\n'; // Pacify linter.
return { name, content, path: path.resolve(dir, name) };
});
files.push({
path: path.resolve(dir, 'binding.gyp'),
content: `# ${auto}\n` + JSON.stringify({
targets: [
{
target_name: 'binding',
defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
sources: files.map(function(file) {
return file.name;
})
}
]
})
});
for (const file of files) {
let content;
try {
content = fs.readFileSync(file.path, 'utf8');
} catch (e) {
strictEqual(e.code, 'ENOENT');
}
// Only update when file content has changed to prevent unneeded rebuilds.
if (content === file.content) continue;
changed.push(file);
if (checkOnly) continue;
try {
fs.mkdirSync(dir);
} catch (e) {
strictEqual(e.code, 'EEXIST');
}
fs.writeFileSync(file.path, file.content);
console.log('wrote', file.path);
}
}
if (checkOnly && changed.length > 0) {
console.error('The following files are out of date:');
for (const { path } of changed) console.error(' ', path);
console.error('Run `node tools/doc/addon-verify.js` to update.');
process.exit(1);
}
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`')}
`;
}