1 var fs = require('graceful-fs')
2 var path = require('path')
3 var invalidWin32Path = require('./win32').invalidWin32Path
5 var o777 = parseInt('0777', 8)
7 function mkdirs (p, opts, callback, made) {
8 if (typeof opts === 'function') {
11 } else if (!opts || typeof opts !== 'object') {
15 if (process.platform === 'win32' && invalidWin32Path(p)) {
16 var errInval = new Error(p + ' contains invalid WIN32 path characters.')
17 errInval.code = 'EINVAL'
18 return callback(errInval)
22 var xfs = opts.fs || fs
24 if (mode === undefined) {
25 mode = o777 & (~process.umask())
27 if (!made) made = null
29 callback = callback || function () {}
32 xfs.mkdir(p, mode, function (er) {
35 return callback(null, made)
39 if (path.dirname(p) === p) return callback(er)
40 mkdirs(path.dirname(p), opts, function (er, made) {
41 if (er) callback(er, made)
42 else mkdirs(p, opts, callback, made)
46 // In the case of any other error, just see if there's a dir
47 // there already. If so, then hooray! If not, then something
50 xfs.stat(p, function (er2, stat) {
51 // if the stat fails, then that's super weird.
52 // let the original error be the failure reason.
53 if (er2 || !stat.isDirectory()) callback(er, made)
54 else callback(null, made)
61 module.exports = mkdirs