Skip to content

Instantly share code, notes, and snippets.

@cgutierrez
Created December 14, 2012 19:23
Show Gist options
  • Save cgutierrez/4287923 to your computer and use it in GitHub Desktop.
Save cgutierrez/4287923 to your computer and use it in GitHub Desktop.
recursive mkdir
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