1 var path = require('path');
2 var fs = require('fs');
4 module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
6 function mkdirP (p, opts, f, made) {
7 if (typeof opts === 'function') {
11 else if (!opts || typeof opts !== 'object') {
12 opts = { mode: opts };
16 var xfs = opts.fs || fs;
18 if (mode === undefined) {
19 mode = 0777 & (~process.umask());
21 if (!made) made = null;
23 var cb = f || function () {};
26 xfs.mkdir(p, mode, function (er) {
29 return cb(null, made);
33 mkdirP(path.dirname(p), opts, function (er, made) {
35 else mkdirP(p, opts, cb, 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()) cb(er, made)
54 mkdirP.sync = function sync (p, opts, made) {
55 if (!opts || typeof opts !== 'object') {
56 opts = { mode: opts };
60 var xfs = opts.fs || fs;
62 if (mode === undefined) {
63 mode = 0777 & (~process.umask());
65 if (!made) made = null;
70 xfs.mkdirSync(p, mode);
76 made = sync(path.dirname(p), opts, made);
80 // In the case of any other error, just see if there's a dir
81 // there already. If so, then hooray! If not, then something
86 stat = xfs.statSync(p);
91 if (!stat.isDirectory()) throw err0;