mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
mkdir
This commit is contained in:
parent
bf9ed2672e
commit
d7b28a2a5a
49
shell.js
49
shell.js
@ -159,37 +159,7 @@ exports.mv = wrap('mv', _mv);
|
||||
//@ ```
|
||||
//@
|
||||
//@ Creates directories.
|
||||
function _mkdir(options, dirs) {
|
||||
options = parseOptions(options, {
|
||||
'p': 'fullpath'
|
||||
});
|
||||
if (!dirs)
|
||||
error('no paths given');
|
||||
|
||||
if (typeof dirs === 'string')
|
||||
dirs = [].slice.call(arguments, 1);
|
||||
// if it's array leave it as it is
|
||||
|
||||
dirs.forEach(function(dir) {
|
||||
if (fs.existsSync(dir)) {
|
||||
if (!options.fullpath)
|
||||
error('path already exists: ' + dir, true);
|
||||
return; // skip dir
|
||||
}
|
||||
|
||||
// Base dir does not exist, and no -p option given
|
||||
var baseDir = path.dirname(dir);
|
||||
if (!fs.existsSync(baseDir) && !options.fullpath) {
|
||||
error('no such file or directory: ' + baseDir, true);
|
||||
return; // skip dir
|
||||
}
|
||||
|
||||
if (options.fullpath)
|
||||
mkdirSyncRecursive(dir);
|
||||
else
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
});
|
||||
} // mkdir
|
||||
var _mkdir = require('./src/mkdir');
|
||||
exports.mkdir = wrap('mkdir', _mkdir);
|
||||
|
||||
//@
|
||||
@ -1093,23 +1063,6 @@ function wrap(cmd, fn, options) {
|
||||
};
|
||||
} // wrap
|
||||
|
||||
// Recursively creates 'dir'
|
||||
function mkdirSyncRecursive(dir) {
|
||||
var baseDir = path.dirname(dir);
|
||||
|
||||
// Base dir exists, no recursion necessary
|
||||
if (fs.existsSync(baseDir)) {
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
return;
|
||||
}
|
||||
|
||||
// Base dir does not exist, go recursive
|
||||
mkdirSyncRecursive(baseDir);
|
||||
|
||||
// Base dir created, can create dir
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
}
|
||||
|
||||
// e.g. 'shelljs_a5f185d0443ca...'
|
||||
function randomFileName() {
|
||||
function randomHash(count) {
|
||||
|
||||
53
src/mkdir.js
Normal file
53
src/mkdir.js
Normal file
@ -0,0 +1,53 @@
|
||||
var common = require('./common');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
|
||||
// Recursively creates 'dir'
|
||||
function mkdirSyncRecursive(dir) {
|
||||
var baseDir = path.dirname(dir);
|
||||
|
||||
// Base dir exists, no recursion necessary
|
||||
if (fs.existsSync(baseDir)) {
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
return;
|
||||
}
|
||||
|
||||
// Base dir does not exist, go recursive
|
||||
mkdirSyncRecursive(baseDir);
|
||||
|
||||
// Base dir created, can create dir
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
}
|
||||
|
||||
function _mkdir(options, dirs) {
|
||||
options = common.parseOptions(options, {
|
||||
'p': 'fullpath'
|
||||
});
|
||||
if (!dirs)
|
||||
common.error('no paths given');
|
||||
|
||||
if (typeof dirs === 'string')
|
||||
dirs = [].slice.call(arguments, 1);
|
||||
// if it's array leave it as it is
|
||||
|
||||
dirs.forEach(function(dir) {
|
||||
if (fs.existsSync(dir)) {
|
||||
if (!options.fullpath)
|
||||
common.error('path already exists: ' + dir, true);
|
||||
return; // skip dir
|
||||
}
|
||||
|
||||
// Base dir does not exist, and no -p option given
|
||||
var baseDir = path.dirname(dir);
|
||||
if (!fs.existsSync(baseDir) && !options.fullpath) {
|
||||
common.error('no such file or directory: ' + baseDir, true);
|
||||
return; // skip dir
|
||||
}
|
||||
|
||||
if (options.fullpath)
|
||||
mkdirSyncRecursive(dir);
|
||||
else
|
||||
fs.mkdirSync(dir, parseInt('0777', 8));
|
||||
});
|
||||
} // mkdir
|
||||
module.exports = _mkdir;
|
||||
Loading…
x
Reference in New Issue
Block a user