From a8b6fa413e79c75cb90886831caa40eae5ecea34 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 9 Apr 2013 12:58:17 -0700 Subject: [PATCH] Cleanup build files and add build/util.js. Former-commit-id: 67e5564a17ec4a438e3d0768e8963a1384d4ce98 --- build.js | 32 +++++++-------- build/minify.js | 20 ++++------ build/mkdirp-sync.js | 43 --------------------- build/util.js | 92 ++++++++++++++++++++++++++++++++++++++++++++ test/test-build.js | 18 ++++----- 5 files changed, 125 insertions(+), 80 deletions(-) delete mode 100755 build/mkdirp-sync.js create mode 100755 build/util.js diff --git a/build.js b/build.js index ab1e04f8a..04de9aa3c 100755 --- a/build.js +++ b/build.js @@ -3,17 +3,16 @@ 'use strict'; /** Load Node.js modules */ - var fs = require('fs'), - path = require('path'), - vm = require('vm'); + var vm = require('vm'); /** Load other modules */ - var _ = require(path.join(__dirname, 'lodash.js')), - minify = require(path.join(__dirname, 'build', 'minify.js')), - mkdirpSync = require(path.join(__dirname, 'build', 'mkdirp-sync.js')); + var _ = require('./lodash.js'), + minify = require('./build/minify.js'), + util = require('./build/util.js'); - /** Add `path.sep` for older versions of Node.js */ - path.sep || (path.sep = process.platform == 'win32' ? '\\' : '/'); + /** Module shortcuts */ + var fs = util.fs, + path = util.path; /** The current working directory */ var cwd = process.cwd(); @@ -25,7 +24,7 @@ var push = arrayRef.push; /** Used to detect the Node.js executable in command-line arguments */ - var reNode = RegExp('(?:^|' + path.sep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')node(?:\\.exe)?$'); + var reNode = RegExp('(?:^|' + path.sepEscaped + ')node(?:\\.exe)?$'); /** Shortcut used to convert array-like objects to arrays */ var slice = arrayRef.slice; @@ -1640,7 +1639,7 @@ if (/-o|--output/.test(value)) { result = options[index + 1]; var dirname = path.dirname(result); - mkdirpSync(dirname); + fs.mkdirpSync(dirname); result = path.join(fs.realpathSync(dirname), path.basename(result)); } return result; @@ -1829,12 +1828,6 @@ // load customized Lo-Dash module var lodash = !isTemplate && (function() { - var context = vm.createContext({ - 'clearTimeout': clearTimeout, - 'console': console, - 'setTimeout': setTimeout - }); - source = setUseStrictOption(source, isStrict); if (isLegacy) { @@ -2499,6 +2492,13 @@ .replace(/\beach(?=\(collection)/g, 'forOwn') .replace(/\beach(?=\(\[)/g, 'forEach'); } + + var context = vm.createContext({ + 'clearTimeout': clearTimeout, + 'console': console, + 'setTimeout': setTimeout + }); + vm.runInContext(source, context); return context._; }()); diff --git a/build/minify.js b/build/minify.js index 50e421424..551c42dd4 100755 --- a/build/minify.js +++ b/build/minify.js @@ -3,24 +3,20 @@ 'use strict'; /** Load Node.js modules */ - var fs = require('fs'), - https = require('https'), - path = require('path'), + var https = require('https'), spawn = require('child_process').spawn, zlib = require('zlib'); /** Load other modules */ var _ = require('../lodash.js'), - mkdirpSync = require('./mkdirp-sync.js'), preprocess = require('./pre-compile.js'), postprocess = require('./post-compile.js'), - tar = require('../vendor/tar/tar.js'); + tar = require('../vendor/tar/tar.js'), + util = require('./util.js'); - /** Add `fs.existsSync` for older versions of Node.js */ - fs.existsSync || (fs.existsSync = path.existsSync); - - /** Add `path.sep` for older versions of Node.js */ - path.sep || (path.sep = process.platform == 'win32' ? '\\' : '/'); + /** Module shortcuts */ + var fs = util.fs, + path = util.path; /** The Git object ID of `closure-compiler.tar.gz` */ var closureId = 'a95a8007892aa824ce90c6aa3d3abb0489bf0fff'; @@ -47,7 +43,7 @@ var mediaType = 'application/vnd.github.v3.raw'; /** Used to detect the Node.js executable in command-line arguments */ - var reNode = RegExp('(?:^|' + path.sep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')node(?:\\.exe)?$'); + var reNode = RegExp('(?:^|' + path.sepEscaped + ')node(?:\\.exe)?$'); /** Used to reference parts of the blob href */ var location = (function() { @@ -146,7 +142,7 @@ if (/-o|--output/.test(value)) { result = options[index + 1]; var dirname = path.dirname(result); - mkdirpSync(dirname); + fs.mkdirpSync(dirname); result = path.join(fs.realpathSync(dirname), path.basename(result)); } return result; diff --git a/build/mkdirp-sync.js b/build/mkdirp-sync.js deleted file mode 100755 index 766cbb6a9..000000000 --- a/build/mkdirp-sync.js +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env node -;(function() { - 'use strict'; - - /** Load Node.js modules */ - var fs = require('fs'), - path = require('path'); - - /** Add `path.sep` for older versions of Node.js */ - path.sep || (path.sep = process.platform == 'win32' ? '\\' : '/'); - - /*--------------------------------------------------------------------------*/ - - /** - * Makes the given `dirname` directory, without throwing errors for existing - * directories and making parent directories as needed. - * - * @param {String} dirname The path of the directory. - * @param {Number|String} [mode='0777'] The permission mode. - */ - function mkdirpSync(dirname, mode) { - var sep = path.sep; - - // ensure relative paths are prefixed with `./` - if (!RegExp('^\\.?' + sep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).test(dirname)) { - dirname = '.' + sep + dirname; - } - dirname.split(sep).reduce(function(currPath, segment) { - currPath += sep + segment; - try { - currPath = fs.realpathSync(currPath); - } catch(e) { - fs.mkdirSync(currPath, mode); - } - return currPath; - }); - } - - /*--------------------------------------------------------------------------*/ - - // expose - module.exports = mkdirpSync; -}()); diff --git a/build/util.js b/build/util.js new file mode 100755 index 000000000..d882e9533 --- /dev/null +++ b/build/util.js @@ -0,0 +1,92 @@ +#!/usr/bin/env node +;(function() { + 'use strict'; + + /** Load Node.js modules */ + var fs = require('fs'), + path = require('path'); + + /** Load other modules */ + var _ = require('../lodash.js'); + + /*--------------------------------------------------------------------------*/ + + /** + * The path separator. + * + * @memberOf util.path + * @type String + */ + var sep = path.sep || (process.platform == 'win32' ? '\\' : '/'); + + /** + * The escaped path separator used for inclusion in RegExp strings. + * + * @memberOf util.path + * @type String + */ + var sepEscaped = sep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + + /*--------------------------------------------------------------------------*/ + + /** + * Makes the given `dirname` directory, without throwing errors for existing + * directories and making parent directories as needed. + * + * @memberOf util.fs + * @param {String} dirname The path of the directory. + * @param {Number|String} [mode='0777'] The permission mode. + */ + function mkdirpSync(dirname, mode) { + // ensure relative paths are prefixed with `./` + if (!RegExp('^\\.?' + sepEscaped).test(dirname)) { + dirname = '.' + sep + dirname; + } + dirname.split(sep).reduce(function(currPath, segment) { + currPath += sep + segment; + try { + currPath = fs.realpathSync(currPath); + } catch(e) { + fs.mkdirSync(currPath, mode); + } + return currPath; + }); + } + + /*--------------------------------------------------------------------------*/ + + /** + * The utility object. + * + * @type Object + */ + var util = { + + /** + * The file system object. + * + * @memberOf util + * @type Object + */ + 'fs': _.defaults({}, fs, { + 'existsSync': fs.existsSync || path.existsSync, + 'mkdirpSync': mkdirpSync + }), + + /** + * The path object. + * + * @memberOf util + * @type Object + */ + 'path': _.defaults({}, path, { + 'sep': sep, + 'sepEscaped': sepEscaped + }) + }; + + /*--------------------------------------------------------------------------*/ + + // expose + module.exports = util; +}()); diff --git a/test/test-build.js b/test/test-build.js index 092b840b5..da6d93fa2 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -3,21 +3,21 @@ 'use strict'; /** Load Node.js modules */ - var fs = require('fs'), - path = require('path'), - vm = require('vm'); + var vm = require('vm'); /** Load other modules */ - var build = require('../build.js'), - minify = require('../build/minify'), - _ = require('../lodash.js'); + var _ = require('../lodash.js'), + build = require('../build.js'), + minify = require('../build/minify.js'), + util = require('../build/util.js'); + + /** Module shortcuts */ + var fs = util.fs, + path = util.path; /** Used to avoid `noglobal` false positives caused by `errno` leaked in Node.js */ global.errno = true; - /** Add `path.sep` for older versions of Node.js */ - path.sep || (path.sep = process.platform == 'win32' ? '\\' : '/'); - /** The current working directory */ var cwd = process.cwd();