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

100% Statements 44/44
88.23% Branches 15/17
100% Functions 21/21
100% Lines 40/40

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                            2x   2x     2x 4x 12x   12x       30x   30x   2x       6x       9x 27x 12x 3x   9x 9x       2x 9x   3x   6x     18x 3x   9x 9x 9x                       2x         4x 1x     3x 18x   3x               2x 2x 2x 2x 2x 2x   2x 1x     1x    
import { ExpectedError } from "../../shared/errors.ts";
 
export interface ProjectManifest {
    root: string;
    name: string;
    description: string;
    tags: string[];
}
 
interface Constraint {
    tag: string;
    allowed: string[];
}
 
export const README_PATH = "README.md";
 
export const BOUNDARIES_PATH = "packages/configs/src/eslint/lib/boundaries.ts";
 
/** Read as text, the way the portal's invariants do: a cross-project import would trip the boundary rule. */
export const constraintsIn = (source: string): Constraint[] =>
    [...source.matchAll(/sourceTag:\s*"([^"]+)",\s*onlyDependOnLibsWithTags:\s*\[([^\]]*)\]/g)].map(
        ([, tag = "", list = ""]) => ({
            tag,
            allowed: [...list.matchAll(/"([^"]+)"/g)].map(([, allowed = ""]) => allowed),
        }),
    );
 
const code = (value: string): string => `\`${value}\``;
 
const directory = (root: string): string => root.split("/").at(-1) ?? root;
 
const tagTable = (
    heading: string,
    constraints: Constraint[],
    projects: ProjectManifest[],
): string[] => [
    `| ${heading} | May depend on | Who has it |`,
    "| --- | --- | --- |",
    ...constraints.map(({ tag, allowed }) => {
        const holders = projects
            .filter((project) => project.tags.includes(tag))
            .map((project) => code(directory(project.root)))
            .sort((a, b) => a.localeCompare(b));
 
        const depends = allowed.length > 0 ? allowed.map(code).join(", ") : "nothing";
        return `| ${code(tag)} | ${depends} | ${holders.join(", ") || "—"} |`;
    }),
];
 
const layout = (projects: ProjectManifest[], tops: string[]): string[] => {
    const width = Math.max(...projects.map((project) => directory(project.root).length)) + 2;
 
    return [
        "```",
        ...tops.flatMap((top) => [
            `${top}/`,
            ...projects
                .filter((project) => project.root.startsWith(`${top}/`))
                .sort((a, b) => a.root.localeCompare(b.root))
                .map(({ root, name, description }) => {
                    const folder = `${directory(root)}/`.padEnd(width);
                    const line = `    ${folder}${name}`;
                    return description ? `${line} — ${description}` : line;
                }),
        ]),
        "```",
    ];
};
 
interface Region {
    name: string;
    lines: string[];
}
 
export const readmeRegions = (
    constraints: Constraint[],
    projects: ProjectManifest[],
    tops: string[],
): Region[] => {
    if (constraints.length === 0) {
        throw new ExpectedError(`No depConstraints parsed from ${BOUNDARIES_PATH}.`);
    }
 
    const ofKind = (prefix: string): Constraint[] =>
        constraints.filter(({ tag }) => tag.startsWith(prefix));
 
    return [
        { name: "layout", lines: layout(projects, tops) },
        { name: "type-tags", lines: tagTable("Tag", ofKind("type:"), projects) },
        { name: "scope-tags", lines: tagTable("Scope", ofKind("scope:"), projects) },
    ];
};
 
/** Replace what sits between each region's markers, leaving the prose around them hand-written. */
export const spliceRegions = (readme: string, regions: Region[]): string =>
    regions.reduce((text, { name, lines }) => {
        const open = `<!-- docs:map:${name} -->`;
        const close = `<!-- /docs:map:${name} -->`;
        const start = text.indexOf(open);
        const end = text.indexOf(close);
 
        if (start === -1 || end < start) {
            throw new ExpectedError(`${README_PATH} is missing the ${open} … ${close} markers.`);
        }
 
        return [text.slice(0, start + open.length), ...lines, text.slice(end)].join("\n");
    }, readme);