2014-01-18 23:49:18 +01:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var marked = require('marked');
|
|
|
|
|
|
|
|
var doc = path.resolve(__dirname, '..', '..', 'doc', 'api', 'addons.markdown');
|
|
|
|
var verifyDir = path.resolve(__dirname, '..', '..', 'test', 'addons');
|
|
|
|
|
|
|
|
var contents = fs.readFileSync(doc).toString();
|
|
|
|
|
|
|
|
var tokens = marked.lexer(contents, {});
|
|
|
|
var files = null;
|
|
|
|
var id = 0;
|
|
|
|
|
|
|
|
// Just to make sure that all examples will be processed
|
|
|
|
tokens.push({ type: 'heading' });
|
|
|
|
|
|
|
|
var oldDirs = fs.readdirSync(verifyDir);
|
|
|
|
oldDirs = oldDirs.filter(function(dir) {
|
|
|
|
return /^doc-/.test(dir);
|
|
|
|
}).map(function(dir) {
|
|
|
|
return path.resolve(verifyDir, dir);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
var token = tokens[i];
|
|
|
|
if (token.type === 'heading') {
|
|
|
|
if (files && Object.keys(files).length !== 0) {
|
2015-08-20 16:57:03 +02:00
|
|
|
verifyFiles(files,
|
|
|
|
console.log.bind(null, 'wrote'),
|
|
|
|
function(err) { if (err) throw err; });
|
2014-01-18 23:49:18 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:57:03 +02:00
|
|
|
function verifyFiles(files, onprogress, ondone) {
|
2014-01-18 23:49:18 +01:00
|
|
|
var dir = path.resolve(verifyDir, 'doc-' + id++);
|
|
|
|
|
|
|
|
files = Object.keys(files).map(function(name) {
|
|
|
|
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',
|
|
|
|
sources: files.map(function(file) {
|
|
|
|
return file.name;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.mkdir(dir, function() {
|
|
|
|
// Ignore errors
|
|
|
|
|
2015-08-20 16:57:03 +02:00
|
|
|
var done = once(ondone);
|
2014-01-18 23:49:18 +01:00
|
|
|
var waiting = files.length;
|
2015-08-20 16:57:03 +02:00
|
|
|
files.forEach(function(file) {
|
|
|
|
fs.writeFile(file.path, file.content, function(err) {
|
|
|
|
if (err)
|
|
|
|
return done(err);
|
2014-01-18 23:49:18 +01:00
|
|
|
|
2015-08-20 16:57:03 +02:00
|
|
|
if (onprogress)
|
|
|
|
onprogress(file.path);
|
2014-01-18 23:49:18 +01:00
|
|
|
|
2015-08-20 16:57:03 +02:00
|
|
|
if (--waiting === 0)
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-18 23:49:18 +01:00
|
|
|
});
|
|
|
|
}
|