-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (48 loc) · 1.94 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
import { readdir, stat } from 'fs/promises';
import { TFileSize } from './interface';
import { join } from 'path';
import normalize from '@/normalize';
import exists from '@/exists';
/**
* 获取指定路径的文件或目录体积 (Get the size of the specified path of the file or directory)
* @param {string} path 文件或目录路径 (the path of the file or directory)
* @returns {Promise<bigint>} 文件或目录体积 (the size of the file or directory)
*/
const getSize = async (path: string): Promise<bigint> => {
const stack = [path];
if (await exists(path)) {
let totalSize: bigint = BigInt(0);
while (stack.length > 0) {
const currentPath = stack.pop();
const stats = await stat(currentPath!, { bigint: true });
if (stats.isDirectory()) {
const files = await readdir(currentPath!);
for (const file of files) {
stack.push(join(currentPath!, file));
}
} else {
totalSize += BigInt(stats.size);
}
}
return totalSize;
} else {
return BigInt(0);
}
};
/**
* 获取指定路径或多个路径的体积 (Get the size of the specified path or multiple paths)
* @param {string | string[]} paths 文件或目录路径 (the path of the file or directory)
* @param {boolean} [useBigInt=false] 是否使用 bigint 类型 (whether to use bigint type)
* @returns {Promise<TFileSize[]>} 文件或目录体积 (the size of the file or directory)
*/
const size = async (paths: string | string[], useBigInt: boolean = false): Promise<TFileSize[]> => {
const sizes: TFileSize[] = [];
const normalizedPaths = Array.isArray(paths) ? paths.map(normalize) : [normalize(paths)];
for (const path of normalizedPaths) {
const calculatedSize = await getSize(path);
const size = useBigInt ? calculatedSize : calculatedSize <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(calculatedSize) : calculatedSize;
sizes.push({ path, size });
}
return sizes;
};
export default size;