namastejs
Version:
A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste ๐'
71 lines (61 loc) โข 1.7 kB
JavaScript
const fs = require("fs");
const path = require("path");
const foldersToDelete = [
"node_modules",
"dist",
"build",
".next",
".turbo",
".parcel-cache",
"coverage",
];
// Calculate folder size recursively
function getFolderSize(folderPath) {
let totalSize = 0;
if (fs.existsSync(folderPath)) {
const files = fs.readdirSync(folderPath);
files.forEach((file) => {
const fullPath = path.join(folderPath, file);
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
totalSize += getFolderSize(fullPath);
} else {
totalSize += stats.size;
}
});
}
return totalSize;
}
function formatSize(bytes) {
const units = ["B", "KB", "MB", "GB"];
let index = 0;
while (bytes >= 1024 && index < units.length - 1) {
bytes /= 1024;
index++;
}
return `${bytes.toFixed(2)} ${units[index]}`;
}
function deleteFolder(folderPath) {
if (fs.existsSync(folderPath)) {
fs.rmSync(folderPath, { recursive: true, force: true });
}
}
function clearCLI() {
console.log("๐ฟ Cleaning project directories...\n");
let totalFreed = 0;
foldersToDelete.forEach((folder) => {
const fullPath = path.join(process.cwd(), folder);
if (fs.existsSync(fullPath)) {
const size = getFolderSize(fullPath);
deleteFolder(fullPath);
totalFreed += size;
console.log(`๐งน Deleted ${folder} โ Freed ${formatSize(size)}`);
}
});
if (totalFreed > 0) {
console.log(`\n๐ฆ Total space cleaned: ${formatSize(totalFreed)}`);
} else {
console.log("โ๏ธ Nothing to clean.");
}
}
module.exports = { clearCLI };