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

93.33% Statements 70/75
96% Branches 24/25
88.57% Functions 31/35
98.36% Lines 60/61

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134              13x   1x 5x   4x   1x 15x 15x   10x   7x       11x   5x   1x 3x   1x   1x 3x 9x 4x 4x 4x 4x                   1x         1x 2x   1x 2x   1x 2x 3x 2x         1x 2x 1x   1x 1x 1x         1x         2x     1x                   1x 5x 10x   10x 4x 6x 5x     5x     1x     1x 1x 5x 4x   1x 1x 1x   1x       1x   1x   1x   1x  
import { readdirSync, readFileSync, statSync, writeFileSync, type Dirent } from "node:fs";
import { join, relative } from "node:path";
import { at, repoRoot } from "../../shared/adapters/git.ts";
import { PROJECT_ROOTS } from "../../shared/domain/layout.ts";
import type { Doc, ProjectScripts } from "../domain/capabilities.ts";
import { BOUNDARIES_PATH, README_PATH, type ProjectManifest } from "../domain/readme.ts";
 
const read = (path: string): string => readFileSync(path, "utf8");
 
const readJson = (path: string): Record<string, unknown> =>
    JSON.parse(read(path)) as Record<string, unknown>;
 
const scriptNames = (pkg: Record<string, unknown>): string[] => Object.keys(pkg.scripts ?? {});
 
const entriesIn = (path: string, keep: (entry: Dirent) => boolean): string[] => {
    try {
        return readdirSync(path, { withFileTypes: true })
            .filter(keep)
            .map((entry) => entry.name);
    } catch {
        return [];
    }
};
 
const dirsIn = (path: string): string[] => entriesIn(path, (entry) => entry.isDirectory());
 
const filesIn = (path: string): string[] => entriesIn(path, (entry) => entry.isFile());
 
const nameField = (text: string): string | undefined =>
    /^name:[^\S\n]*(\S.*)$/m.exec(text)?.[1]?.trim();
 
export const rootScripts = (): string[] => scriptNames(readJson(at("package.json")));
 
export const projectManifests = (): (ProjectManifest & { scripts: string[] })[] =>
    PROJECT_ROOTS.flatMap((top) =>
        dirsIn(at(top)).flatMap((dir) => {
            try {
                const pkg = readJson(at(top, dir, "package.json"));
                const nx = (pkg.nx ?? {}) as { tags?: string[] };
                return [
                    {
                        root: `${top}/${dir}`,
                        name: String(pkg.name),
                        description: typeof pkg.description === "string" ? pkg.description : "",
                        tags: nx.tags ?? [],
                        scripts: scriptNames(pkg),
                    },
                ];
            } catch {
                return [];
            }
        }),
    );
 
export const projectScripts = (): ProjectScripts[] =>
    projectManifests().map(({ root, name, scripts }) => ({ root, name, scripts }));
 
export const hookNames = (): string[] =>
    filesIn(at(".husky")).filter((name) => !name.startsWith("_"));
 
export const workflowFiles = (): { file: string; name: string }[] =>
    filesIn(at(".github", "workflows"))
        .filter((file) => file.endsWith(".yml") || file.endsWith(".yaml"))
        .map((file) => ({
            file,
            name: nameField(read(at(".github", "workflows", file))) ?? file,
        }));
 
const skillsUnder = (dir: string): { source: string; name: string }[] =>
    dirsIn(at(dir))
        .map((skill) => `${dir}/${skill}/SKILL.md`)
        .filter((source) => {
            try {
                statSync(at(source));
                return true;
            } catch {
                return false;
            }
        })
        .map((source) => ({
            source,
            name: nameField(read(at(source))) ?? source,
        }));
 
export const skillFiles = (): { source: string; name: string }[] => skillsUnder(".claude/skills");
 
/** Ignore linked worktrees or duplicate docs would make the map branch-dependent. */
const IGNORED_DIRS = new Set([
    "node_modules",
    ".git",
    ".nx",
    "dist",
    ".output",
    ".nuxt",
    "worktrees",
]);
 
const walkMarkdown = (dir: string, found: string[] = []): string[] => {
    for (const entry of readdirSync(dir, { withFileTypes: true })) {
        const child = join(dir, entry.name);
 
        if (entry.isDirectory() && !IGNORED_DIRS.has(entry.name)) {
            walkMarkdown(child, found);
        } else if (entry.isFile() && entry.name.endsWith(".md")) {
            found.push(relative(repoRoot(), child));
        }
    }
    return found;
};
 
export const MAP_PATH = "docs/FEATURES.md";
 
/** Two deliberate exclusions from the markdown scan — see ./README.md#picking-the-doc. */
export const docs = (): Doc[] =>
    walkMarkdown(repoRoot())
        .filter((path) => !path.endsWith("CHANGELOG.md") && path !== MAP_PATH)
        .map((path) => ({ path, text: read(at(path)) }));
 
export const readMap = (): string => {
    try {
        return read(at(MAP_PATH));
    } catch {
        return "";
    }
};
 
export const writeMap = (markdown: string): void => writeFileSync(at(MAP_PATH), markdown);
 
export const readBoundaries = (): string => read(at(BOUNDARIES_PATH));
 
export const readReadme = (): string => read(at(README_PATH));
 
export const writeReadme = (markdown: string): void => writeFileSync(at(README_PATH), markdown);