UNPKG

741 BJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3
4module.exports = function (dir, mode) {
5 const sep = path.sep, arr = dir.split(sep)
6 let i = 0, len = arr.length, tmp
7 for (; i < len; i++) {
8 tmp = arr.slice(0, i + 1).join(sep);
9 if (tmp === '') continue;
10 if (!fs.existsSync(tmp)) {
11 try {
12 fs.mkdirSync(tmp, mode);
13 } catch (e) {
14 switch (e.code) {
15 case 'EEXIST':
16 break;
17 case 'ENOTDIR' :
18 throw `${tmp.slice(0, tmp.lastIndexOf(sep))} is not a directory`
19 default:
20 throw e
21 }
22 }
23 }
24 }
25}
\No newline at end of file