All files / src diagnostics-export.ts

100% Statements 20/20
100% Branches 4/4
100% Functions 2/2
100% Lines 20/20

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              3x     8x         5x 5x 5x   5x 5x 5x 5x   3x 3x 3x 3x 3x   2x 2x       3x 3x       3x 3x          
import { Notice } from "obsidian";
import { shell } from "electron";
import { node } from "./binary-manager";
import { buildZip, collectDiagnostics, resolveOutputDir } from "./diagnostics";
import { MESSAGES } from "./locales/en";
import type { DiagnosticsContext } from "./types";
 
const NOTICE_DURATION_MS = 15_000;
 
function timestampSlug(): string {
    return new Date().toISOString().replace(/[:.]/g, "-");
}
 
/** Collect, zip, write, reveal, copy summary. Never throws; reports via Notice. */
export async function exportDiagnostics(ctx: DiagnosticsContext): Promise<void> {
    const bundle = collectDiagnostics(ctx);
    const outDir = resolveOutputDir(ctx.dataDir ?? ".");
    const zipPath = node.join(outDir, `lilbee-diagnostics-${timestampSlug()}.zip`);
 
    let writtenPath = zipPath;
    try {
        node.writeFileSync(zipPath, buildZip(bundle));
        new Notice(MESSAGES.NOTICE_DIAGNOSTICS_EXPORTED(zipPath), NOTICE_DURATION_MS);
    } catch {
        const summaryPath = node.join(outDir, `lilbee-diagnostics-${timestampSlug()}.summary.md`);
        try {
            node.writeFileSync(summaryPath, bundle.summaryMarkdown);
            writtenPath = summaryPath;
            new Notice(MESSAGES.NOTICE_DIAGNOSTICS_SUMMARY_ONLY(summaryPath), NOTICE_DURATION_MS);
        } catch (e) {
            new Notice(MESSAGES.NOTICE_DIAGNOSTICS_FAILED(e instanceof Error ? e.message : String(e)));
            return;
        }
    }
 
    try {
        shell.showItemInFolder(writtenPath);
    } catch {
        // notice already carries the path
    }
    try {
        await navigator.clipboard.writeText(bundle.summaryMarkdown);
    } catch {
        // clipboard is best-effort
    }
}