UNPKG

mdxlayer

Version:

Transform your MDX content into typed, JSON-powered data with flexible schema validation.

41 lines (38 loc) 992 B
import fs from 'node:fs'; import path from 'node:path'; import { cliOutDir } from '../utils/args.js'; import { hash } from '../utils/hash.js'; import { transformFile } from '../utils/transform.js'; const get = () => { const cachePath = path.resolve( process.cwd(), `${cliOutDir}/cache`, `data.json` ); if (!fs.existsSync(cachePath)) return {}; return JSON.parse(fs.readFileSync(cachePath, "utf8")); }; const set = (item) => transformFile({ doc: { ...get(), ...item }, filename: "data.json", subpath: "cache" }); const mtime = (key) => { const { mtimeMs } = fs.statSync(key); const isChanged = get()[key] !== mtimeMs; if (isChanged) cache.set({ [key]: mtimeMs }); return isChanged; }; const changed = (content, key) => { const sig = hash.number(content); const isChanged = hash.number(content) !== cache.get()[key]; if (isChanged) cache.set({ [key]: sig }); return isChanged; }; const cache = { changed, get, mtime, set }; export { cache };