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

View File

@@ -221,7 +221,7 @@ def build_ol_all_js(t):
@target('build/exports.js', SRC)
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:

View File

@@ -22,6 +22,7 @@
"jsdoc": "~3.3.0-alpha5",
"walk": "~2.3.1",
"fs-extra": "~0.8.1",
"nomnom": "~1.6.2"
"nomnom": "~1.6.2",
"temp": "~0.7.0"
}
}

View File

@@ -8,6 +8,7 @@ var async = require('async');
var closure = require('closure-util');
var fse = require('fs-extra');
var nomnom = require('nomnom');
var temp = require('temp').track();
var generateExports = require('./generate-exports');
@@ -70,22 +71,59 @@ function readConfig(configPath, callback) {
/**
* Get the list of sources sorted in dependency order.
* @param {Array.<string>} src List of paths or patterns to source files. By
* default, all .js files in the src directory are included.
* @param {function(Error, Array.<string>)} callback Called with a list of paths
* or any error.
* 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 getDependencies(src, callback) {
log.info('ol', 'Parsing dependencies');
src = src || ['src/**/*.js'];
closure.getDependencies({lib: src}, function(err, paths) {
function writeExports(exports, callback) {
temp.open({prefix: 'exports', suffix: '.js'}, function(err, info) {
if (err) {
callback(err);
return;
}
paths.push(path.join(root, 'build', 'exports.js'));
callback(null, paths);
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.
* @param {Array.<string>} src List of paths or patterns to source files. By
* 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
* or any error.
*/
function getDependencies(src, exports, callback) {
writeExports(exports, function(err, exportsPath) {
if (err) {
callback(err);
return;
}
log.info('ol', 'Parsing dependencies');
src = src || ['src/**/*.js'];
closure.getDependencies({lib: src}, function(err, paths) {
if (err) {
callback(err);
return;
}
paths.push(exportsPath);
callback(null, paths);
});
});
}

View File

@@ -2,6 +2,8 @@ var fs = require('fs');
var path = require('path');
var async = require('async');
var fse = require('fs-extra');
var nomnom = require('nomnom');
var generateSymbols = require('./generate-symbols');
@@ -159,52 +161,58 @@ function generateExports(names) {
/**
* Write the build/exports.js file.
* @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.
* Generate the exports code.
*
* @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) {
async.waterfall([
getSymbols.bind(null, patterns),
filterSymbols,
writeExports
function(names, done) {
var code, err;
try {
code = generateExports(names);
} catch (e) {
err = e;
}
done(err, code);
}
], callback);
}
/**
* If running this module directly, read the config file and call the main
* function.
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (require.main === module) {
var configPath = process.argv[2];
getPatterns(configPath, function(err, patterns) {
var options = nomnom.options({
output: {
position: 0,
required: true,
help: 'Output file path'
},
config: {
abbr: 'c',
help: 'Path to JSON config file',
metavar: 'CONFIG'
}
}).parse();
async.waterfall([
getPatterns.bind(null, options.config),
main,
fse.outputFile.bind(fse, options.output)
], function(err) {
if (err) {
console.error(err.message);
process.exit(1);
} else {
process.exit(0);
}
main(patterns, function(err) {
if (err) {
console.error(err.message);
process.exit(1);
} else {
process.exit(0);
}
});
});
}