-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
31 lines (31 loc) · 860 Bytes
/
cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import path from "path";
import fs from "fs/promises";
import { useProject } from "./project.js";
import { Logger } from "./logger.js";
import { lazy } from "./util/lazy.js";
export const useCache = lazy(async () => {
const project = useProject();
const cache = path.join(project.paths.out, "cache");
await fs.mkdir(cache, {
recursive: true,
});
async function write(key, data) {
const full = path.join(cache, key);
Logger.debug("Writing cache", full, data.length, "bytes");
await fs.writeFile(full, data);
}
async function read(key) {
const full = path.join(cache, key);
try {
const data = await fs.readFile(full);
return data.toString();
}
catch {
return null;
}
}
return {
write,
read,
};
});