All files / apps/developer-portal/tools/collect index.ts

95% Statements 57/60
83.33% Branches 10/12
91.66% Functions 22/24
100% Lines 54/54

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 135 136 137 138 139 140 141 142 143 144 145 146                                16x   6x   4x 20x                         4x           19x   19x 6x 6x 6x     13x 13x 13x 13x     4x 4x   3x 3x   3x     4x 4x 4x   4x   4x 4x 4x     4x 4x               4x     4x 3x     4x 1x 1x 1x     3x     3x   2x     3x       3x         2x     3x     3x 2x     3x     3x 2x     3x     3x   2x     3x 3x   3x     4x  
import { mkdir, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import type { ArtifactStatus } from "#layers/snapshot/shared/types/ArtifactStatus.ts";
import type { Manifest } from "#layers/snapshot/shared/types/Manifest.ts";
import { toBadges } from "./badges.ts";
import { collectCoverage } from "./coverage.ts";
import { collectDeps } from "./deps.ts";
import { collectDocs } from "./docs.ts";
import { collectGraph } from "./graph.ts";
import { collectMetrics } from "./metrics.ts";
import { collectRetired } from "./retired.ts";
import { collectUsage } from "./usage.ts";
import { SCHEMA_VERSION, SNAPSHOT_DIR } from "../lib/paths.ts";
import { git, tryRun } from "../lib/run.ts";
 
// eslint-disable-next-line no-console -- each collector's result line is this CLI's output
const print = (line: string) => console.log(line);
// eslint-disable-next-line no-console -- a failed collector is reported on stderr
const printError = (line: string) => console.error(line);
 
const write = async (name: string, value: unknown): Promise<void> => {
    await writeFile(
        resolve(SNAPSHOT_DIR, `${name}.json`),
        `${JSON.stringify(value, null, 4)}\n`,
        "utf8",
    );
};
 
interface Run {
    generatedAt: string;
    artifacts: Record<string, ArtifactStatus>;
}
 
/** Runs one collector, writes its artifact and records the outcome; null when it failed. */
const step = async <T>(
    run: Run,
    name: string,
    collect: () => Promise<T>,
    summary: (value: T) => string,
): Promise<T | null> => {
    const result = await tryRun(collect);
 
    if (!result.ok) {
        run.artifacts[name] = { generatedAt: run.generatedAt, ok: false, error: result.error };
        printError(`✗ ${name} — ${result.error}`);
        return null;
    }
 
    await write(name, result.value);
    run.artifacts[name] = { generatedAt: run.generatedAt, ok: true };
    print(`✓ ${name} — ${summary(result.value)}`);
    return result.value;
};
 
const collectProjects = async (generatedAt: string) => {
    const graph = await collectGraph(generatedAt);
 
    const retired = await tryRun(() => collectRetired(graph.projects));
    Iif (!retired.ok) printError(`✗ retired projects — ${retired.error}`);
 
    return { ...graph, retired: retired.ok ? retired.value : [] };
};
 
const main = async (): Promise<void> => {
    const run: Run = { generatedAt: new Date().toISOString(), artifacts: {} };
    const { generatedAt } = run;
 
    await mkdir(SNAPSHOT_DIR, { recursive: true });
 
    const [commit, branch] = await Promise.all([
        git(["rev-parse", "--short", "HEAD"]).then((out) => out.trim()),
        git(["rev-parse", "--abbrev-ref", "HEAD"]).then((out) => out.trim()),
    ]);
 
    const writeManifest = () =>
        write("manifest", {
            schemaVersion: SCHEMA_VERSION,
            generatedAt,
            commit,
            branch,
            artifacts: run.artifacts,
        } satisfies Manifest);
 
    const graph = await step(
        run,
        "projects",
        () => collectProjects(generatedAt),
        (value) => `${value.projects.length} projects`,
    );
 
    if (!graph) {
        await writeManifest();
        process.exitCode = 1;
        return;
    }
 
    const coverage = await step(
        run,
        "coverage",
        () => collectCoverage(graph.projects, generatedAt),
        (value) =>
            `${value.projects.filter((project) => project.collected).length}/${value.projects.length} projects collected`,
    );
 
    const metrics = await step(
        run,
        "metrics",
        () =>
            collectMetrics(
                graph.projects,
                coverage ?? { generatedAt, totals: null, report: false, projects: [] },
                generatedAt,
            ),
        (value) => `${value.invariantFindings.length} invariant findings`,
    );
 
    const deps = await step(
        run,
        "deps",
        () => collectDeps(generatedAt),
        (value) => `${value.advisories.length} advisories, ${value.outdated?.length ?? 0} outdated`,
    );
 
    const docs = await step(
        run,
        "docs",
        () => collectDocs(generatedAt),
        (value) => `${value.pages.length} pages, ${value.brokenLinkCount} broken links`,
    );
 
    await step(
        run,
        "usage",
        () => collectUsage(generatedAt),
        (value) =>
            `${Object.values(value.scripts).reduce((sum, item) => sum + item.runs, 0)} runs across ${Object.keys(value.scripts).length} scripts`,
    );
 
    await write("badges", toBadges({ deps, coverage, docs, metrics }));
    await writeManifest();
 
    print(`\nSnapshot written to ${SNAPSHOT_DIR}`);
};
 
await main();