-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
86 lines (71 loc) · 1.91 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const fs = require('fs');
const path = require('path');
const pify = require('pify');
const defaults = {
mode: 0o777 & (~process.umask()),
fs
};
// https://github.com/nodejs/node/issues/8987
// https://github.com/libuv/libuv/pull/1088
const checkPath = pth => {
if (process.platform === 'win32') {
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''));
if (pathHasInvalidWinCharacters) {
const error = new Error(`Path contains invalid characters: ${pth}`);
error.code = 'EINVAL';
throw error;
}
}
};
module.exports = (input, options) => Promise.resolve().then(() => {
checkPath(input);
options = Object.assign({}, defaults, options);
// TODO: Use util.promisify when targeting Node.js 8
const mkdir = pify(options.fs.mkdir);
const stat = pify(options.fs.stat);
const make = pth => {
return mkdir(pth, options.mode)
.then(() => pth)
.catch(error => {
if (error.code === 'ENOENT') {
if (error.message.includes('null bytes') || path.dirname(pth) === pth) {
throw error;
}
return make(path.dirname(pth)).then(() => make(pth));
}
return stat(pth)
.then(stats => stats.isDirectory() ? pth : Promise.reject())
.catch(() => {
throw error;
});
});
};
return make(path.resolve(input));
});
module.exports.sync = (input, options) => {
checkPath(input);
options = Object.assign({}, defaults, options);
const make = pth => {
try {
options.fs.mkdirSync(pth, options.mode);
} catch (error) {
if (error.code === 'ENOENT') {
if (error.message.includes('null bytes') || path.dirname(pth) === pth) {
throw error;
}
make(path.dirname(pth));
return make(pth);
}
try {
if (!options.fs.statSync(pth).isDirectory()) {
throw new Error('The path is not a directory');
}
} catch (_) {
throw error;
}
}
return pth;
};
return make(path.resolve(input));
};