-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
74 lines (58 loc) · 1.72 KB
/
cache.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pathFs from "path";
import fse from "fs-extra";
import { Options, Stat } from "./index.js";
import uniqBy from "lodash/uniqBy.js";
import { sortStats } from "./analyze.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const VERSION: string = fse.readJSONSync(
pathFs.resolve(__dirname, "../package.json")
).version;
let cachedFileCache: FileCache | undefined;
interface FileCache {
data: Stat[];
path: string;
version: string;
}
export async function readData(options: Options): Promise<Stat[]> {
const outputPath = getOutputPath(options);
if (!(await fse.pathExists(outputPath))) return [];
const fileCache = await read(options);
if (!fileCache) return [];
return fileCache.data;
}
export async function writeData(data: Stat[], options: Options): Promise<void> {
const outputPath = getOutputPath(options);
const allData = uniqBy(
(cachedFileCache?.data || []).concat(data).sort(sortStats),
(v) => v.date
);
await fse.outputJSON(outputPath, create(options.path, allData), {
spaces: 2,
});
}
async function read(options: Options): Promise<FileCache | null> {
const outputPath = getOutputPath(options);
const result: FileCache | null = await fse.readJSON(outputPath, {
throws: false,
});
if (
!result ||
result.version !== VERSION ||
result.path !== pathFs.resolve(options.path)
)
return null;
cachedFileCache = result;
return result;
}
function create(path: string, data: Stat[] = []): FileCache {
return {
data,
path: pathFs.resolve(path),
version: VERSION,
};
}
function getOutputPath(options: Options): string {
return pathFs.resolve(options.output, "data.json");
}