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) {
22 if (!made) made = null;
24 var cb = f || function () {};
27 xfs.mkdir(p, mode, function (er) {
30 return cb(null, made);
34 if (path.dirname(p) === p) return cb(er);
35 mkdirP(path.dirname(p), opts, function (er, made) {
37 else mkdirP(p, opts, cb, made);
41 // In the case of any other error, just see if there's a dir
42 // there already. If so, then hooray! If not, then something
45 xfs.stat(p, function (er2, stat) {
46 // if the stat fails, then that's super weird.
47 // let the original error be the failure reason.
48 if (er2 || !stat.isDirectory()) cb(er, made)
56 mkdirP.sync = function sync (p, opts, made) {
57 if (!opts || typeof opts !== 'object') {
58 opts = { mode: opts };
62 var xfs = opts.fs || fs;
64 if (mode === undefined) {
67 if (!made) made = null;
72 xfs.mkdirSync(p, mode);
78 made = sync(path.dirname(p), opts, made);
82 // In the case of any other error, just see if there's a dir
83 // there already. If so, then hooray! If not, then something
88 stat = xfs.statSync(p);
93 if (!stat.isDirectory()) throw err0;