-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmkdirp.js
More file actions
43 lines (37 loc) · 1.22 KB
/
mkdirp.js
File metadata and controls
43 lines (37 loc) · 1.22 KB
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
var path = require('path');
var fs = require('fs');
exports.mkdirp = exports.mkdirP = function mkdirP (p, mode, f) {
var cb = f || function () {};
if (p.charAt(0) != '/') { cb(new Error('Relative path: ' + p)); return }
var ps = path.normalize(p).split('/');
fs.exists(p, function (exists) {
if (exists) cb(null);
else mkdirP(ps.slice(0,-1).join('/'), mode, function (err) {
if (err && err.code !== 'EEXIST') cb(err)
else fs.mkdir(p, mode, function (err) {
if (err && err.code !== 'EEXIST') cb(err)
else cb()
});
});
});
};
exports.mkdirpSync = exports.mkdirPSync = function mkdirPSync (p, mode) {
if (p.charAt(0) != '/') { throw new Error('Relative path: ' + p); return; }
var ps = path.normalize(p).split('/'),
exists = fs.existsSync(p);
function tryMkdirSync () {
try { fs.mkdirSync(p, mode); }
catch (ex) { if (ex.code !== 'EEXIST') throw ex; }
}
if (exists) return;
else {
try {
mkdirPSync(ps.slice(0,-1).join('/'), mode);
tryMkdirSync();
}
catch (ex) {
console.dir(ex);
if (ex.code !== 'EEXIST') { throw ex; }
}
}
}