All files / infrastructure/scripts/src/invariants/domain bans.ts

100% Statements 34/34
100% Branches 18/18
100% Functions 11/11
100% Lines 25/25

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                    2x                 2x       2x 41x   20x   2x 20x   10x               3x               3x               2x 21x   20x 20x   20x 20x   20x 9x 1x 36x   9x     20x 20x 3x     20x    
import type { Finding } from "./findings.ts";
 
interface ManifestSource {
    /** Repo-relative, e.g. `packages/content/package.json`. */
    path: string;
    manifest: unknown;
    /** `version` at the base ref; undefined when no base was given or the file is new there. */
    baseVersion?: string;
}
 
const LIFECYCLE_SCRIPTS = new Set([
    "preinstall",
    "install",
    "postinstall",
    "prepare",
    "prepublish",
    "prepublishOnly",
]);
 
const SOURCE_ONLY_FIELDS = ["main", "types", "typings", "module"];
 
type JsonObject = Record<string, unknown>;
 
const isObject = (value: unknown): value is JsonObject =>
    typeof value === "object" && value !== null && !Array.isArray(value);
 
const isPackage = (path: string): boolean => /^packages\/[^/]+\/package\.json$/.test(path);
 
const scriptNames = (manifest: JsonObject): string[] =>
    isObject(manifest.scripts) ? Object.keys(manifest.scripts) : [];
 
const lifecycleFinding = (path: string, scripts: string[]): Finding => ({
    id: "lifecycle-script",
    title: "A lifecycle script is declared",
    detail: `${path} declares ${scripts.join(", ")}. CI installs with --ignore-scripts, so it would work locally and do nothing there.`,
    evidence: [path],
    blocks: true,
});
 
const buildFinding = (path: string, fields: string[]): Finding => ({
    id: "package-build-step",
    title: "A package declares a build step",
    detail: `${path} declares ${fields.join(", ")}. packages/* export raw source through their exports map, with no build script, main or types.`,
    evidence: [path],
    blocks: true,
});
 
const versionFinding = (path: string, from: string, to: string): Finding => ({
    id: "version-hand-edit",
    title: "A version was edited by hand",
    detail: `${path} moved from ${from} to ${to}. nx release derives versions from Conventional Commits.`,
    evidence: [path],
    blocks: true,
});
 
export const manifestBans = ({ path, manifest, baseVersion }: ManifestSource): Finding[] => {
    if (!isObject(manifest)) return [];
 
    const findings: Finding[] = [];
    const scripts = scriptNames(manifest);
 
    const lifecycle = scripts.filter((name) => LIFECYCLE_SCRIPTS.has(name));
    if (lifecycle.length > 0) findings.push(lifecycleFinding(path, lifecycle));
 
    if (isPackage(path)) {
        const buildFields = [
            ...scripts.filter((name) => name === "build").map(() => "a build script"),
            ...SOURCE_ONLY_FIELDS.filter((field) => field in manifest),
        ];
        if (buildFields.length > 0) findings.push(buildFinding(path, buildFields));
    }
 
    const { version } = manifest;
    if (baseVersion !== undefined && typeof version === "string" && version !== baseVersion) {
        findings.push(versionFinding(path, baseVersion, version));
    }
 
    return findings;
};