All files / infrastructure/scripts/src/drift main.ts

100% Statements 34/34
83.33% Branches 15/18
100% Functions 2/2
100% Lines 31/31

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 70 71 72 73 74 75 76                                  1x 1x     1x 6x 6x   6x 2x 2x     4x   4x         4x 1x 1x     1x         1x 1x     1x 9x 9x   9x 9x   8x   9x 8x 8x   7x 7x 7x     7x   3x 3x      
import { confirm } from "@clack/prompts";
import process from "node:process";
import { readCache, writeCache } from "../shared/adapters/cache.ts";
import { createIssue } from "../shared/adapters/gh.ts";
import { isInteractive, out } from "../shared/adapters/io.ts";
import { orExit } from "../shared/adapters/prompts.ts";
import {
    changedFiles,
    diffNameStatus,
    diffNumstat,
    diffText,
    lastMdCommitEpochSeconds,
    mergeBase,
} from "./adapters/git.ts";
import { displayName, projectRootsFor, recordFingerprint, shouldWarn } from "./domain/detect.ts";
import { changeSize, lastMdCommitMs } from "./domain/size.ts";
 
const PROJECT = "Joinery";
const CACHE_KEY = "drift-fingerprints";
 
/** Resolves to whether someone answered, which is what makes the diff safe to stop asking about. */
const warnFor = async (root: string): Promise<boolean> => {
    const name = displayName(root);
    out.warn(`${root} changed a lot and its docs might be stale.`);
 
    if (!isInteractive()) {
        out.info("Run `pnpm docs:drift` to review and file an issue.");
        return false;
    }
 
    out.begin(`📚 Possible docs drift — ${name}`);
 
    const wantsIssue = orExit(
        await confirm({ message: "File a GitHub issue for it?" }),
        "Skipped.",
    );
 
    if (!wantsIssue) {
        out.end("Skipped — won't ask again until this project changes further.");
        return true;
    }
 
    const url = createIssue({
        title: `Address documentation drift for ${name}`,
        body: "",
        project: PROJECT,
    });
    out.end(url);
    return true;
};
 
export const main = async (): Promise<void> => {
    const base = process.env.DOCS_DRIFT_BASE ?? mergeBase("master");
    const head = process.env.DOCS_DRIFT_HEAD ?? "HEAD";
 
    const roots = projectRootsFor(changedFiles(base, head));
    if (roots.length === 0) return;
 
    let seen = readCache<Record<string, string>>(CACHE_KEY) ?? {};
 
    for (const root of roots) {
        const transition = recordFingerprint(seen, root, diffText(base, head, root));
        if (!transition.isNew) continue;
 
        const numstat = diffNumstat(base, head, root);
        const size = changeSize(numstat, diffNameStatus(base, head, root));
        const warn = shouldWarn(size, lastMdCommitMs(lastMdCommitEpochSeconds(root)));
 
        // eslint-disable-next-line no-await-in-loop -- one prompt at a time, each answer recorded
        if (warn && !(await warnFor(root))) continue;
 
        seen = transition.seen;
        writeCache(CACHE_KEY, seen);
    }
};