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 | View Code Duplication | function mkdirP (p, opts, f, made) { |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
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; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
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); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
36 | else mkdirP(p, opts, cb, made); |
||
37 | }); |
||
38 | break; |
||
0 ignored issues
–
show
|
|||
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) |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
48 | else cb(null, made); |
||
49 | }); |
||
50 | break; |
||
0 ignored issues
–
show
|
|||
51 | } |
||
52 | }); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | mkdirP.sync = function sync (p, opts, made) { |
|
0 ignored issues
–
show
|
|||
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; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
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; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
93 | break; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return made; |
||
98 | }; |
||
99 |