From fd170eb2957cd20b5244a9a87c77dc498507dca3 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 28 Apr 2014 16:04:38 -0600 Subject: [PATCH] Accept a path for exports file --- build.py | 2 +- package.json | 3 +- tasks/build.js | 60 +++++++++++++++++++++++++++++------- tasks/generate-exports.js | 64 ++++++++++++++++++++++----------------- 4 files changed, 88 insertions(+), 41 deletions(-) diff --git a/build.py b/build.py index a97279c1a0..3ad90a04eb 100755 --- a/build.py +++ b/build.py @@ -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: diff --git a/package.json b/package.json index 1fe77d0590..c45c4e44c4 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/tasks/build.js b/tasks/build.js index 7fd85f0666..e778bac0c5 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -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.} src List of paths or patterns to source files. By - * default, all .js files in the src directory are included. - * @param {function(Error, Array.)} 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.} 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.)} 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); + }); }); } diff --git a/tasks/generate-exports.js b/tasks/generate-exports.js index 7134f3b1a8..e6a7ed8745 100644 --- a/tasks/generate-exports.js +++ b/tasks/generate-exports.js @@ -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.} 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.} 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); - } - }); }); }