From 5ca2da76df0d1ecf869b8a1fd4e8ad7a15d39d72 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 11 Jul 2012 05:39:30 -0400 Subject: [PATCH] Make build.js work as a npm executable. Former-commit-id: fff327957854aa85a5abfe80994b59f3d0d24370 --- build.js | 9 ++++++--- build/minify.js | 32 +++++++++++++++++++------------- build/post-compile.js | 2 +- build/pre-compile.js | 4 ++-- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/build.js b/build.js index 45510c5e8..e8715ae5a 100755 --- a/build.js +++ b/build.js @@ -8,6 +8,9 @@ vm = require('vm'), minify = require(path.join(__dirname, 'build', 'minify')); + /** The current working directory */ + var cwd = process.cwd(); + /** Flag used to specify a backbone build */ var isBackbone = process.argv.indexOf('backbone') > -1; @@ -871,14 +874,14 @@ // begin the minification process if (filterType || isBackbone || isLegacy || isMobile) { - fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source); + fs.writeFileSync(path.join(cwd, 'lodash.custom.js'), source); minify(source, 'lodash.custom.min', function(result) { - fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result); + fs.writeFileSync(path.join(cwd, 'lodash.custom.min.js'), result); }); } else { minify(source, 'lodash.min', function(result) { - fs.writeFileSync(path.join(__dirname, 'lodash.min.js'), result); + fs.writeFileSync(path.join(cwd, 'lodash.min.js'), result); }); } }()); diff --git a/build/minify.js b/build/minify.js index 39efec5e0..95f691311 100755 --- a/build/minify.js +++ b/build/minify.js @@ -35,8 +35,8 @@ /*--------------------------------------------------------------------------*/ /** - * The exposed `minify` function minifies a given `source` and invokes the - * `onComplete` callback when finished. + * The exposed `minify` function minifies a given Lo-Dash `source` and invokes + * the `onComplete` callback when finished. * * @param {String} source The source to minify. * @param {String} workingName The name to give temporary files creates during the minification process. @@ -58,7 +58,10 @@ function Minify(source, workingName, onComplete) { // create the destination directory if it doesn't exist if (!fs.existsSync(distPath)) { - fs.mkdirSync(distPath); + // avoid errors when called as a npm executable + try { + fs.mkdirSync(distPath); + } catch(e) { } } this.compiled = {}; @@ -291,17 +294,20 @@ name = this.workingName, uglified = this.uglified; - // save the Closure Compiled version to disk - fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source); - fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip); + // avoid errors when called as a npm executable + try { + // save the Closure Compiled version to disk + fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source); + fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip); - // save the Uglified version to disk - fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source); - fs.writeFileSync(path.join(distPath, name + '.uglify.js.gz'), uglified.gzip); + // save the Uglified version to disk + fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source); + fs.writeFileSync(path.join(distPath, name + '.uglify.js.gz'), uglified.gzip); - // save the hybrid minified version to disk - fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source); - fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip); + // save the hybrid minified version to disk + fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source); + fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip); + } catch(e) { } // select the smallest gzipped file and use its minified counterpart as the // official minified release (ties go to Closure Compiler) @@ -324,7 +330,7 @@ module.exports = minify; } else { - // read the JavaScript source file from the first argument if the script + // read the Lo-Dash source file from the first argument if the script // was invoked directly (e.g. `node minify.js source.js`) and write to // the same file (function() { diff --git a/build/post-compile.js b/build/post-compile.js index 544318ea4..915d39f7d 100644 --- a/build/post-compile.js +++ b/build/post-compile.js @@ -56,7 +56,7 @@ if (module != require.main) { module.exports = postprocess; } else { - // read the JavaScript source file from the first argument if the script + // read the Lo-Dash source file from the first argument if the script // was invoked directly (e.g. `node post-compile.js source.js`) and write to // the same file (function() { diff --git a/build/pre-compile.js b/build/pre-compile.js index bbc57dcf4..5736212e7 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -196,7 +196,7 @@ /*--------------------------------------------------------------------------*/ /** - * Pre-process a given Lo-Dash source, preparing it for minification. + * Pre-process a given Lo-Dash `source`, preparing it for minification. * * @param {String} source The source to process. * @returns {String} Returns the processed source. @@ -361,7 +361,7 @@ module.exports = preprocess; } else { - // read the JavaScript source file from the first argument if the script + // read the Lo-Dash source file from the first argument if the script // was invoked directly (e.g. `node pre-compile.js source.js`) and write to // the same file (function() {