// Find personal data: reads the text and CSV files in a folder and counts, in each, the email addresses, phone numbers // and numbers shaped like payment cards (those that pass the cards' own check). It prints counts and file names only, // never what it found, and changes nothing. Run it from its form in CLIDE, or here: // bun find-personal-data.ts ~/Documents import { readdirSync, readFileSync, statSync } from "node:fs"; import { extname, join, relative } from "node:path"; const root = process.argv[2] ?? ""; const TEXT = new Set([".txt", ".md", ".markdown", ".csv", ".tsv", ".eml", ".log", ".json", ".html", ".htm", ".xml", ".yaml", ".yml", ".vcf", ".ics"]); const SKIP = new Set(["node_modules", "__pycache__", "venv", ".git"]); /** Bigger files are passed over, and said so. */ const BIGGEST = 10 * 1024 * 1024; const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g; // A phone number: a + or a digit, then 9 to 14 more digits with spaces, dots, dashes or brackets between. const PHONE = /(? 9 ? d * 2 - 9 : d * 2; sum += d; } return sum % 10 === 0; } function counted(text: string): { emails: number; phones: number; cards: number } { const cards = [...text.matchAll(CARD)].map((m) => m[0].replace(/\D/g, "")).filter((digits) => digits.length >= 13 && luhn(digits)); const cardSet = new Set(cards); const phones = [...text.matchAll(PHONE)].filter((m) => !cardSet.has(m[0].replace(/\D/g, ""))); return { emails: [...text.matchAll(EMAIL)].length, phones: phones.length, cards: cards.length }; } try { if (!statSync(root).isDirectory()) throw new Error(); } catch { console.error(`${root || "That"} isn't a folder.`); process.exit(2); } const holding: { name: string; emails: number; phones: number; cards: number }[] = []; let read = 0; let unread = 0; let tooBig = 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()) { if (!TEXT.has(extname(entry.name).toLowerCase())) { unread++; continue; } try { if (statSync(path).size > BIGGEST) { tooBig++; continue; } const found = counted(readFileSync(path, "utf8")); read++; if (found.emails + found.phones + found.cards > 0) holding.push({ name: relative(root, path), ...found }); } catch { unread++; } } } }; walk(root); holding.sort((a, b) => b.cards - a.cards || b.emails + b.phones - (a.emails + a.phones)); const what = (one: { emails: number; phones: number; cards: number }) => [one.cards && `${one.cards} card-like number${one.cards === 1 ? "" : "s"}`, one.emails && `${one.emails} email address${one.emails === 1 ? "" : "es"}`, one.phones && `${one.phones} phone number${one.phones === 1 ? "" : "s"}`].filter(Boolean).join(", "); console.log( [ `# Personal data in ${root}`, `${read} text file${read === 1 ? "" : "s"} read; ${holding.length} hold personal data.`, ...(unread > 0 ? [`${unread} other file${unread === 1 ? "" : "s"} (PDFs, Word files, spreadsheets, images and the like) couldn't be read, so a file not listed here isn't known to be clean.`] : []), ...(tooBig > 0 ? [`${tooBig} text file${tooBig === 1 ? " was" : "s were"} over 10 MB and passed over.`] : []), "", ...(holding.length === 0 ? ["None found."] : holding.slice(0, 100).map((one) => `- ${one.name}: ${what(one)}`)), ...(holding.length > 100 ? [`- and ${holding.length - 100} more files`] : []), ].join("\n"), );