1 var fs = require('graceful-fs')
2 var path = require('path')
4 var o777 = parseInt('0777', 8)
6 function mkdirs (p, opts, callback, made) {
7 if (typeof opts === 'function') {
10 } else if (!opts || typeof opts !== 'object') {
15 var xfs = opts.fs || fs
17 if (mode === undefined) {
18 mode = o777 & (~process.umask())
20 if (!made) made = null
22 callback = callback || function () {}
25 xfs.mkdir(p, mode, function (er) {
28 return callback(null, made)
32 if (path.dirname(p) === p) return callback(er)
33 mkdirs(path.dirname(p), opts, function (er, made) {
34 if (er) callback(er, made)
35 else mkdirs(p, opts, callback, made)
39 // In the case of any other error, just see if there's a dir
40 // there already. If so, then hooray! If not, then something
43 xfs.stat(p, function (er2, stat) {
44 // if the stat fails, then that's super weird.
45 // let the original error be the failure reason.
46 if (er2 || !stat.isDirectory()) callback(er, made)
47 else callback(null, made)
54 module.exports = mkdirs