datasquirel/package-shared/utils/empty-dir.ts
2025-01-10 20:10:28 +01:00

24 lines
673 B
TypeScript

import fs from "fs";
import path from "path";
export default function emptyDirectory(dir: string) {
try {
const dirContent = fs.readdirSync(dir);
for (let i = 0; i < dirContent.length; i++) {
const fileFolder = dirContent[i];
const fullFileFolderPath = path.join(dir, fileFolder);
const stat = fs.statSync(fullFileFolderPath);
if (stat.isDirectory()) {
emptyDirectory(fullFileFolderPath);
continue;
}
fs.unlinkSync(fullFileFolderPath);
}
} catch (error: any) {
console.log(`Error Emptying ${dir}: ${error.message}`);
}
}