This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Cache.ts
71 lines (62 loc) · 2.08 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
import Config from "@truffle/config";
import path from "path";
import fs from "fs";
// @ts-ignore
import * as fsPromises from "fs/promises";
const fileNotFoundMessage = "no such file or directory";
export class Cache {
private compilerCachePath: string;
public memoizedCompilers: Map<string, string>;
constructor() {
const compilersDir = path.resolve(
Config.getTruffleDataDirectory(),
"compilers"
);
const compilerCachePath = path.resolve(compilersDir, "node_modules"); // because babel binds to require & does weird things
if (!fs.existsSync(compilersDir)) fs.mkdirSync(compilersDir);
if (!fs.existsSync(compilerCachePath)) fs.mkdirSync(compilerCachePath); // for 5.0.8 users
this.compilerCachePath = compilerCachePath;
this.memoizedCompilers = new Map();
}
async list() {
return await fsPromises.readdir(this.compilerCachePath);
}
async add(code: string, fileName: string) {
const filePath = this.resolve(fileName);
await fsPromises.writeFile(filePath, code);
this.memoizedCompilers.set(filePath, code);
}
async has(fileName: string): Promise<boolean> {
const filePath = this.resolve(fileName);
try {
await fsPromises.stat(filePath);
return true;
} catch (error) {
// only throw if the error is due to something other than it not existing
if (!error.message.includes(fileNotFoundMessage)) {
throw error;
}
return false;
}
}
async loadFile(fileName: string): Promise<string> {
const filePath = this.resolve(fileName);
if (this.memoizedCompilers.has(filePath)) {
return this.memoizedCompilers.get(filePath)!;
}
try {
const compiler = (await fsPromises.readFile(filePath)).toString();
this.memoizedCompilers.set(filePath, compiler);
return compiler;
} catch (error) {
if (!error.message.includes(fileNotFoundMessage)) {
throw error;
} else {
throw new Error("The specified file could not be found.");
}
}
}
resolve(fileName: string) {
return path.resolve(this.compilerCachePath, fileName);
}
}