-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (45 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const path = require('path');
const fs = require('fs');
const _0777 = parseInt('0777', 8);
module.exports = mkdirP;
const errCode = process.platform === 'win32' ? 3 : 2;
const supportRecursive = process.version >= '0.32.1';
function mkdirP(p, opts, made) {
if (!opts || typeof opts !== 'object') {
opts = {
mode: opts
};
}
let mode = opts.mode;
const xfs = opts.fs || fs;
if (mode === undefined) {
mode = _0777 & (~process.umask());
}
if (!made) made = null;
p = path.resolve(p);
try {
xfs.mkdir(p, supportRecursive ? { recursive: true, mode } : mode);
made = made || p;
} catch (err0) {
switch (err0.number) {
case errCode:
made = mkdirP(path.dirname(p), opts, made);
mkdirP(p, opts, made);
break;
// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
let stat;
try {
stat = xfs.stat(p);
} catch (err1) {
throw err0;
}
if (!stat.isDirectory()) throw err0;
break;
}
}
return made;
}
mkdirP.supportRecursive = supportRecursive;