Allow to provide externs for inclusion in info.json

This commit is contained in:
Andreas Hocevar
2014-08-07 09:12:07 +02:00
parent 9575396aad
commit e8eebd8846
3 changed files with 53 additions and 34 deletions

View File

@@ -22,15 +22,15 @@ exports.publish = function(data, opts) {
return types; return types;
} }
var cwd = process.cwd();
// get all doclets with the "api" property or define (excluding events) or // get all doclets with the "api" property or define (excluding events) or
// with olx namespace // with olx namespace
var docs = data( var docs = data(
[ [
{define: {isObject: true}}, {define: {isObject: true}},
{api: {isString: true}}, {api: {isString: true}},
{longname: {left: 'olx.'}} function() {
return this.meta && (/[\\\/]externs$/).test(this.meta.path);
}
], ],
{kind: {'!is': 'event'}}).get(); {kind: {'!is': 'event'}}).get();
@@ -38,6 +38,7 @@ exports.publish = function(data, opts) {
var symbols = []; var symbols = [];
var defines = []; var defines = [];
var typedefs = []; var typedefs = [];
var externs = [];
docs.filter(function(doc) { docs.filter(function(doc) {
var include = true; var include = true;
var constructor = doc.memberof; var constructor = doc.memberof;
@@ -113,7 +114,8 @@ exports.publish = function(data, opts) {
return true; return true;
}); });
} }
symbols.push(symbol); var target = (/[\\\/]externs$/).test(doc.meta.path) ? externs : symbols;
target.push(symbol);
} }
}); });
@@ -121,7 +123,8 @@ exports.publish = function(data, opts) {
JSON.stringify({ JSON.stringify({
symbols: symbols, symbols: symbols,
defines: defines, defines: defines,
typedefs: typedefs typedefs: typedefs,
externs: externs
}, null, 2)); }, null, 2));
}; };

View File

@@ -7,15 +7,12 @@ var nomnom = require('nomnom');
var generateInfo = require('./generate-info'); var generateInfo = require('./generate-info');
var olxPath = path.join(__dirname, '..', 'externs', 'olx.js');
/** /**
* Read the symbols from info file. * Read the symbols from info file.
* @param {funciton(Error, Array.<string>, Array.<Object>)} callback Called * @param {funciton(Error, Array.<string>, Array.<Object>)} callback Called
* with the patterns and symbols (or any error). * with the patterns and symbols (or any error).
*/ */
function getTypedefsAndSymbols(callback) { function getInfo(callback) {
generateInfo(function(err) { generateInfo(function(err) {
if (err) { if (err) {
callback(new Error('Trouble generating info: ' + err.message)); callback(new Error('Trouble generating info: ' + err.message));
@@ -23,7 +20,8 @@ function getTypedefsAndSymbols(callback) {
} }
var typedefs = require('../build/info.json').typedefs; var typedefs = require('../build/info.json').typedefs;
var symbols = require('../build/info.json').symbols; var symbols = require('../build/info.json').symbols;
callback(null, typedefs, symbols); var externs = require('../build/info.json').externs;
callback(null, typedefs, symbols, externs);
}); });
} }
@@ -32,10 +30,11 @@ function getTypedefsAndSymbols(callback) {
* Generate externs code given a list symbols. * Generate externs code given a list symbols.
* @param {Array.<Object>} typedefs List of typedefs. * @param {Array.<Object>} typedefs List of typedefs.
* @param {Array.<Object>} symbols List of symbols. * @param {Array.<Object>} symbols List of symbols.
* @param {Array.<Object>} externs List of externs.
* @param {string|undefined} namespace Target object for exported symbols. * @param {string|undefined} namespace Target object for exported symbols.
* @return {string} Export code. * @return {string} Export code.
*/ */
function generateExterns(typedefs, symbols) { function generateExterns(typedefs, symbols, externs) {
var lines = []; var lines = [];
var namespaces = {}; var namespaces = {};
var constructors = {}; var constructors = {};
@@ -60,16 +59,7 @@ function generateExterns(typedefs, symbols) {
}); });
} }
typedefs.forEach(function(typedef) { function processSymbol(symbol) {
addNamespaces(typedef.name);
lines.push('/**');
lines.push(' * @typedef {' + typedef.types.join('|') + '}');
lines.push(' */');
lines.push(typedef.name + ';');
lines.push('\n');
});
symbols.forEach(function(symbol) {
addNamespaces(symbol.name.split('#')[0]); addNamespaces(symbol.name.split('#')[0]);
var name = symbol.name; var name = symbol.name;
@@ -118,7 +108,19 @@ function generateExterns(typedefs, symbols) {
lines.push(name + ';'); lines.push(name + ';');
} }
lines.push('\n'); lines.push('\n');
}
typedefs.forEach(function(typedef) {
addNamespaces(typedef.name);
lines.push('/**');
lines.push(' * @typedef {' + typedef.types.join('|') + '}');
lines.push(' */');
lines.push(typedef.name + ';');
lines.push('\n');
}); });
externs.forEach(processSymbol);
symbols.forEach(processSymbol);
return lines.join('\n'); return lines.join('\n');
} }
@@ -132,11 +134,11 @@ function generateExterns(typedefs, symbols) {
*/ */
function main(callback) { function main(callback) {
async.waterfall([ async.waterfall([
getTypedefsAndSymbols, getInfo,
function(typedefs, symbols, done) { function(typedefs, symbols, externs, done) {
var code, err; var code, err;
try { try {
code = generateExterns(typedefs, symbols); code = generateExterns(typedefs, symbols, externs);
} catch (e) { } catch (e) {
err = e; err = e;
} }

View File

@@ -7,7 +7,11 @@ var fse = require('fs-extra');
var walk = require('walk').walk; var walk = require('walk').walk;
var sourceDir = path.join(__dirname, '..', 'src'); var sourceDir = path.join(__dirname, '..', 'src');
var olxPath = path.join(__dirname, '..', 'externs', 'olx.js'); var externsDir = path.join(__dirname, '..', 'externs');
var externsPaths = [
path.join(externsDir, 'olx.js'),
path.join(externsDir, 'geojson.js')
];
var infoPath = path.join(__dirname, '..', 'build', 'info.json'); var infoPath = path.join(__dirname, '..', 'build', 'info.json');
var jsdoc = path.join(__dirname, '..', 'node_modules', '.bin', 'jsdoc'); var jsdoc = path.join(__dirname, '..', 'node_modules', '.bin', 'jsdoc');
var jsdocConfig = path.join( var jsdocConfig = path.join(
@@ -41,13 +45,23 @@ function getInfoTime(callback) {
* error, the mtime of the info file (zero date if it doesn't exist), and * error, the mtime of the info file (zero date if it doesn't exist), and
* whether externs/olx.js is newer than that date. * whether externs/olx.js is newer than that date.
*/ */
function getOlxNewer(date, callback) { function getNewerExterns(date, callback) {
fs.stat(olxPath, function(err, stats) { var newer = false;
if (err) { var walker = walk(externsDir);
callback(new Error('Trouble reading ' + olxPath)); walker.on('file', function(root, stats, next) {
} else { var sourcePath = path.join(root, stats.name);
callback(null, date, stats.mtime > date); externsPaths.forEach(function(path) {
} if (sourcePath == path && stats.mtime > date) {
newer = true;
}
});
next();
});
walker.on('errors', function() {
callback(new Error('Trouble walking ' + sourceDir));
});
walker.on('end', function() {
callback(null, date, newer);
}); });
} }
@@ -61,7 +75,7 @@ function getOlxNewer(date, callback) {
* error and the array of source paths (empty if none newer). * error and the array of source paths (empty if none newer).
*/ */
function getNewer(date, newer, callback) { function getNewer(date, newer, callback) {
var paths = [olxPath]; var paths = [].concat(externsPaths);
var walker = walk(sourceDir); var walker = walk(sourceDir);
walker.on('file', function(root, stats, next) { walker.on('file', function(root, stats, next) {
@@ -238,7 +252,7 @@ function writeInfo(info, callback) {
function main(callback) { function main(callback) {
async.waterfall([ async.waterfall([
getInfoTime, getInfoTime,
getOlxNewer, getNewerExterns,
getNewer, getNewer,
spawnJSDoc, spawnJSDoc,
addSymbolProvides, addSymbolProvides,