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