Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: modernize and optimize doc/addon-verify.js #20188

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 37 additions & 61 deletions tools/doc/addon-verify.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,52 @@
'use strict';

const fs = require('fs');
const path = require('path');
const marked = require('marked');
const { mkdir, readFileSync, writeFile } = require('fs');
const { resolve } = require('path');
const { lexer } = require('marked');

const rootDir = path.resolve(__dirname, '..', '..');
const doc = path.resolve(rootDir, 'doc', 'api', 'addons.md');
const verifyDir = path.resolve(rootDir, 'test', 'addons');
const rootDir = resolve(__dirname, '..', '..');
const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
const verifyDir = resolve(rootDir, 'test', 'addons');

const contents = fs.readFileSync(doc).toString();

const tokens = marked.lexer(contents);
const tokens = lexer(readFileSync(doc, 'utf8'));
const addons = {};
let id = 0;

let currentHeader;
const addons = {};
tokens.forEach((token) => {
if (token.type === 'heading' && token.text) {
currentHeader = token.text;
addons[currentHeader] = {
files: {}
};

const validNames = /^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/;
tokens.forEach(({ type, text }) => {
if (type === 'heading') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the check for text required here?

Copy link
Contributor Author

@vsemozhetbyt vsemozhetbyt Apr 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I tested correctly, there cannot be empty headings: they are not recognized as headings by marked:

const marked = require('marked ');

const md = `
# heading

text

#

text
`;

console.log(marked.lexer(md));
[ { type: 'heading', depth: 1, text: 'heading' },
  { type: 'paragraph', text: 'text' },
  { type: 'paragraph', text: '#' },
  { type: 'paragraph', text: 'text' },
  links: {} ]

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(validNames);
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);
};
}
Object.keys(addons).forEach((header) => {
verifyFiles(addons[header].files, header);
});

function verifyFiles(files, blockName) {
const fileNames = Object.keys(files);

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))) {
if (!fileNames.some((name) => name.endsWith('.cc')) ||
!fileNames.some((name) => name.endsWith('.js'))) {
return;
}

blockName = blockName
.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^a-z\d_]/g, '');
const dir = path.resolve(
blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
const dir = resolve(
verifyDir,
`${(++id < 10 ? '0' : '') + id}_${blockName}`
`${String(++id).padStart(2, '0')}_${blockName}`
);

files = Object.keys(files).map(function(name) {
files = fileNames.map((name) => {
if (name === 'test.js') {
files[name] = `'use strict';
const common = require('../../common');
Expand All @@ -73,42 +57,34 @@ ${files[name].replace(
`;
}
return {
path: path.resolve(dir, name),
path: resolve(dir, name),
name: name,
content: files[name]
};
});

files.push({
path: path.resolve(dir, 'binding.gyp'),
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;
})
sources: files.map(({ name }) => name)
}
]
})
});

fs.mkdir(dir, function() {
mkdir(dir, () => {
// Ignore errors.

const done = once(ondone);
var waiting = files.length;
files.forEach(function(file) {
fs.writeFile(file.path, file.content, function(err) {
files.forEach(({ path, content }) => {
writeFile(path, content, (err) => {
if (err)
return done(err);

if (onprogress)
onprogress(file.path);
throw err;

if (--waiting === 0)
done();
console.log(`Wrote ${path}`);
});
});
});
Expand Down