import { useCallback, useEffect, useState } from 'react'; import * as fs from 'fs'; export function usePromise(f: () => Promise): T | null { const [value, setValue] = useState(null); useEffect(() => { setValue(null); let cancelled = false; (async (): Promise => { const v = await f(); if (!cancelled) { setValue(v); } })(); return (): void => { cancelled = true; }; }, [f]); return value; } export function useReadFile(path: string): Buffer | null { const readFile = useCallback(() => fs.promises.readFile(path), [path]); return usePromise(readFile); } export async function readJSON(path: string): Promise { const content = await fs.promises.readFile(path, 'utf8'); return JSON.parse(content); }