Move mkdir -p functionality to its own module and cleanup --output build tests.

Former-commit-id: b7ea2a41c60357e780e10bd3d665db3d50e7f044
This commit is contained in:
John-David Dalton
2013-02-24 01:24:47 -08:00
parent afbe5aa540
commit 6c811a3261
4 changed files with 80 additions and 27 deletions

View File

@@ -2,12 +2,15 @@
;(function() { ;(function() {
'use strict'; 'use strict';
/** Load modules */ /** Load Node.js modules */
var fs = require('fs'), var fs = require('fs'),
path = require('path'), path = require('path'),
vm = require('vm'), vm = require('vm');
/** Load other modules */
var _ = require(path.join(__dirname, 'lodash.js')),
minify = require(path.join(__dirname, 'build', 'minify.js')), minify = require(path.join(__dirname, 'build', 'minify.js')),
_ = require(path.join(__dirname, 'lodash.js')); mkdirpSync = require(path.join(__dirname, 'build', 'mkdirp-sync.js'));
/** The current working directory */ /** The current working directory */
var cwd = process.cwd(); var cwd = process.cwd();
@@ -1513,15 +1516,9 @@
var outputPath = options.reduce(function(result, value, index) { var outputPath = options.reduce(function(result, value, index) {
if (/-o|--output/.test(value)) { if (/-o|--output/.test(value)) {
result = options[index + 1]; result = options[index + 1];
var cwd = process.cwd(); var dirname = path.dirname(result);
path.dirname(result).split(path.sep).forEach(function(pathSegment){ mkdirpSync(dirname);
try { result = path.join(fs.realpathSync(dirname), path.basename(result));
fs.mkdirSync(pathSegment);
} catch(err){}
process.chdir(pathSegment);
});
process.chdir(cwd);
result = path.join(fs.realpathSync(path.dirname(result)), path.basename(result));
} }
return result; return result;
}, ''); }, '');

View File

@@ -2,18 +2,19 @@
;(function() { ;(function() {
'use strict'; 'use strict';
/** Load modules */ /** Load Node.js modules */
var fs = require('fs'), var fs = require('fs'),
https = require('https'), https = require('https'),
path = require('path'), path = require('path'),
spawn = require('child_process').spawn, spawn = require('child_process').spawn,
zlib = require('zlib'), zlib = require('zlib');
tar = require('../vendor/tar/tar.js'),
_ = require('../lodash.js');
/** Load other modules */ /** Load other modules */
var preprocess = require('./pre-compile.js'), var _ = require('../lodash.js'),
postprocess = require('./post-compile.js'); mkdirpSync = require('./mkdirp-sync.js'),
preprocess = require('./pre-compile.js'),
postprocess = require('./post-compile.js'),
tar = require('../vendor/tar/tar.js');
/** The Git object ID of `closure-compiler.tar.gz` */ /** The Git object ID of `closure-compiler.tar.gz` */
var closureId = '23cf67d0f0b979d97631fc108a2a43bb82225994'; var closureId = '23cf67d0f0b979d97631fc108a2a43bb82225994';
@@ -141,7 +142,9 @@
outputPath = options.reduce(function(result, value, index) { outputPath = options.reduce(function(result, value, index) {
if (/-o|--output/.test(value)) { if (/-o|--output/.test(value)) {
result = options[index + 1]; result = options[index + 1];
result = path.join(fs.realpathSync(path.dirname(result)), path.basename(result)); var dirname = path.dirname(result);
mkdirpSync(dirname);
result = path.join(fs.realpathSync(dirname), path.basename(result));
} }
return result; return result;
}, outputPath); }, outputPath);

41
build/mkdirp-sync.js Executable file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env node
;(function() {
'use strict';
/** Load Node.js modules */
var fs = require('fs'),
path = require('path');
/**
* Makes the given `path` 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 The permission mode.
*/
function mkdirpSync(dirname, mode) {
var separator = path.sep,
segments = dirname.split(separator),
type = typeof mode;
if (!(type == 'number' || type == 'string')) {
mode = '0777';
}
segments.reduce(function(currPath, segment, index) {
// skip leading separator of absolute paths
if (index === 0 && currPath === '') {
return separator;
}
segment = currPath + (currPath === separator ? segment : separator + segment);
try {
segment = fs.realpathSync(segment);
} catch(e) {
fs.mkdirSync(segment, mode);
}
return segment;
});
}
// expose
module.exports = mkdirpSync;
}());

View File

@@ -1114,22 +1114,34 @@
QUnit.module('output options'); QUnit.module('output options');
(function() { (function() {
var nestedDir = 'test/mkdir/'; var nestedPath = path.join(__dirname, 'a', 'b');
var commands = [ var commands = [
'-o a.js', '-o a.js',
'--output a.js', '--output b.js',
'-o ' + nestedDir + 'a.js' '-o ./a/b/c.js'
]; ];
commands.forEach(function(command, index) { commands.forEach(function(command) {
asyncTest('`lodash ' + command +'`', function() { asyncTest('`lodash ' + command +'`', function() {
var counter = -1, var counter = 0,
start = _.after(2, _.once(QUnit.start)); expected = /(\w+)(?=\.js$)/.exec(command)[0],
isDirs = _.contains(command, 'c.js');
var start = _.after(2, _.once(function() {
if (isDirs) {
fs.rmdirSync(nestedPath);
}
QUnit.start();
}));
if (isDirs) {
command = command.replace('./a/b/c.js', path.join(nestedPath, 'c.js'));
}
build(['-s'].concat(command.split(' ')), function(data) { build(['-s'].concat(command.split(' ')), function(data) {
equal(path.basename(data.outputPath, '.js'), (++counter ? 'a.min' : 'a'), command); var basename = path.basename(data.outputPath, '.js');
equal(basename, expected + (counter++ ? '.min' : ''), command);
start(); start();
fs.existsSync(nestedDir) && fs.rmdirSync(nestedDir);
}); });
}); });
}); });