30 lines
1008 B
JavaScript
Raw Normal View History

2026-05-31 13:21:13 +02:00
import { createHash } from 'node:crypto';
import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
import { resolve, join } from 'node:path';
import process from 'node:process';
function createFileSystemTypesCache(options = {}) {
const dir = resolve(process.cwd(), options.dir ?? ".vitepress/cache/twoslash");
return {
init() {
mkdirSync(dir, { recursive: true });
},
read(code) {
const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12);
const filePath = join(dir, `${hash}.json`);
if (!existsSync(filePath)) {
return null;
}
return JSON.parse(readFileSync(filePath, { encoding: "utf-8" }));
},
write(code, data) {
const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12);
const filePath = join(dir, `${hash}.json`);
const json = JSON.stringify(data);
writeFileSync(filePath, json, { encoding: "utf-8" });
}
};
}
export { createFileSystemTypesCache };