All files / infrastructure/scripts/src/shared/domain logs.ts

98% Statements 49/50
94.73% Branches 18/19
100% Functions 11/11
100% Lines 37/37

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    3x     3x                             3x   3x 3x   3x               12x   3x 10x   271x 2689x   268x 10x 10x   10x     3x 5x   5x 257x 6x 34x     30x       3x 10x 10x   5x 111x   10x 10x     10x 39x 39x 38x 38x 39x 39x     5x    
// GitHub's job logs spell the escape character as the two characters `^[`.
// eslint-disable-next-line no-control-regex, sonarjs/no-control-regex -- matching the escape character is the point
const ANSI = /(?:\u001b|\^\[)\[[0-9;?]*[A-Z]/gi;
 
/** Lines every tool prints around a failure that never help fix it. */
const NOISE = [
    /Nx Cloud/,
    /sufficient access to this workspace/,
    /^Re-run with --output-style/,
    /^View logs and run details/,
    /^\s*(Run duration|Cache|Critical path|Recoverable time):/,
    /^\[ELIFECYCLE\]/,
    /^Warning: command ".*" exited with non-zero status code/,
    /^\s*(Start at|Duration|Isolate)\s/,
    /^\s+at least ~\d/,
    /^\s*RUN\s+v\d/,
];
 
// The lookarounds skip `error` inside a path, such as a passing `app/error.spec.ts`.
const SIGNAL =
    /(?<![\w./-])error(?![\w./-])|\bFAIL\b|✖|×|AssertionError|Expected|Received|\bTS\d{4}\b/i;
 
const CONTEXT_BEFORE = 1;
const CONTEXT_AFTER = 4;
 
const DEFAULT_CAP = 40;
 
export interface Excerpt {
    lines: string[];
    /** Lines of the cleaned log that didn't make the cut. */
    omitted: number;
}
 
export const stripAnsi = (text: string): string => text.replaceAll(ANSI, "").replaceAll("\r", "");
 
const clean = (text: string): string[] => {
    const lines = stripAnsi(text)
        .split("\n")
        .map((line) => line.trimEnd())
        .filter((line) => !NOISE.some((pattern) => pattern.test(line)));
 
    const collapsed = lines.filter((line, index) => line || lines[index - 1]);
    while (collapsed[0] === "") collapsed.shift();
    while (collapsed.at(-1) === "") collapsed.pop();
 
    return collapsed;
};
 
const signalWindows = (lines: string[]): number[] => {
    const kept = new Set<number>();
 
    lines.forEach((line, index) => {
        if (!SIGNAL.test(line)) return;
        const last = Math.min(lines.length - 1, index + CONTEXT_AFTER);
        for (let at = Math.max(0, index - CONTEXT_BEFORE); at <= last; at++) kept.add(at);
    });
 
    return [...kept].sort((a, b) => a - b);
};
 
/** The whole log when it fits; otherwise the error lines and their context, gaps marked `…`. */
export const excerpt = (text: string, cap = DEFAULT_CAP): Excerpt => {
    const lines = clean(text);
    if (lines.length <= cap) return { lines, omitted: 0 };
 
    const windows = signalWindows(lines);
    const picked = windows.length > 0 ? windows : lines.map((_, index) => index).slice(-cap);
 
    const shown: string[] = [];
    let count = 0;
    let previous: number | undefined;
 
    for (const index of picked) {
        const gap = previous !== undefined && index > previous + 1;
        if (shown.length + (gap ? 2 : 1) > cap) break;
        if (gap) shown.push("…");
        shown.push(lines[index] ?? "");
        count++;
        previous = index;
    }
 
    return { lines: shown, omitted: lines.length - count };
};