| 1 | var path = require('path');
|
|---|
| 2 | var fs = require('fs');
|
|---|
| 3 | var _0777 = parseInt('0777', 8);
|
|---|
| 4 |
|
|---|
| 5 | module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
|
|---|
| 6 |
|
|---|
| 7 | function mkdirP (p, opts, f, made) {
|
|---|
| 8 | if (typeof opts === 'function') {
|
|---|
| 9 | f = opts;
|
|---|
| 10 | opts = {};
|
|---|
| 11 | }
|
|---|
| 12 | else if (!opts || typeof opts !== 'object') {
|
|---|
| 13 | opts = { mode: opts };
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | var mode = opts.mode;
|
|---|
| 17 | var xfs = opts.fs || fs;
|
|---|
| 18 |
|
|---|
| 19 | if (mode === undefined) {
|
|---|
| 20 | mode = _0777
|
|---|
| 21 | }
|
|---|
| 22 | if (!made) made = null;
|
|---|
| 23 |
|
|---|
| 24 | var cb = f || /* istanbul ignore next */ function () {};
|
|---|
| 25 | p = path.resolve(p);
|
|---|
| 26 |
|
|---|
| 27 | xfs.mkdir(p, mode, function (er) {
|
|---|
| 28 | if (!er) {
|
|---|
| 29 | made = made || p;
|
|---|
| 30 | return cb(null, made);
|
|---|
| 31 | }
|
|---|
| 32 | switch (er.code) {
|
|---|
| 33 | case 'ENOENT':
|
|---|
| 34 | /* istanbul ignore if */
|
|---|
| 35 | if (path.dirname(p) === p) return cb(er);
|
|---|
| 36 | mkdirP(path.dirname(p), opts, function (er, made) {
|
|---|
| 37 | /* istanbul ignore if */
|
|---|
| 38 | if (er) cb(er, made);
|
|---|
| 39 | else mkdirP(p, opts, cb, made);
|
|---|
| 40 | });
|
|---|
| 41 | break;
|
|---|
| 42 |
|
|---|
| 43 | // In the case of any other error, just see if there's a dir
|
|---|
| 44 | // there already. If so, then hooray! If not, then something
|
|---|
| 45 | // is borked.
|
|---|
| 46 | default:
|
|---|
| 47 | xfs.stat(p, function (er2, stat) {
|
|---|
| 48 | // if the stat fails, then that's super weird.
|
|---|
| 49 | // let the original error be the failure reason.
|
|---|
| 50 | if (er2 || !stat.isDirectory()) cb(er, made)
|
|---|
| 51 | else cb(null, made);
|
|---|
| 52 | });
|
|---|
| 53 | break;
|
|---|
| 54 | }
|
|---|
| 55 | });
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | mkdirP.sync = function sync (p, opts, made) {
|
|---|
| 59 | if (!opts || typeof opts !== 'object') {
|
|---|
| 60 | opts = { mode: opts };
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | var mode = opts.mode;
|
|---|
| 64 | var xfs = opts.fs || fs;
|
|---|
| 65 |
|
|---|
| 66 | if (mode === undefined) {
|
|---|
| 67 | mode = _0777
|
|---|
| 68 | }
|
|---|
| 69 | if (!made) made = null;
|
|---|
| 70 |
|
|---|
| 71 | p = path.resolve(p);
|
|---|
| 72 |
|
|---|
| 73 | try {
|
|---|
| 74 | xfs.mkdirSync(p, mode);
|
|---|
| 75 | made = made || p;
|
|---|
| 76 | }
|
|---|
| 77 | catch (err0) {
|
|---|
| 78 | switch (err0.code) {
|
|---|
| 79 | case 'ENOENT' :
|
|---|
| 80 | made = sync(path.dirname(p), opts, made);
|
|---|
| 81 | sync(p, opts, made);
|
|---|
| 82 | break;
|
|---|
| 83 |
|
|---|
| 84 | // In the case of any other error, just see if there's a dir
|
|---|
| 85 | // there already. If so, then hooray! If not, then something
|
|---|
| 86 | // is borked.
|
|---|
| 87 | default:
|
|---|
| 88 | var stat;
|
|---|
| 89 | try {
|
|---|
| 90 | stat = xfs.statSync(p);
|
|---|
| 91 | }
|
|---|
| 92 | catch (err1) /* istanbul ignore next */ {
|
|---|
| 93 | throw err0;
|
|---|
| 94 | }
|
|---|
| 95 | /* istanbul ignore if */
|
|---|
| 96 | if (!stat.isDirectory()) throw err0;
|
|---|
| 97 | break;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | return made;
|
|---|
| 102 | };
|
|---|