import fs, { ensureDir } from 'fs-extra'; import { FsCache } from '../types'; import { dirname, resolve } from 'path'; import { writeFileIfDifferent } from './fs'; /** * A basic file system cache stored at `/.wxt/cache/`. Just caches a string in a * file for the given key. * * @param srcDir Absolute path to source directory. See `InternalConfig.srcDir` */ export function createFsCache(wxtDir: string): FsCache { const getPath = (key: string) => resolve(wxtDir, 'cache', encodeURIComponent(key)); return { async set(key: string, value: string): Promise { const path = getPath(key); await ensureDir(dirname(path)); await writeFileIfDifferent(path, value); }, async get(key: string): Promise { const path = getPath(key); try { return await fs.readFile(path, 'utf-8'); } catch { return undefined; } }, }; }