Created
December 14, 2012 19:23
-
-
Save cgutierrez/4287923 to your computer and use it in GitHub Desktop.
recursive mkdir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mkdir(dir, mode, callback) { | |
var fs = require('fs'); | |
var path = require('path'); | |
if (typeof mode === 'function') { | |
callback = mode; | |
mode = null; | |
} | |
mode = mode || 0777; | |
dir = path.normalize(dir); | |
dir = dir.split(path.sep); | |
if (dir[0] == '') { | |
dir[0] = path.sep; | |
} | |
function fs_mkdir(newdir, complete) { | |
fs.stat(newdir, function(error, stats) { | |
if (!error) { | |
return complete(error, newdir) | |
} | |
fs.mkdir(newdir, mode, function(error) { | |
complete(error, newdir) | |
}) | |
}); | |
} | |
fs_mkdir(dir.shift(), function(error, prev_path) { | |
if (!dir.length || error) { | |
return typeof callback === 'function' && callback(error, prev_path) | |
} | |
fs_mkdir(path.join(prev_path, dir.shift()), arguments.callee) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment