-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
59 lines (50 loc) · 2.79 KB
/
index.ts
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
import fs from 'fs/promises';
import { extname, dirname } from 'path';
import normalize from '@/normalize';
/**
* 创建指定路径的目录,支持创建多级目录 (create the specified path of the folder, support to create multi-level folders)
* @description 如果存在这个路径,则不会创建,如果不存在,则会创建;如果是文件路径,则取其父目录 (if this path exists, it will not be created, if it does not exist, it will be created; if it is a file path, then take its parent directory)
* @param {string} dir 要创建的目录路径,如果是文件路径,则取其所在目录 (the path of the folder to create, if it is a file path, then take its parent directory)
* @returns {Promise<boolean>} 如果创建成功或已存在,则返回 true;否则返回 false (if the creation is successful or exists, return true; otherwise return false)
*/
const createDir = async (dir: string): Promise<boolean> => {
try {
dir = normalize(dir);
// 判断是否是文件路径
const isFilePath = extname(dir) !== '';
// 获取目录所在的目录路径,如果是文件路径,则取其父目录,否则默认为当前目录
await fs.mkdir(isFilePath ? dirname(dir) : dir, { recursive: true });
return true;
} catch (err) {
const error = err as NodeJS.ErrnoException;
// 如果错误代码表示目录已存在,则返回 true
if (error.code === 'EEXIST') {
return true;
} else if (err instanceof Error) {
throw err;
}
return false;
}
};
/**
* 创建指定路径的目录,支持创建多级目录,可以传入多个路径 (create the specified path of the folder, support to create multi-level folders, you can pass in multiple paths)
* @description 如果存在这个路径,则不会创建,如果不存在,则会创建;如果是文件路径,则取其父目录 (if this path exists, it will not be created, if it does not exist, it will be created; if it is a file path, then take its parent directory)
* @param {string | string[]} dirs 要创建的目录路径,如果是文件路径,则取其所在目录 (the path of the folder to create, if it is a file path, then take its parent directory)
* @returns {Promise<boolean>} 如果创建成功或已存在,则返回 true;否则返回 false (if the creation is successful or exists, return true; otherwise return false)
*/
const makeDir = async (dirs: string | string[]): Promise<boolean> => {
// eslint-disable-next-line no-useless-catch
try {
const normalizedPaths = Array.isArray(dirs) ? dirs.map(normalize) : [normalize(dirs)];
for (const path of normalizedPaths) {
const result = await createDir(path);
if (!result) {
return false;
}
}
return true;
} catch (err) {
throw err;
}
};
export default makeDir;