All files / infrastructure/scripts/src/shared/adapters usage.ts

100% Statements 18/18
100% Branches 1/1
100% Functions 7/7
100% Lines 18/18

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              4x 5x     4x 3x 3x 3x 3x           4x 2x 2x   1x       4x 2x 2x   4x 3x 2x       1x      
import { appendFileSync, mkdirSync, readdirSync } from "node:fs";
import { dirname, join } from "node:path";
import { PROJECT_ROOTS } from "../domain/layout.ts";
import { toLine, type UsageRow } from "../domain/usage.ts";
import { at, git } from "./git.ts";
 
/** Inside the common `.git`, so every worktree shares one buffer and the working tree stays clean. */
export const usageBufferPath = (log = "scripts"): string =>
    join(git("rev-parse", "--path-format=absolute", "--git-common-dir"), "telemetry", `${log}.csv`);
 
/** Fails open: a script must never break because its usage could not be written. */
export const appendUsage = (row: UsageRow): void => {
    try {
        const file = usageBufferPath();
        mkdirSync(dirname(file), { recursive: true });
        appendFileSync(file, `${toLine(row)}\n`);
    } catch {
        // No git checkout, a read-only disk: nothing to record into.
    }
};
 
export const currentBranch = (): string => {
    try {
        return git("branch", "--show-current");
    } catch {
        return "";
    }
};
 
export const projectNames = (): Set<string> => {
    try {
        return new Set(
            PROJECT_ROOTS.flatMap((root) =>
                readdirSync(at(root), { withFileTypes: true })
                    .filter((entry) => entry.isDirectory())
                    .map((entry) => entry.name),
            ),
        );
    } catch {
        return new Set();
    }
};