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

96.15% Statements 25/26
93.75% Branches 15/16
90% Functions 9/10
95.83% Lines 23/24

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            5x                 5x 9x 9x   9x       9x 9x   9x         7x 7x   71x 26x     7x 7x         7x                         1x         6x 4x   4x   1x     5x 4x                             4x                    
import { dirname } from "node:path";
import type { ProjectNode } from "#layers/snapshot/shared/types/ProjectNode.ts";
import type { RetiredProject } from "#layers/snapshot/shared/types/RetiredProject.ts";
import { git } from "../lib/run.ts";
import { isDeclaredTag } from "./graph.ts";
 
const MANIFESTS = [
    "apps/*/package.json",
    "packages/*/package.json",
    "infrastructure/*/package.json",
];
 
type Deletion = Omit<RetiredProject, "commits">;
 
/** One project per deleted `package.json`, read from the patch since `git show` is off the allowlist. */
export const parseDeletions = (log: string, live: ProjectNode[]): Deletion[] => {
    const liveNames = new Set(live.map((project) => project.name));
    const seen = new Set<string>();
 
    return log
        .split("\0")
        .filter(Boolean)
        .flatMap((entry) => {
            const [header = "", ...patch] = entry.split("\n");
            const [commit = "", deletedAt = "", subject = ""] = header.split("\t");
 
            return patch
                .join("\n")
                .split(/^diff --git /m)
                .slice(1)
                .flatMap((file): Deletion[] => {
                    const path = /^--- a\/(.+)$/m.exec(file)?.[1];
                    const body = file
                        .split("\n")
                        .filter((line) => line.startsWith("-") && !line.startsWith("--- "))
                        .map((line) => line.slice(1))
                        .join("\n");
 
                    try {
                        const manifest = JSON.parse(body) as {
                            name?: string;
                            nx?: { tags?: string[] };
                        };
 
                        return path && manifest.name
                            ? [
                                  {
                                      name: manifest.name,
                                      root: dirname(path),
                                      tags: (manifest.nx?.tags ?? []).filter(isDeclaredTag),
                                      deletedAt,
                                      commit,
                                      subject,
                                  },
                              ]
                            : [];
                    } catch {
                        return [];
                    }
                });
        })
        .filter((project) => {
            if (liveNames.has(project.name) || seen.has(project.name)) return false;
            seen.add(project.name);
 
            return true;
        })
        .sort((a, b) => a.name.localeCompare(b.name));
};
 
export const collectRetired = async (live: ProjectNode[]): Promise<RetiredProject[]> => {
    const log = await git([
        "log",
        "--diff-filter=D",
        "-p",
        // A user's diff config would otherwise decide what counts as deleted and how the patch reads.
        "--find-renames",
        "--no-ext-diff",
        "--no-color",
        "--src-prefix=a/",
        "--dst-prefix=b/",
        "--format=%x00%H%x09%cs%x09%s",
        "--",
        ...MANIFESTS,
    ]);
 
    return Promise.all(
        parseDeletions(log, live).map(async (project) => ({
            ...project,
            commits: Number.parseInt(
                (await git(["rev-list", "--count", project.commit, "--", project.root])).trim(),
                10,
            ),
        })),
    );
};