/**
* Per-package spec-patch statistics for the /shame page and the homepage
* award. Read-only over `packages/
/{patches,src/services}`.
*
* "Fixes" are counted from the committed patch files, not by running the
* generator: one fix per RFC-6902 operation in a file's `patches`.
*
* "Operations" are the `API.OperationMethod` / `API.PaginatedOperationMethod`
* exports in the generated services, plus the `client.operation(…)` exports a
* GraphQL package generates into `src/*.ts` instead, so the ratio is fixes per
* 100 SDK calls either way.
*/
import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
export interface PatchStats {
readonly dir: string;
readonly files: number;
readonly fixes: number;
readonly operations: number;
/** Fixes per 100 SDK operations; `null` when there are no operations. */
readonly per100: number | null;
readonly ops: Readonly>;
}
const OP_KINDS = ["add", "remove", "replace", "move", "copy", "test"] as const;
const walk = async (dir: string, out: string[] = []): Promise => {
let entries;
try {
entries = await readdir(dir, { withFileTypes: true });
} catch {
return out;
}
for (const entry of entries) {
const p = join(dir, entry.name);
if (entry.isDirectory()) await walk(p, out);
else out.push(p);
}
return out;
};
const REST_OPERATION = /^export const [A-Za-z0-9_]+: API\.(?:Paginated)?OperationMethod => {
let n = 0;
for (const file of files) {
if (!file.endsWith(".ts")) continue;
n += (await readFile(file, "utf8")).match(re)?.length ?? 0;
}
return n;
};
const listFiles = async (dir: string): Promise => {
let entries;
try {
entries = await readdir(dir, { withFileTypes: true });
} catch {
return [];
}
return entries.filter((e) => e.isFile()).map((e) => join(dir, e.name));
};
/**
* REST packages declare one `API.OperationMethod` per call under
* `src/services`; a GraphQL package has no services directory and binds its
* operations in the module beside it (`railway/src/graphql.ts`).
*/
const countOperations = async (srcDir: string): Promise =>
(await countMatches(await walk(join(srcDir, "services")), REST_OPERATION)) +
(await countMatches(await listFiles(srcDir), GRAPHQL_OPERATION));
export const readPatchStats = async (packagesDir: string, dir: string): Promise => {
const pkgRoot = join(packagesDir, dir);
const ops: Record = {};
let files = 0;
let fixes = 0;
const patchFiles = (await walk(join(pkgRoot, "patches"))).filter((f) => f.endsWith(".json"));
for (const file of patchFiles) {
let doc: unknown;
try {
doc = JSON.parse(await readFile(file, "utf8"));
} catch {
continue;
}
files++;
if (Array.isArray(doc)) {
fixes += doc.length;
for (const op of doc) tally(ops, op);
} else if (doc && typeof doc === "object") {
const rec = doc as Record;
if (Array.isArray(rec.patches)) {
fixes += rec.patches.length;
for (const op of rec.patches) tally(ops, op);
}
}
}
const operations = await countOperations(join(pkgRoot, "src"));
return {
dir,
files,
fixes,
operations,
per100: operations > 0 ? (fixes / operations) * 100 : null,
ops,
};
};
const tally = (ops: Record, op: unknown): void => {
const kind =
op && typeof op === "object" && typeof (op as { op?: unknown }).op === "string"
? (op as { op: string }).op
: "other";
const key = (OP_KINDS as readonly string[]).includes(kind) ? kind : "other";
ops[key] = (ops[key] ?? 0) + 1;
};