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

100% Statements 22/22
100% Branches 4/4
100% Functions 6/6
100% Lines 19/19

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                                                            1x 5x   5x                                 1x 5x 5x           5x               1x 5x   5x 6x   3x 2x 2x         2x 1x     2x 3x 3x      
import type { Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { out } from "../shared/adapters/io.ts";
import { PROJECT_ROOTS } from "../shared/domain/layout.ts";
import {
    docs,
    hookNames,
    MAP_PATH,
    projectManifests,
    projectScripts,
    readBoundaries,
    readMap,
    readReadme,
    rootScripts,
    skillFiles,
    workflowFiles,
    writeMap,
    writeReadme,
} from "./adapters/files.ts";
import { hooks, projectCommands, rootCommands, skills, workflows } from "./domain/capabilities.ts";
import { render } from "./domain/render.ts";
import { constraintsIn, README_PATH, readmeRegions, spliceRegions } from "./domain/readme.ts";
 
interface Output {
    path: string;
    current: string;
    rendered: string;
    write: (markdown: string) => void;
}
 
const featureMap = (): Output => {
    const scripts = rootScripts();
 
    return {
        path: MAP_PATH,
        current: readMap(),
        rendered: render(
            [
                ...rootCommands(scripts),
                ...projectCommands(projectScripts(), scripts),
                ...hooks(hookNames()),
                ...workflows(workflowFiles()),
                ...skills(skillFiles()),
            ],
            docs(),
        ),
        write: writeMap,
    };
};
 
const readme = (): Output => {
    const current = readReadme();
    const regions = readmeRegions(
        constraintsIn(readBoundaries()),
        projectManifests(),
        PROJECT_ROOTS,
    );
 
    return {
        path: README_PATH,
        current,
        rendered: spliceRegions(current, regions),
        write: writeReadme,
    };
};
 
export const main = ({ flags }: Args): void => {
    const outputs = [featureMap(), readme()];
 
    if (flags.has("check")) {
        const stale = outputs.filter(({ current, rendered }) => current !== rendered);
 
        if (stale.length > 0) {
            const paths = stale.map(({ path }) => path).join(" and ");
            throw new ExpectedError(
                `${paths} out of date. Run \`pnpm docs:map\` and commit the result.`,
            );
        }
 
        out.success(`${outputs.map(({ path }) => path).join(" and ")} up to date.`);
        return;
    }
 
    for (const { path, rendered, write } of outputs) {
        write(rendered);
        out.success(`Wrote ${path}.`);
    }
};