-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (28 loc) · 798 Bytes
/
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
const fs = require('fs');
const { dirname } = require('path');
const { promisify } = require('util');
const mkdir = promisify(fs.mkdir);
const stat = promisify(fs.stat);
async function mkdirp(p, mode = 0o777) {
try {
await mkdir(p, mode);
} catch (error) {
switch (error.code) {
case 'ENOENT':
// Recursively move down tree until we find a dir that exists.
await mkdirp(dirname(p), mode);
// Bubble back up and create every dir.
await mkdirp(p, mode);
break;
default:
// If EEXISTS error, check if it's a file or directory
// If it's not a directory throw.
const stats = await stat(p);
if (!stats.isDirectory()) {
throw error;
}
break;
}
}
}
module.exports = mkdirp;