UNPKG

namastejs

Version:

A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste 🙏'

120 lines (99 loc) 3.08 kB
const fs = require("fs").promises; const path = require("path"); const SKIP_FOLDERS = [ "Windows", "Program Files", "Program Files (x86)", "System Volume Information", "$Recycle.Bin", ]; function formatSize(bytes) { if (bytes < 1024) return `${bytes} B`; const kb = bytes / 1024; if (kb < 1024) return `${kb.toFixed(1)} KB`; const mb = kb / 1024; if (mb < 1024) return `${mb.toFixed(1)} MB`; const gb = mb / 1024; return `${gb.toFixed(1)} GB`; } async function getSize(filePath) { try { const stats = await fs.stat(filePath); if (stats.isFile()) return stats.size; let total = 0; const entries = await fs.readdir(filePath); for (const entry of entries) { total += await getSize(path.join(filePath, entry)); } return total; } catch { return 0; } } async function scanDirectory(dirPath) { const sizes = []; try { const entries = await fs.readdir(dirPath); for (const entry of entries) { if (SKIP_FOLDERS.includes(entry)) continue; const fullPath = path.join(dirPath, entry); try { const stat = await fs.stat(fullPath); const size = await getSize(fullPath); sizes.push({ name: entry, size, isDir: stat.isDirectory() }); } catch { // skip error } } } catch { // skip directory error } sizes.sort((a, b) => b.size - a.size); return sizes.slice(0, 5); } function showLoader(message) { const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; let i = 0; const interval = setInterval(() => { const frame = frames[i++ % frames.length]; process.stdout.write(`\r${frame} ${message} `); }, 80); return () => { clearInterval(interval); process.stdout.clearLine(0); process.stdout.cursorTo(0); process.stdout.write("\n"); }; } async function sizeCLI(args = []) { let targetPath = process.cwd(); const pathArg = args.find((arg) => arg.startsWith("--path=")); if (pathArg) { const customPath = pathArg.split("=")[1]; if (require("fs").existsSync(customPath)) { targetPath = path.resolve(customPath); } else { console.log(`❌ Provided path does not exist: ${customPath}`); return; } } if (targetPath === path.parse(targetPath).root) { console.log("⚠️ Scanning from root directory may take a long time."); console.log("💡 Tip: Use --path to scan a specific folder\n"); } const stop = showLoader("Scanning for heavy folders/files..."); const results = await scanDirectory(targetPath); stop(); console.log("📊 Project Bloat Report Is Here\n"); if (results.length === 0) { console.log("No files or folders found to scan."); return; } results.forEach((item, index) => { const emoji = item.isDir ? "📁" : "📄"; console.log( `${index + 1}. ${emoji} ${item.name}${formatSize(item.size)}` ); }); } module.exports = { sizeCLI };