Files
lodash/build/mkdirp-sync.js
John-David Dalton 81b3567133 Add path.sep for older versions of Node.js.
Former-commit-id: 1a058681fc5c6aae426902a7d6dd76b3a5849837
2013-02-24 09:29:23 -08:00

49 lines
1.4 KiB
JavaScript
Executable File

#!/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 `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;
}());