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

100% Statements 22/22
100% Branches 10/10
100% Functions 9/9
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                              1x       5x 5x   4x     5x   4x   4x     4x             8x         1x 5x 5x           4x     5x   4x           4x       1x 5x   5x                
import { relative } from "node:path";
import type { DepsArtifact } from "#layers/snapshot/shared/types/DepsArtifact.ts";
import type { OutdatedPackage } from "#layers/snapshot/shared/types/OutdatedPackage.ts";
import type { VulnerabilityAdvisory } from "#layers/snapshot/shared/types/VulnerabilityAdvisory.ts";
import { WORKSPACE_ROOT } from "../lib/paths.ts";
import { runAllowFailure, tryRun } from "../lib/run.ts";
 
interface RawAdvisory {
    id: number;
    title: string;
    module_name: string;
    severity: VulnerabilityAdvisory["severity"];
    patched_versions: string;
    url: string;
    findings: { paths: string[] }[];
}
 
interface RawAuditReport {
    advisories: Record<string, RawAdvisory>;
    metadata: {
        vulnerabilities: {
            info: number;
            low: number;
            moderate: number;
            high: number;
            critical: number;
        };
        totalDependencies: number;
    };
}
 
interface RawOutdatedEntry {
    current: string;
    wanted: string;
    latest: string;
    isDeprecated: boolean;
    dependentPackages: { name: string; location: string }[];
}
 
const collectAudit = async (): Promise<Pick<
    DepsArtifact,
    "vulnerabilities" | "totalDependencies" | "advisories"
> | null> => {
    const result = await tryRun(async () => {
        const stdout = await runAllowFailure("pnpm", ["audit", "--json"], WORKSPACE_ROOT);
 
        return JSON.parse(stdout) as RawAuditReport;
    });
 
    if (!result.ok) return null;
 
    const { advisories, metadata } = result.value;
 
    return {
        vulnerabilities: metadata.vulnerabilities,
        totalDependencies: metadata.totalDependencies,
        advisories: Object.values(advisories).map((advisory) => ({
            id: advisory.id,
            title: advisory.title,
            moduleName: advisory.module_name,
            severity: advisory.severity,
            patchedVersions: advisory.patched_versions,
            url: advisory.url,
            paths: advisory.findings.flatMap((finding) => finding.paths),
        })),
    };
};
 
const collectOutdated = async (): Promise<OutdatedPackage[] | null> => {
    const result = await tryRun(async () => {
        const stdout = await runAllowFailure(
            "pnpm",
            ["outdated", "-r", "--format", "json"],
            WORKSPACE_ROOT,
        );
 
        return JSON.parse(stdout) as Record<string, RawOutdatedEntry>;
    });
 
    if (!result.ok) return null;
 
    return Object.entries(result.value).map(([name, entry]) => ({
        name,
        current: entry.current,
        wanted: entry.wanted,
        latest: entry.latest,
        isDeprecated: entry.isDeprecated,
        dependents: entry.dependentPackages.map((pkg) => relative(WORKSPACE_ROOT, pkg.location)),
    }));
};
 
export const collectDeps = async (generatedAt: string): Promise<DepsArtifact> => {
    const [audit, outdated] = await Promise.all([collectAudit(), collectOutdated()]);
 
    return {
        generatedAt,
        vulnerabilities: audit?.vulnerabilities ?? null,
        totalDependencies: audit?.totalDependencies ?? null,
        advisories: audit?.advisories ?? [],
        outdated,
    };
};