mkpath.js
1.52 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var fs = require('fs');
var path = require('path');
var mkpath = function mkpath(dirpath, mode, callback) {
dirpath = path.resolve(dirpath);
if (typeof mode === 'function' || typeof mode === 'undefined') {
callback = mode;
mode = 0777 & (~process.umask());
}
if (!callback) {
callback = function () {};
}
fs.stat(dirpath, function (err, stats) {
if (err) {
if (err.code === 'ENOENT') {
mkpath(path.dirname(dirpath), mode, function (err) {
if (err) {
callback(err);
} else {
fs.mkdir(dirpath, mode, callback);
}
});
} else {
callback(err);
}
} else if (stats.isDirectory()) {
callback(null);
} else {
callback(new Error(dirpath + ' exists and is not a directory'));
}
});
};
mkpath.sync = function mkpathsync(dirpath, mode) {
dirpath = path.resolve(dirpath);
if (typeof mode === 'undefined') {
mode = 0777 & (~process.umask());
}
try {
if (!fs.statSync(dirpath).isDirectory()) {
throw new Error(dirpath + ' exists and is not a directory');
}
} catch (err) {
if (err.code === 'ENOENT') {
mkpathsync(path.dirname(dirpath), mode);
fs.mkdirSync(dirpath, mode);
} else {
throw err;
}
}
};
module.exports = mkpath;