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

76.59% Statements 36/47
66.66% Branches 8/12
62.5% Functions 15/24
77.27% Lines 34/44

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                1x   1x           4x 4x   4x 1x 3x 3x       4x     1x 5x 5x 5x 2x   1x 5x 5x   2x             1x 2x 3x   3x               1x 1x 3x 2x 2x       1x 1x   2x 2x       1x           1x                     1x        
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { join, relative } from "node:path";
import { at } from "../../shared/adapters/git.ts";
import { PROJECT_ROOTS } from "../../shared/domain/layout.ts";
import type { PackageSource } from "../domain/contracts.ts";
import type { NuxtSources } from "../domain/sides.ts";
import type { WorkflowSource } from "../domain/workflows.ts";
 
const GENERATED = new Set([".nuxt", ".output", "node_modules", "coverage", "dist", "public"]);
 
const filesUnder = (
    directory: string,
    root: string,
    found: string[] = [],
    skip: ReadonlySet<string> = new Set(),
): string[] => {
    for (const entry of readdirSync(directory, { withFileTypes: true })) {
        const path = join(directory, entry.name);
 
        if (entry.isDirectory()) {
            Eif (!skip.has(entry.name)) filesUnder(path, root, found, skip);
        } else Eif (entry.isFile()) {
            found.push(relative(root, path).replaceAll("\\", "/"));
        }
    }
 
    return found;
};
 
const directoriesIn = (root: string): string[] =>
    readdirSync(at(root), { withFileTypes: true })
        .filter((entry) => entry.isDirectory())
        .map((entry) => entry.name)
        .sort((a, b) => a.localeCompare(b));
 
const parse = (path: string): { manifest: unknown; readError?: string } => {
    try {
        return { manifest: JSON.parse(readFileSync(path, "utf8")) as unknown };
    } catch (error) {
        return {
            manifest: undefined,
            readError: error instanceof Error ? error.message : String(error),
        };
    }
};
 
export const readPackages = (): PackageSource[] =>
    directoriesIn("packages").map((directory) => {
        const packageRoot = at("packages", directory);
 
        return {
            directory,
            files: filesUnder(packageRoot, packageRoot),
            ...parse(join(packageRoot, "package.json")),
        };
    });
 
/** Repo-relative roots of every workspace project, e.g. `apps/huella-legal`. */
export const projectRoots = (): string[] =>
    PROJECT_ROOTS.flatMap((root) =>
        directoriesIn(root)
            .map((directory) => `${root}/${directory}`)
            .filter((project) => existsSync(at(project, "package.json"))),
    );
 
/** The root manifest and every project's, repo-relative; one that fails to parse is left out. */
export const readManifests = (): { path: string; manifest: unknown }[] =>
    ["package.json", ...projectRoots().map((project) => `${project}/package.json`)].flatMap(
        (path) => {
            const { manifest, readError } = parse(at(path));
            return readError === undefined ? [{ path, manifest }] : [];
        },
    );
 
export const readTableSources = (): { boundaries: string; readme: string } => ({
    boundaries: readFileSync(at("packages/configs/src/eslint/lib/boundaries.ts"), "utf8"),
    readme: readFileSync(at("README.md"), "utf8"),
});
 
/** Every `.ts` and `.vue` source of each app with a `nuxt.config.ts`, generated output left out. */
export const readNuxtSources = (): NuxtSources[] =>
    projectRoots()
        .filter((project) => existsSync(at(project, "nuxt.config.ts")))
        .map((app) => {
            const root = at(app);
            const files = filesUnder(root, root, [], GENERATED)
                .filter((path) => /\.(?:ts|vue)$/.test(path))
                .map((path) => ({ path, text: readFileSync(join(root, path), "utf8") }));
            return { app, files };
        });
 
export const readWorkflows = (): WorkflowSource[] =>
    readdirSync(at(".github/workflows"))
        .filter((file) => /\.ya?ml$/.test(file))
        .map((file) => ({ file, text: readFileSync(at(".github/workflows", file), "utf8") }));