Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 1x 1x 1x 1x 8x 8x 8x 4x 3x 4x 20x 8x 4x 1x 4x 1x 1x 4x 4x 4x 1x 4x 4x 4x | import { readdir, readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { parseDisables, summarizeLint } from "#layers/telemetry/shared/lintUsage.ts";
import type { UsageArtifact } from "#layers/snapshot/shared/types/UsageArtifact.ts";
import { parseLog, summarize, windowStart } from "#layers/telemetry/shared/usage.ts";
import { enabledRules } from "../lib/lintRules.ts";
import { WORKSPACE_ROOT } from "../lib/paths.ts";
import { git } from "../lib/run.ts";
/** One CSV per branch and log, written by `pnpm usage:flush` from the pre-commit hook. */
const LOG_ROOT = resolve(WORKSPACE_ROOT, "data/telemetry");
const SCRIPTS_DIR = resolve(WORKSPACE_ROOT, "infrastructure/scripts/src");
/** `shared/` is what every script uses, and `usage/` is the flush, which never records itself. */
const NOT_RECORDED = new Set(["shared", "usage"]);
/** No log folder yet is the state of a repo nobody has committed a row in, so it reads as none. */
const readLog = async (log: string): Promise<string[]> => {
const dir = resolve(LOG_ROOT, log);
const files = await readdir(dir).catch(() => []);
return Promise.all(
files
.filter((file) => file.endsWith(".csv"))
.map((file) => readFile(resolve(dir, file), "utf8")),
);
};
const knownScripts = async (): Promise<string[]> =>
(await readdir(SCRIPTS_DIR, { withFileTypes: true }))
.filter((entry) => entry.isDirectory() && !NOT_RECORDED.has(entry.name))
.map((entry) => entry.name)
.sort((a, b) => a.localeCompare(b));
/** Specs hold directives as test fixtures, and a file deleted but not yet committed has nothing to read. */
const countDisables = async (files: string[]): Promise<Record<string, number>> => {
const sources = await Promise.all(
files
.filter((file) => !file.endsWith(".spec.ts"))
.map((file) => readFile(resolve(WORKSPACE_ROOT, file), "utf8").catch(() => "")),
);
const counts: Record<string, number> = {};
for (const rule of sources.flatMap(parseDisables)) counts[rule] = (counts[rule] ?? 0) + 1;
return counts;
};
export const collectUsage = async (generatedAt: string): Promise<UsageArtifact> => {
const tracked = (await git(["ls-files", "--", "*.ts", "*.vue"])).split("\n").filter(Boolean);
const [known, scriptLogs, lintLogs, enabled, disabled] = await Promise.all([
knownScripts(),
readLog("scripts"),
readLog("lint"),
enabledRules(tracked),
countDisables(tracked),
]);
return {
...summarize(scriptLogs.flatMap(parseLog), known, generatedAt),
lint: summarizeLint(
lintLogs.flatMap(parseLog),
enabled,
disabled,
windowStart(generatedAt),
),
};
};
|