Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80681 views
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 & (~process.umask());
21
}
22
if (!made) made = null;
23
24
var cb = f || 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
mkdirP(path.dirname(p), opts, function (er, made) {
35
if (er) cb(er, made);
36
else mkdirP(p, opts, cb, made);
37
});
38
break;
39
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
42
// is borked.
43
default:
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)
48
else cb(null, made);
49
});
50
break;
51
}
52
});
53
}
54
55
mkdirP.sync = function sync (p, opts, made) {
56
if (!opts || typeof opts !== 'object') {
57
opts = { mode: opts };
58
}
59
60
var mode = opts.mode;
61
var xfs = opts.fs || fs;
62
63
if (mode === undefined) {
64
mode = _0777 & (~process.umask());
65
}
66
if (!made) made = null;
67
68
p = path.resolve(p);
69
70
try {
71
xfs.mkdirSync(p, mode);
72
made = made || p;
73
}
74
catch (err0) {
75
switch (err0.code) {
76
case 'ENOENT' :
77
made = sync(path.dirname(p), opts, made);
78
sync(p, opts, made);
79
break;
80
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
83
// is borked.
84
default:
85
var stat;
86
try {
87
stat = xfs.statSync(p);
88
}
89
catch (err1) {
90
throw err0;
91
}
92
if (!stat.isDirectory()) throw err0;
93
break;
94
}
95
}
96
97
return made;
98
};
99
100