// Folder activity: walks a folder (not hidden folders, nor ones like node_modules) and prints what's new and what // changed in the last few days, the biggest files, and totals by kind. It reads names, sizes and dates only, and // changes nothing. Run it from its form in CLIDE, or here: // bun folder-activity.ts --days 7 ~/Documents import { readdirSync, statSync } from "node:fs"; import { extname, join, relative } from "node:path"; import { parseArgs } from "node:util"; const { values, positionals } = parseArgs({ args: process.argv.slice(2), options: { days: { type: "string", default: "7" } }, allowPositionals: true }); const root = positionals[0] ?? ""; const days = Math.max(1, Number(values.days) || 7); const since = Date.now() - days * 86_400_000; const SKIP = new Set(["node_modules", "__pycache__", "venv", ".git"]); const MOST_FILES = 100_000; const SHOWN = 25; type Seen = { path: string; size: number; made: number; changed: number }; const files: Seen[] = []; let limited = false; const walk = (dir: string) => { let entries; try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; } for (const entry of entries) { if (files.length >= MOST_FILES) return void (limited = true); if (entry.name.startsWith(".") || SKIP.has(entry.name)) continue; const path = join(dir, entry.name); if (entry.isDirectory()) walk(path); else if (entry.isFile()) { try { const stat = statSync(path); files.push({ path, size: stat.size, made: stat.birthtimeMs || stat.ctimeMs, changed: stat.mtimeMs }); } catch {} } } }; try { if (!statSync(root).isDirectory()) throw new Error(); } catch { console.error(`${root || "That"} isn't a folder.`); process.exit(2); } walk(root); const size = (bytes: number) => (bytes < 1024 ? `${bytes} B` : bytes < 1024 ** 2 ? `${(bytes / 1024).toFixed(1)} KB` : bytes < 1024 ** 3 ? `${(bytes / 1024 ** 2).toFixed(1)} MB` : `${(bytes / 1024 ** 3).toFixed(2)} GB`); const day = (ms: number) => new Date(ms).toLocaleDateString(); const name = (one: Seen) => relative(root, one.path); const list = (title: string, some: Seen[], line: (one: Seen) => string) => [ `## ${title} (${some.length})`, ...(some.length === 0 ? ["None."] : some.slice(0, SHOWN).map((one) => `- ${line(one)}`)), ...(some.length > SHOWN ? [`- and ${some.length - SHOWN} more`] : []), "", ]; const added = files.filter((one) => one.made >= since).sort((a, b) => b.made - a.made); const changed = files.filter((one) => one.made < since && one.changed >= since).sort((a, b) => b.changed - a.changed); const biggest = [...added, ...changed].sort((a, b) => b.size - a.size).slice(0, 10); const kinds = new Map(); for (const one of files) { const kind = extname(one.path).slice(1).toLowerCase() || "no extension"; const was = kinds.get(kind) ?? { count: 0, size: 0 }; kinds.set(kind, { count: was.count + 1, size: was.size + one.size }); } const total = files.reduce((sum, one) => sum + one.size, 0); console.log( [ `# ${root}`, `The last ${days} day${days === 1 ? "" : "s"}, to ${new Date().toLocaleString()}. ${files.length.toLocaleString("en")} files, ${size(total)} in all${limited ? `: stopped at ${MOST_FILES.toLocaleString("en")} files` : ""}.`, "", ...list("New", added, (one) => `${name(one)} · ${size(one.size)} · ${day(one.made)}`), ...list("Changed", changed, (one) => `${name(one)} · ${size(one.size)} · ${day(one.changed)}`), ...list("Biggest of those", biggest, (one) => `${name(one)} · ${size(one.size)}`), "## By kind", ...[...kinds.entries()].sort((a, b) => b[1].size - a[1].size).slice(0, 15).map(([kind, { count, size: bytes }]) => `- ${kind}: ${count.toLocaleString("en")} file${count === 1 ? "" : "s"}, ${size(bytes)}`), ].join("\n"), );