Created
November 12, 2012 18:28
-
-
Save jonvuri/4061004 to your computer and use it in GitHub Desktop.
Shorter mkdirp
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
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