🎈 perf: 优化清理文件性能

This commit is contained in:
RrOrange 2023-04-11 21:29:54 +08:00
parent 7385898f97
commit 10ee4155aa

View File

@ -43,20 +43,20 @@ async function mkdirIfNotExists(dir) {
async function deleteFolderRecursive(folderPath) { async function deleteFolderRecursive(folderPath) {
try { try {
const files = await fs.promises.readdir(folderPath); const files = await fs.promises.readdir(folderPath);
const filePromises = files.map(async (file) => { const actions = files.map(async (file) => {
const curPath = path.join(folderPath, file); const curPath = path.join(folderPath, file);
const stat = await fs.promises.lstat(curPath);
const stat = await fs.promises.lstat(curPath);
if (stat.isDirectory()) { if (stat.isDirectory()) {
// recurse // recurse
await deleteFolderRecursive(curPath); return deleteFolderRecursive(curPath);
} else { } else {
// delete file // delete file
await fs.promises.unlink(curPath); return fs.promises.unlink(curPath);
} }
}); });
await Promise.all(filePromises); await Promise.allSettled(actions);
await fs.promises.rmdir(folderPath); await fs.promises.rmdir(folderPath);
return files.length; return files.length;
} catch (error) { } catch (error) {