0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/tools/doc/addon-verify.js
Rich Trott 6a1b8135d6 tools: generate template literal for addon tests
Instead of generating string concatenation, generate a template literal.
This is mostly useful as a pre-emptive measure for avoiding problems
when (if?) we enable the prefer-template lint rule in the test
directory.

PR-URL: https://github.com/nodejs/node/pull/14094
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-07-07 15:22:35 -07:00

116 lines
2.7 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const marked = require('marked');
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 files = null;
let id = 0;
// Just to make sure that all examples will be processed
tokens.push({ type: 'heading' });
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type === 'heading' && token.text) {
const blockName = token.text;
if (files && Object.keys(files).length !== 0) {
verifyFiles(files,
blockName,
console.log.bind(null, 'wrote'),
function(err) { if (err) throw err; });
}
files = {};
} else if (token.type === 'code') {
var match = token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
if (match === null)
continue;
files[match[1]] = token.text;
}
}
function once(fn) {
var once = false;
return function() {
if (once)
return;
once = true;
fn.apply(this, arguments);
};
}
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;
}
blockName = blockName
.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^a-z\d_]/g, '');
const dir = path.resolve(
verifyDir,
`${(++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.push({
path: path.resolve(dir, 'binding.gyp'),
content: JSON.stringify({
targets: [
{
target_name: 'addon',
defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
sources: files.map(function(file) {
return file.name;
})
}
]
})
});
fs.mkdir(dir, function() {
// Ignore errors
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);
if (onprogress)
onprogress(file.path);
if (--waiting === 0)
done();
});
});
});
}