datasquirel/package-shared/utils/empty-dir.ts

24 lines
673 B
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
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}`);
}
}