1 var path = require('path');
2 var fs = require('fs');
3 var _0777 = parseInt('0777', 8);
5 module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
7 function mkdirP (p, opts, f, made) {
8 if (typeof opts === 'function') {
12 else if (!opts || typeof opts !== 'object') {
13 opts = { mode: opts };
17 var xfs = opts.fs || fs;
19 if (mode === undefined) {
20 mode = _0777 & (~process.umask());
22 if (!made) made = null;
24 var cb = f || function () {};
27 xfs.mkdir(p, mode, function (er) {
30 return cb(null, made);
34 mkdirP(path.dirname(p), opts, function (er, made) {
36 else mkdirP(p, opts, cb, made);
40 // In the case of any other error, just see if there's a dir
41 // there already. If so, then hooray! If not, then something
44 xfs.stat(p, function (er2, stat) {
45 // if the stat fails, then that's super weird.
46 // let the original error be the failure reason.
47 if (er2 || !stat.isDirectory()) cb(er, made)
55 mkdirP.sync = function sync (p, opts, made) {
56 if (!opts || typeof opts !== 'object') {
57 opts = { mode: opts };
61 var xfs = opts.fs || fs;
63 if (mode === undefined) {
64 mode = _0777 & (~process.umask());
66 if (!made) made = null;
71 xfs.mkdirSync(p, mode);
77 made = sync(path.dirname(p), opts, made);
81 // In the case of any other error, just see if there's a dir
82 // there already. If so, then hooray! If not, then something
87 stat = xfs.statSync(p);
92 if (!stat.isDirectory()) throw err0;