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 | 1x 1x 23x 7x 10x 1x 7x 7x 7x 1x 7x 7x 6x 6x 6x 6x 6x 6x 1x 2x 2x 1x | import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname } from "node:path";
import { flag } from "../cli.ts";
const CACHE_DIR = "node_modules/.cache/@joinery/scripts";
const TTL_MS = 24 * 60 * 60 * 1000;
const pathFor = (key: string): string => `${CACHE_DIR}/${key}.json`;
// Read per call so importing a module cannot freeze the flag's value.
const refreshRequested = (): boolean => flag("refresh");
const isEmptyList = (data: unknown): boolean => Array.isArray(data) && data.length === 0;
/** Issue lists refresh the cache on every run instead of using `cached`'s TTL gate. */
export const writeCache = <T>(key: string, data: T): void => {
try {
mkdirSync(dirname(pathFor(key)), { recursive: true });
writeFileSync(pathFor(key), JSON.stringify({ fetchedAt: Date.now(), data }));
} catch {
// Cache writes fail open so a successful command is not lost to storage errors.
}
};
/** Cache stable project and label lists for 24h; issue lists stay live because triage changes them. */
export const cached = <T>(key: string, fetch: () => T): T => {
const file = pathFor(key);
if (!refreshRequested()) {
try {
const { fetchedAt, data } = JSON.parse(readFileSync(file, "utf8")) as {
fetchedAt: number;
data: T;
};
if (!isEmptyList(data) && Date.now() - fetchedAt < TTL_MS) return data;
} catch {
// Cache misses and corrupt data fall through to a live fetch.
}
}
const data = fetch();
// An empty list here is usually a swallowed failure, and writing it would erase the offline fallback.
if (!isEmptyList(data)) writeCache(key, data);
return data;
};
/** Offline mode reads stale cache without the TTL gate because no fetch can replace it. */
export const readCache = <T>(key: string): T | undefined => {
try {
return (JSON.parse(readFileSync(pathFor(key), "utf8")) as { data: T }).data;
} catch {
return undefined;
}
};
|