30 lines
1008 B
JavaScript
30 lines
1008 B
JavaScript
|
|
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 };
|