import { parse } from "https://deno.land/std@0.195.0/yaml/mod.ts"; import { join, fromFileUrl } from "https://deno.land/std@0.193.0/path/mod.ts"; export const PAGES_PATH = join(fromFileUrl(import.meta.url), "../pages.yaml"); export interface PageDescription { name: string; description: string; } async function readPagesDescription() { const pagesText = await Deno.readTextFile(PAGES_PATH); const pages = parse(pagesText) as PageDescription[]; return pages; } function watchFile( path: string, callback: () => void | Promise, ) { const watcherRef = Deno.watchFs(path); (async () => { for await (const event of watcherRef) { if (event.kind == "modify") { await callback(); } } })(); const closeHandler = () => { watcherRef.close(); }; Deno.addSignalListener("SIGINT", closeHandler); return ()=>{ Deno.removeSignalListener("SIGINT", closeHandler); closeHandler(); } } let pages_meta: PageDescription[] = []; let mtime = 0; let lastest_disposer = () => {}; export async function get_pages_meta(): Promise<[PageDescription[],number]>{ if (pages_meta.length == 0) { pages_meta = await readPagesDescription(); mtime = Date.now(); lastest_disposer(); lastest_disposer = watchFile(PAGES_PATH, async () => { pages_meta = await readPagesDescription(); mtime = Date.now(); }); } return [pages_meta, mtime]; }