// Gather text files: prints the words of the text files (.txt, .md, .csv, .eml and the like) in a folder that changed // in the last few days, newest first, each under its name, up to a limit. It reads them and changes nothing. Run it // from its form in CLIDE, or here: // bun gather-text-files.ts --days 7 ~/Documents/Customer\ notes import { readdirSync, readFileSync, 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 TEXT = new Set([".txt", ".md", ".markdown", ".csv", ".tsv", ".eml", ".log", ".json", ".html", ".htm", ".xml", ".yaml", ".yml", ".rtf"]); const SKIP = new Set(["node_modules", "__pycache__", "venv", ".git"]); /** The most it prints in all, and of any one file. */ const MOST = 40_000; const EACH = 6_000; try { if (!statSync(root).isDirectory()) throw new Error(); } catch { console.error(`${root || "That"} isn't a folder.`); process.exit(2); } const found: { path: string; changed: number }[] = []; let other = 0; const walk = (dir: string) => { let entries; try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; } for (const entry of entries) { 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 changed = statSync(path).mtimeMs; if (changed < since) continue; if (TEXT.has(extname(entry.name).toLowerCase())) found.push({ path, changed }); else other++; } catch {} } } }; walk(root); found.sort((a, b) => b.changed - a.changed); const parts: string[] = []; let used = 0; let shown = 0; for (const one of found) { if (used >= MOST) break; let text: string; try { text = readFileSync(one.path, "utf8").replace(/\r\n/g, "\n").trim(); } catch { continue; } const room = Math.min(EACH, MOST - used); const words = text.length > room ? `${text.slice(0, room)}\n… cut, ${(text.length - room).toLocaleString("en")} characters more.` : text; parts.push(`## ${relative(root, one.path)} (${new Date(one.changed).toLocaleString()})\n\n${words}\n`); used += words.length; shown++; } console.log( [ `# Text files in ${root} changed in the last ${days} day${days === 1 ? "" : "s"}`, `${found.length} found${shown < found.length ? `, the newest ${shown} here` : ""}.${other > 0 ? ` ${other} other file${other === 1 ? "" : "s"} changed too, which ${other === 1 ? "isn't" : "aren't"} text (PDFs, Word files, images and the like), so ${other === 1 ? "it isn't" : "they aren't"} read.` : ""}`, "", ...(parts.length === 0 ? ["None."] : parts), ].join("\n"), );