-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
38 lines (34 loc) · 1.05 KB
/
utils.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
var path = require('path'),
fs = require('fs');
module.exports = {
ensureDirExists: function(filename, cb) {
function iterate(parts, i) {
if (i < parts.length) {
recurse(parts, i+1);
}
else {
cb(null, true);
}
}
function recurse(parts, i) {
var dir = parts.slice(0,i+1).join('/');
path.exists(dir, function(exists) {
if (!exists) {
fs.mkdir(dir, 0755, function(err, rsp) {
if (err && err.errno != 17) { // 17 is EEXIST, no problem
cb(err, false);
}
else {
iterate(parts, i);
}
});
}
else {
iterate(parts, i);
}
})
}
var folder = path.dirname(path.normalize(filename));
recurse(folder.split('/'), 0);
}
}