Accept a path for exports file

This commit is contained in:
Tim Schaub
2014-04-28 16:04:38 -06:00
parent 3dd5fb88e5
commit fd170eb295
4 changed files with 88 additions and 41 deletions
+1 -1
View File
@@ -221,7 +221,7 @@ def build_ol_all_js(t):
@target('build/exports.js', SRC) @target('build/exports.js', SRC)
def build_exports_js(t): def build_exports_js(t):
t.run('node', 'tasks/generate-exports.js') t.run('node', 'tasks/generate-exports.js', 'build/exports.js')
for glsl_src in GLSL_SRC: for glsl_src in GLSL_SRC:
+2 -1
View File
@@ -22,6 +22,7 @@
"jsdoc": "~3.3.0-alpha5", "jsdoc": "~3.3.0-alpha5",
"walk": "~2.3.1", "walk": "~2.3.1",
"fs-extra": "~0.8.1", "fs-extra": "~0.8.1",
"nomnom": "~1.6.2" "nomnom": "~1.6.2",
"temp": "~0.7.0"
} }
} }
+40 -2
View File
@@ -8,6 +8,7 @@ var async = require('async');
var closure = require('closure-util'); var closure = require('closure-util');
var fse = require('fs-extra'); var fse = require('fs-extra');
var nomnom = require('nomnom'); var nomnom = require('nomnom');
var temp = require('temp').track();
var generateExports = require('./generate-exports'); var generateExports = require('./generate-exports');
@@ -69,14 +70,50 @@ function readConfig(configPath, callback) {
} }
/**
* Write the exports code to a temporary file.
* @param {string} exports Exports code.
* @param {function(Error, string)} callback Called with the path to the temp
* file (or any error).
*/
function writeExports(exports, callback) {
temp.open({prefix: 'exports', suffix: '.js'}, function(err, info) {
if (err) {
callback(err);
return;
}
log.verbose('build', 'Writing exports: ' + info.path);
fs.writeFile(info.path, exports, function(err) {
if (err) {
callback(err);
return;
}
fs.close(info.fd, function(err) {
if (err) {
callback(err);
return;
}
callback(null, info.path);
});
});
});
}
/** /**
* Get the list of sources sorted in dependency order. * Get the list of sources sorted in dependency order.
* @param {Array.<string>} src List of paths or patterns to source files. By * @param {Array.<string>} src List of paths or patterns to source files. By
* default, all .js files in the src directory are included. * default, all .js files in the src directory are included.
* @param {string} exports Exports code (with goog.exportSymbol calls).
* @param {function(Error, Array.<string>)} callback Called with a list of paths * @param {function(Error, Array.<string>)} callback Called with a list of paths
* or any error. * or any error.
*/ */
function getDependencies(src, callback) { function getDependencies(src, exports, callback) {
writeExports(exports, function(err, exportsPath) {
if (err) {
callback(err);
return;
}
log.info('ol', 'Parsing dependencies'); log.info('ol', 'Parsing dependencies');
src = src || ['src/**/*.js']; src = src || ['src/**/*.js'];
closure.getDependencies({lib: src}, function(err, paths) { closure.getDependencies({lib: src}, function(err, paths) {
@@ -84,9 +121,10 @@ function getDependencies(src, callback) {
callback(err); callback(err);
return; return;
} }
paths.push(path.join(root, 'build', 'exports.js')); paths.push(exportsPath);
callback(null, paths); callback(null, paths);
}); });
});
} }
+33 -25
View File
@@ -2,6 +2,8 @@ var fs = require('fs');
var path = require('path'); var path = require('path');
var async = require('async'); var async = require('async');
var fse = require('fs-extra');
var nomnom = require('nomnom');
var generateSymbols = require('./generate-symbols'); var generateSymbols = require('./generate-symbols');
@@ -159,45 +161,52 @@ function generateExports(names) {
/** /**
* Write the build/exports.js file. * Generate the exports code.
* @param {Array.<string>} names List of symbol names.
* @param {function(Error)} callback Callback.
*/
function writeExports(names, callback) {
var code = generateExports(names);
fs.writeFile(path.join(build, 'exports.js'), code, callback);
}
/**
* Generate the build/exports.js file. If the options.config value is provided,
* it is assumed to be a path to a JSON file with an 'exports' member whose
* value is an array of symbol names or patterns.
* *
* @param {Array.<string>} patterns List of symbol names or patterns. * @param {Array.<string>} patterns List of symbol names or patterns.
* @param {function(Error)} callback Callback. * @param {function(Error, string)} callback Called with the exports code or any
* error generating it.
*/ */
function main(patterns, callback) { function main(patterns, callback) {
async.waterfall([ async.waterfall([
getSymbols.bind(null, patterns), getSymbols.bind(null, patterns),
filterSymbols, filterSymbols,
writeExports function(names, done) {
var code, err;
try {
code = generateExports(names);
} catch (e) {
err = e;
}
done(err, code);
}
], callback); ], callback);
} }
/** /**
* If running this module directly, read the config file and call the main * If running this module directly, read the config file, call the main
* function. * function, and write the output file.
*/ */
if (require.main === module) { if (require.main === module) {
var configPath = process.argv[2]; var options = nomnom.options({
getPatterns(configPath, function(err, patterns) { output: {
if (err) { position: 0,
console.error(err.message); required: true,
process.exit(1); help: 'Output file path'
},
config: {
abbr: 'c',
help: 'Path to JSON config file',
metavar: 'CONFIG'
} }
main(patterns, function(err) { }).parse();
async.waterfall([
getPatterns.bind(null, options.config),
main,
fse.outputFile.bind(fse, options.output)
], function(err) {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
process.exit(1); process.exit(1);
@@ -205,7 +214,6 @@ if (require.main === module) {
process.exit(0); process.exit(0);
} }
}); });
});
} }