All files / infrastructure/scripts/src/map/domain capabilities.ts

84.44% Statements 38/45
78.26% Branches 18/23
78.94% Functions 15/19
89.47% Lines 34/38

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                    2x               2x 2x             2x 2x 2x 4x 1x               2x               2x               2x 5x               2x 19x 19x     2x 2x 2x 2x 2x 2x 2x       2x 4x 4x 1x                   2x     2x       11x   6x   3x     8x 6x     2x              
export type Kind = "command" | "hook" | "workflow" | "skill";
 
export interface Capability {
    kind: Kind;
    invocation: string;
    source: string;
    token: string;
}
 
/** Omit commands already exposed by the root so the map stays focused on project-specific capabilities. */
const STANDARD_TARGETS = ["lint", "typecheck", "test", "test:dev", "test:coverage", "build"];
 
export interface ProjectScripts {
    root: string;
    name: string;
    scripts: string[];
}
 
export const rootCommands = (scripts: string[]): Capability[] =>
    scripts.map((script) => ({
        kind: "command" as const,
        invocation: `pnpm ${script}`,
        source: "package.json",
        token: script,
    }));
 
export const projectCommands = (projects: ProjectScripts[], rootScripts: string[]): Capability[] =>
    projects.flatMap(({ root, name, scripts }) =>
        scripts
            .filter((script) => !STANDARD_TARGETS.includes(script) && !rootScripts.includes(script))
            .map((script) => ({
                kind: "command" as const,
                invocation: `pnpm exec nx ${script} ${name}`,
                source: `${root}/package.json`,
                token: script,
            })),
    );
 
export const hooks = (names: string[]): Capability[] =>
    names.map((name) => ({
        kind: "hook" as const,
        invocation: name,
        source: `.husky/${name}`,
        token: name,
    }));
 
export const workflows = (files: { file: string; name: string }[]): Capability[] =>
    files.map(({ file, name }) => ({
        kind: "workflow" as const,
        invocation: name,
        source: `.github/workflows/${file}`,
        token: file,
    }));
 
export const skills = (found: { source: string; name: string }[]): Capability[] =>
    found.map(({ source, name }) => ({
        kind: "skill" as const,
        invocation: `/${name}`,
        source,
        token: name,
    }));
 
/** Match a token without accepting a longer name on either side, such as `test:coverage` for `coverage`. */
export const mentions = (text: string, token: string): boolean => {
    const escaped = token.replace(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
    return new RegExp(String.raw`(?<![\w:-])${escaped}(?![\w:-])`).test(text);
};
 
const sharedPrefix = (a: string, b: string): number => {
    const left = a.split("/");
    const right = b.split("/");
    let shared = 0;
    while (shared < left.length && shared < right.length && left[shared] === right[shared])
        shared++;
    return shared;
};
 
/** Prefer human-facing docs before proximity so skills do not cite one another as explanations. */
const audience = (path: string): number => {
    const name = path.split("/").pop();
    if (name === "README.md") return 0;
    Eif (name === "CLAUDE.md") return 2;
    if (name === "SKILL.md") return 3;
    return 1;
};
 
export interface Doc {
    path: string;
    text: string;
}
 
const SKILL_INDEX = "CLAUDE.md";
 
/** Skills cite the CLAUDE.md table; everything else ranks by audience, then nearness. */
export const documentedBy = (
    { kind, source, token }: Capability,
    docs: Doc[],
): string | undefined => {
    if (
        kind === "skill" &&
        docs.some((doc) => doc.path === SKILL_INDEX && mentions(doc.text, token))
    ) {
        return SKILL_INDEX;
    }
 
    return docs
        .filter((doc) => doc.path !== source && mentions(doc.text, token))
        .sort(
            (a, b) =>
                audience(a.path) - audience(b.path) ||
                sharedPrefix(b.path, source) - sharedPrefix(a.path, source) ||
                a.path.split("/").length - b.path.split("/").length ||
                a.path.localeCompare(b.path),
        )
        .at(0)?.path;
};