Merge pull request #162 from pbouianov/mkdirp

Fixed issues as per pull request conversation
This commit is contained in:
Alan K 2014-04-02 19:58:54 -04:00
commit 4ef0f26b79
2 changed files with 23 additions and 24 deletions

View File

@ -1323,7 +1323,7 @@ sh.tempDir(function(err, tmp) {
}); });
``` ```
#### sh.mkdirp(callback)<a name="mkdirp"></a> #### sh.mkdirp(path, callback)<a name="mkdirp"></a>
Recursively creates the directory at the provided path. If the Recursively creates the directory at the provided path. If the
directory already exists, no error is returned. All parents must directory already exists, no error is returned. All parents must

View File

@ -365,19 +365,32 @@ define(function(require) {
callback = callback || function(){}; callback = callback || function(){};
if(!path) { if(!path) {
callback(new Errors.EINVAL('missing path argument')); callback(new Errors.EINVAL("Missing path argument"));
return; return;
} }
else if (path === '/'){ else if (path === '/') {
callback(); callback();
return; return;
} }
function _mkdirp(path, callback){ function _mkdirp(path, callback) {
fs.stat(path, function (err, stat) { fs.stat(path, function (err, stat) {
//doesn't exist if(stat) {
if (err && err.code === 'ENOENT') { if(stat.isDirectory()) {
callback();
return;
}
else if (stat.isFile()) {
callback(new Errors.ENOTDIR());
return;
}
}
else if (err && err.code !== 'ENOENT') {
callback(err);
return;
}
else {
var parent = Path.dirname(path); var parent = Path.dirname(path);
if(parent === '/'){ //parent is root if(parent === '/') {
fs.mkdir(path, function (err) { fs.mkdir(path, function (err) {
if (err && err.code != 'EEXIST') { if (err && err.code != 'EEXIST') {
callback(err); callback(err);
@ -387,10 +400,10 @@ define(function(require) {
return; return;
}); });
} }
else { //parent is not root, make parent first else {
_mkdirp(parent, function (err) { _mkdirp(parent, function (err) {
if (err) return callback(err); if (err) return callback(err);
fs.mkdir(path, function (err) { //then make dir fs.mkdir(path, function (err) {
if (err && err.code != 'EEXIST') { if (err && err.code != 'EEXIST') {
callback(err); callback(err);
return; return;
@ -401,21 +414,7 @@ define(function(require) {
}); });
} }
} }
//other error
else if (err){
callback(err);
return;
}
//already exists
else if (stat.type === 'DIRECTORY') {
callback();
return;
}
//not a directory
else if (stat.type === 'FILE') {
callback(new Errors.ENOTDIR());
return;
}
}); });
} }