Files
siftlode/frontend/src/lib/rawSources.testkit.ts
T
peter d44d8d6dda fix(gate): knip sees the watch entry, the build must emit watch.html, and two hand copies are held
- The raw-source contract tests read files from disk (rawSources.testkit)
  instead of import.meta.glob, which knip counted as importing every src file —
  its unused-files lane could never name an orphan. knip now names
  src/watchMain.tsx as an entry (registered in docs/gates.md).
- vite build fails when index.html or watch.html is not emitted.
- The watch-entry import walk follows dynamic import() too.
- audit_metrics.py's plan-code patterns are held equal to the guard's.
- CrashBoundary is domain-free: it moves into components/ui.
2026-10-06 03:19:33 +02:00

34 lines
1.8 KiB
TypeScript

import { globSync, readFileSync } from "node:fs";
import { matchesGlob, relative, resolve, sep } from "node:path";
// The source text a contract test scans, read from disk. Not `import.meta.glob(…, { query: "?raw" })`:
// knip reads every such glob as an import of each file it matches, and two tests globbed all of src —
// so its unused-files lane counted every source file as used and could never name an orphaned one
// (the shared-link entry was "used" only by those globs).
/** Every file matching `patterns` (relative to `dir`, the calling test's directory; a leading "!"
* excludes) with its text, keyed the way `import.meta.glob` keyed it: "./x.tsx", "../lib/y.ts". */
export function readSources(dir: string, patterns: string | string[]): Record<string, string> {
const list = typeof patterns === "string" ? [patterns] : patterns;
const include = list.filter((p) => !p.startsWith("!"));
const exclude = list.filter((p) => p.startsWith("!")).map((p) => key(p.slice(1)));
const out: Record<string, string> = {};
for (const pattern of include) {
for (const rel of globSync(pattern, { cwd: dir })) {
// Canonical, as Vite's: "../ui/Modal.tsx" from inside ui/ is "./Modal.tsx", matched once.
const k = key(relative(dir, resolve(dir, rel)));
// An exclusion names the file either way: "!../**/*.test.*" as the pattern spelled it
// ("../lib/x.test.ts"), "!./**" by its canonical key ("./x.test.ts" from inside lib/).
if (exclude.some((x) => matchesGlob(key(rel), x) || matchesGlob(k, x))) continue;
out[k] = readFileSync(resolve(dir, rel), "utf8");
}
}
return out;
}
/** A path relative to the test's directory, as `import.meta.glob` spells it. */
function key(path: string): string {
const posix = path.split(sep).join("/");
return posix.startsWith(".") ? posix : `./${posix}`;
}