12345678910111213141516171819202122232425262728293031323334 |
- import { readdir, stat } from "node:fs";
- import { sum, formatBytes } from "@pureadmin/utils";
- const fileListTotal: number[] = [];
- /**
- * @description 获取指定文件夹中所有文件的总大小
- */
- export const getPackageSize = options => {
- const { folder = "dist", callback, format = true } = options;
- readdir(folder, (err, files: string[]) => {
- if (err) throw err;
- let count = 0;
- const checkEnd = () => {
- ++count == files.length &&
- callback(format ? formatBytes(sum(fileListTotal)) : sum(fileListTotal));
- };
- files.forEach((item: string) => {
- stat(`${folder}/${item}`, async (err, stats) => {
- if (err) throw err;
- if (stats.isFile()) {
- fileListTotal.push(stats.size);
- checkEnd();
- } else if (stats.isDirectory()) {
- getPackageSize({
- folder: `${folder}/${item}/`,
- callback: checkEnd
- });
- }
- });
- });
- files.length === 0 && callback(0);
- });
- };
|