-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cache.ts
131 lines (104 loc) · 3.17 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { LoDashStatic } from "lodash";
import type { ProjectPathsConfig } from "hardhat/types/config";
import type { VyperConfig } from "./types";
import path from "path";
import fsExtra from "fs-extra";
import * as t from "io-ts";
import { CACHE_FORMAT_VERSION, VYPER_FILES_CACHE_FILENAME } from "./constants";
import { getLogger } from "./util";
const log = getLogger("cache");
const CacheEntryCodec = t.type({
lastModificationDate: t.number,
contentHash: t.string,
sourceName: t.string,
vyperConfig: t.any,
versionPragma: t.string,
artifacts: t.array(t.string),
});
const CacheCodec = t.type({
_format: t.string,
files: t.record(t.string, CacheEntryCodec),
});
export interface CacheEntry {
lastModificationDate: number;
contentHash: string;
sourceName: string;
vyperConfig: VyperConfig;
versionPragma: string;
artifacts: string[];
}
export interface Cache {
_format: string;
files: Record<string, CacheEntry>;
}
export class VyperFilesCache {
constructor(private _cache: Cache) {}
public static createEmpty(): VyperFilesCache {
return new VyperFilesCache({ _format: CACHE_FORMAT_VERSION, files: {} });
}
public static async readFromFile(
vyperFilesCachePath: string
): Promise<VyperFilesCache> {
const cacheRaw: Cache = fsExtra.existsSync(vyperFilesCachePath)
? fsExtra.readJSONSync(vyperFilesCachePath)
: {
_format: CACHE_FORMAT_VERSION,
files: {},
};
const result = CacheCodec.decode(cacheRaw);
if (result.isRight()) {
const vyperFilesCache = new VyperFilesCache(result.value);
await vyperFilesCache.removeNonExistingFiles();
return vyperFilesCache;
}
log("There was a problem reading the cache");
return VyperFilesCache.createEmpty();
}
public async removeNonExistingFiles() {
for (const absolutePath of Object.keys(this._cache.files)) {
if (!fsExtra.existsSync(absolutePath)) {
this.removeEntry(absolutePath);
}
}
}
public async writeToFile(vyperFilesCachePath: string) {
await fsExtra.outputJson(vyperFilesCachePath, this._cache, { spaces: 2 });
}
public addFile(absolutePath: string, entry: CacheEntry) {
this._cache.files[absolutePath] = entry;
}
public getEntries(): CacheEntry[] {
return Object.values(this._cache.files);
}
public getEntry(file: string): CacheEntry | undefined {
return this._cache.files[file];
}
public removeEntry(file: string) {
delete this._cache.files[file];
}
public hasFileChanged(
absolutePath: string,
contentHash: string,
vyperConfig?: VyperConfig
): boolean {
const { isEqual }: LoDashStatic = require("lodash");
const cacheEntry = this.getEntry(absolutePath);
if (cacheEntry === undefined) {
// new file or no cache available, assume it's new
return true;
}
if (cacheEntry.contentHash !== contentHash) {
return true;
}
if (
vyperConfig !== undefined &&
!isEqual(vyperConfig, cacheEntry.vyperConfig)
) {
return true;
}
return false;
}
}
export function getVyperFilesCachePath(paths: ProjectPathsConfig): string {
return path.join(paths.cache, VYPER_FILES_CACHE_FILENAME);
}