All files / apps/developer-portal/layers/snapshot/server/utils store.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

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              1x 4x 4x   2x               1x 4x  
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
 
/**
 * A missing or half-written artifact resolves to null rather than throwing: `.report/` is
 * gitignored, so "never collected" is the state of a fresh clone and every page renders it.
 */
const readJson = async <T>(path: string): Promise<T | null> => {
    try {
        return JSON.parse(await readFile(path, "utf8")) as T;
    } catch {
        return null;
    }
};
 
/**
 * The directory is resolved in `nuxt.config.ts`: from `import.meta.url` here it became
 * `.output/server/.report` once Nitro bundled this module, and prerender baked an empty dashboard.
 */
export const readArtifact = <T>(name: string): Promise<T | null> =>
    readJson<T>(resolve(useRuntimeConfig().snapshotDir, `${name}.json`));