All files / src/utils capped-log.ts

100% Statements 8/8
100% Branches 6/6
100% Functions 1/1
100% Lines 7/7

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        42x 42x 42x 41x 2x 2x   41x          
import { node } from "../binary-manager";
 
/** Append to a log file, first trimming it to its newest `maxBytes` tail. Never throws. */
export function appendCapped(path: string, chunk: string, maxBytes: number): void {
    try {
        const dir = node.dirname(path);
        if (!node.existsSync(dir)) node.mkdirSync(dir, { recursive: true });
        if (node.existsSync(path) && node.statSync(path).size > maxBytes) {
            const content = node.readFileSync(path, "utf-8");
            node.writeFileSync(path, content.slice(-maxBytes));
        }
        node.appendFileSync(path, chunk);
    } catch {
        // capture must never break the caller
    }
}