-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (59 loc) · 2.08 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
60
61
62
63
64
65
66
67
import fs from 'fs/promises';
import { join } from 'path';
import normalize from '@/normalize';
import exists from '@/exists';
/**
* 删除指定的文件或目录 (delete the specified file or directory)
* @param {string} itemPath 文件或目录路径 (the path of the file or directory)
* @param {boolean} includeSubDirs 是否包含子目录 (whether to include subdirectories)
*/
const deleteItem = async (itemPath: string, includeSubDirs: boolean): Promise<void> => {
const stack = [itemPath];
while (stack.length > 0) {
const currentPath = stack[stack.length - 1];
const stat = await fs.stat(currentPath);
if (stat.isDirectory()) {
const files = await fs.readdir(currentPath);
if (files.length > 0) {
if (includeSubDirs) {
// 将子目录和文件添加到栈中
files.forEach((file) => stack.push(join(currentPath, file)));
} else {
// 不包含子目录,跳过非空目录
stack.pop();
}
} else {
// 删除空目录并从栈中移除
await fs.rmdir(currentPath);
stack.pop();
}
} else {
// 删除文件并从栈中移除
await fs.unlink(currentPath);
stack.pop();
}
}
};
/**
* 删除指定路径的文件或目录 (delete the specified path of the file or directory)
* @param {string | string[]} paths 要删除的文件或目录路径 (the path of the file or directory to delete)
* @param {boolean} [includeSubDirs=true] 是否包含子目录 (whether to include subdirectories)
* @returns {Promise<boolean>} 是否删除成功 (whether the deletion is successful)
*/
const remove = async (paths: string | string[], includeSubDirs: boolean = true): Promise<boolean> => {
try {
const normalizedPaths = Array.isArray(paths) ? paths.map(normalize) : [normalize(paths)];
for (const path of normalizedPaths) {
if (await exists(path)) {
await deleteItem(path, includeSubDirs);
}
}
return true;
} catch (err) {
if (err instanceof Error) {
throw err;
}
return false;
}
};
export default remove;