All files / infrastructure/scripts/src/drift/domain detect.ts

100% Statements 31/31
95.23% Branches 20/21
100% Functions 13/13
100% Lines 27/27

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        2x 7x 7x     2x 3x     2x 3x 5x 5x     2x 5x   2x 9x             2x         2x 2x   1x     2x   2x 2x                 2x 6x   2x 6x   2x       3x   3x    
import { createHash } from "node:crypto";
import { PROJECT_ROOTS, titleCase } from "../../shared/domain/layout.ts";
 
/** A file at the repo root (README.md, CLAUDE.md, package.json) maps to no project on purpose. */
export const projectRootFor = (file: string): string | undefined => {
    const [top, name] = file.split("/");
    return top && name && PROJECT_ROOTS.includes(top) ? `${top}/${name}` : undefined;
};
 
export const projectRootsFor = (files: string[]): string[] => [
    ...new Set(files.map(projectRootFor).filter((root): root is string => root !== undefined)),
];
 
export const parseLinesChanged = (numstat: string[]): number =>
    numstat.reduce((sum, line) => {
        const [added, deleted] = line.split("\t");
        return sum + (Number(added) || 0) + (Number(deleted) || 0);
    }, 0);
 
export const hasRename = (nameStatus: string[]): boolean =>
    nameStatus.some((line) => line.startsWith("R"));
 
export const fingerprint = (diff: string): string =>
    createHash("sha256").update(diff).digest("hex");
 
interface FingerprintTransition {
    seen: Record<string, string>;
    isNew: boolean;
}
 
export const recordFingerprint = (
    seen: Record<string, string>,
    root: string,
    diff: string,
): FingerprintTransition => {
    const value = fingerprint(diff);
    if (seen[root] === value) return { seen, isNew: false };
 
    return { seen: { ...seen, [root]: value }, isNew: true };
};
 
const STALE_DOCS_MS = 120 * 24 * 60 * 60 * 1000;
 
const BIG_CHANGE_LINES = 200;
const BIG_CHANGE_FILES = 8;
 
export interface ChangeSize {
    linesChanged: number;
    filesChanged: number;
    renamed: boolean;
}
 
/** `undefined` means no markdown file under the project has ever been committed — treated as stale. */
export const isStale = (lastMdCommitMs: number | undefined, now: number): boolean =>
    lastMdCommitMs === undefined || now - lastMdCommitMs >= STALE_DOCS_MS;
 
export const isBigChange = ({ linesChanged, filesChanged, renamed }: ChangeSize): boolean =>
    renamed || linesChanged >= BIG_CHANGE_LINES || filesChanged >= BIG_CHANGE_FILES;
 
export const shouldWarn = (
    size: ChangeSize,
    lastMdCommitMs: number | undefined,
    now = Date.now(),
): boolean => isStale(lastMdCommitMs, now) || isBigChange(size);
 
export const displayName = (root: string): string =>
    (root.split("/").pop() ?? root).split("-").map(titleCase).join(" ");