count-callers.ts
This file
Path
examples/burrow/scripts/count-callers.ts
61 lines · 1.9 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–61why, one step back1/**2 * Counts the calls to a function in each workspace package's `src/`:3 *4 * bun scripts/count-callers.ts legacyFetch a table5 * bun scripts/count-callers.ts legacyFetch --json {"byModule": {"@burrow/kit": n, …}, "total": n}6 *7 * A plain text scan: a call is the name followed by `(`, so the function's8 * own definition, an import or a property of another name (`x.legacyFetch`)9 * is not one. Dotted names work too: `http.get`.10 */11import { readdirSync, readFileSync } from "node:fs";12import path from "node:path";1314interface Manifest {15 readonly name: string;16}1718const [symbol, ...flags] = process.argv.slice(2);1920if (symbol === undefined || symbol.startsWith("-")) {21 throw new Error("usage: bun scripts/count-callers.ts <symbol> [--json]");22}2324const escaped = symbol.replaceAll(/[$()*+.?[\\\]^{|}]/gu, String.raw`\$&`);25const call = new RegExp(String.raw`(?<![\w.$])${escaped}\s*\(`, "gu");2627const callsIn = (dir: string) =>28 readdirSync(dir, { encoding: "utf-8", recursive: true })29 .filter((file) => /\.tsx?$/u.test(file))30 .map(31 (file) =>32 readFileSync(path.join(dir, file), "utf-8").match(call)?.length ?? 033 )34 .reduce((sum, count) => sum + count, 0);3536const byModule = Object.fromEntries(37 readdirSync("packages", { encoding: "utf-8" })38 .toSorted()39 .map((dir) => {40 const manifest: Manifest = JSON.parse(41 readFileSync(path.join("packages", dir, "package.json"), "utf-8")42 );4344 return [45 manifest.name,46 callsIn(path.join("packages", dir, "src")),47 ] as const;48 })49);5051const total = Object.values(byModule).reduce((sum, count) => sum + count, 0);5253if (flags.includes("--json")) {54 console.log(JSON.stringify({ byModule, total }));55} else {56 for (const [module, count] of Object.entries(byModule)) {57 console.log(`${module.padEnd(20)} ${count}`);58 }5960 console.log(`${"total".padEnd(20)} ${total}`);61}