Skip to content

Instantly share code, notes, and snippets.

@jonvuri
Created November 12, 2012 18:28
Show Gist options
  • Save jonvuri/4061004 to your computer and use it in GitHub Desktop.
Save jonvuri/4061004 to your computer and use it in GitHub Desktop.
Shorter mkdirp
var fs = require('fs');
var path = require('path');
module.exports = exports = function mkdirp(dirpath, mode, callback) {
dirpath = path.resolve(dirpath);
if (typeof mode === 'function' || typeof mode === 'undefined') {
callback = mode;
mode = 0777 & (~process.umask());
}
if (!callback) {
callback = function () {};
}
fs.stat(dirpath, function (err, stats) {
if (err) {
if (err.code === 'ENOENT') {
mkdirp(path.dirname(dirpath), function (err) {
if (err) {
callback(err);
} else {
fs.mkdir(dirpath, mode, callback);
}
});
} else {
callback(err);
}
} else if (stats.isDirectory()) {
callback(null);
} else {
callback(new Error(dirpath + ' exists and is not a directory'));
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment