forked from prettier/prettier-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
129 lines (115 loc) · 4.12 KB
/
Copy pathcache.ts
File metadata and controls
129 lines (115 loc) · 4.12 KB
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
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fastRelativePath, getCachePath, isArray, isBoolean, isObject, isString, isUndefined, sha1hex, sha1base64 } from "./utils.js";
import type Logger from "./logger.js";
import type { Options, PromiseMaybe } from "./types.js";
type Store = Partial<{
[version: string]: StoreVersion;
}>;
type StoreVersion = Partial<{
modified: number;
files: Partial<Record<string, StoreFile | false>>;
}>;
type StoreFile = [hash: string, formatted: boolean];
type FileData = {
content?: Buffer | string;
formatted?: boolean;
save: (formatted: boolean, fileContentExpected: string) => void;
};
//TODO: Maybe remember thrown errors also, if they are under a certain size
class Cache {
private version: string;
private rootPath: string;
private storePath: string;
private store: Store;
private logger: Logger;
private dirty: boolean;
constructor(version: string, rootPath: string, cacheRootPath: string | undefined, options: Options, logger: Logger) {
this.version = sha1hex(version);
this.logger = logger;
this.rootPath = rootPath;
const cachePath = cacheRootPath ? getCachePath(cacheRootPath) : path.join(os.tmpdir(), "prettier", ".prettier-caches");
this.storePath = options.cacheLocation || path.join(cachePath, `${sha1hex(rootPath)}.json`);
this.store = this.read();
this.dirty = false;
}
cleanup(store: Store): Store {
//TODO: Use a more sophisticated cleanup logic
for (const version in store) {
if (version === this.version) continue;
delete store[version];
this.dirty = true;
}
return store;
}
read(): Store {
try {
const store = JSON.parse(fs.readFileSync(this.storePath, "utf8"));
if (!isObject(store)) return {};
return this.cleanup(store);
} catch (error: unknown) {
this.logger.prefixed.debug(String(error));
return {};
}
}
write(): void {
if (!this.dirty) return;
try {
const store = JSON.stringify(this.store);
fs.mkdirSync(path.dirname(this.storePath), { recursive: true });
fs.writeFileSync(this.storePath, store);
} catch (error: unknown) {
this.logger.prefixed.debug(String(error));
}
}
get(filePath: string): FileData {
const fileRelativePath = fastRelativePath(this.rootPath, filePath);
const save = this.set.bind(this, filePath, fileRelativePath);
try {
const file = this.store[this.version]?.files?.[fileRelativePath];
if (!file || !isArray(file) || file.length !== 2) return { save };
const [hash, formatted] = file;
if (!isString(hash) || !isBoolean(formatted)) return { save };
const content = fs.readFileSync(filePath);
const fileHash = sha1base64(content);
if (hash !== fileHash) return { content, save };
return { formatted, content, save };
} catch (error: unknown) {
this.logger.prefixed.debug(String(error));
return { save };
}
}
set(filePath: string, fileRelativePath: string, fileFormatted: boolean, fileContentExpected: string): void {
try {
const version = (this.store[this.version] ||= {});
const files = (version.files ||= {});
//TODO: Skip the following hash if the expected content we got is the same one that we had
const hash = sha1base64(fileContentExpected);
version.modified = Date.now();
files[fileRelativePath] = [hash, fileFormatted];
this.dirty = true;
} catch (error: unknown) {
this.logger.prefixed.debug(String(error));
}
}
async has(filePath: string, isIgnored: () => PromiseMaybe<boolean>): Promise<boolean> {
const fileRelativePath = fastRelativePath(this.rootPath, filePath);
const file = this.store[this.version]?.files?.[fileRelativePath];
if (isUndefined(file)) {
const ignored = await isIgnored();
if (ignored) {
const version = (this.store[this.version] ||= {});
const files = (version.files ||= {});
files[fileRelativePath] = false;
this.dirty = true;
return false;
} else {
return true;
}
} else {
return !!file;
}
}
}
export default Cache;