mkdirp.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
'use strict';
var fs = require('fs');
var path = require('path');
//var noop = function () {};
// keeping the function signature Promise-friendly
function mkdirp(opts, cb) {
//if (!cb) { cb = noop; }
if ('string' === typeof opts) {
opts = { name: opts };
}
if (!opts || !opts.name || 'string' !== typeof opts.name) {
cb(new Error("mkdirp missing opts.name"));
return;
}
// normalize doesn't account for "../my-sibling/my-neice"
opts.name = path.resolve(opts.name);
fs.mkdir(opts.name, function (err) {
// All's well if all's well
if (!err) { cb(null, null); return; }
if ('ENOENT' === err.code) {
// The only error we care about is the error that we can ostensibly fix
// (same on Windows, Mac, Linux)
var name = opts.name;
opts.name = path.dirname(opts.name);
mkdirp(opts, function (err) {
if (err) {
cb(err);
return;
}
opts.name = name;
mkdirp(opts, cb);
});
} else {
// Any other error could be something normal and fairly cross platform (like EEXIST),
// but it could also be something random and weird and platform specific that doesn't
// actually matter, so we just carry on and try anyway.
fs.stat(opts.name, function (_err, stat) {
// if we don't succeed, propagate the original error (which will be more meaningful)
if (_err || !stat.isDirectory()) {
cb(err);
return;
}
cb(null, null);
});
}
});
}
module.exports = mkdirp;