-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompileCache.ts
122 lines (94 loc) · 3.23 KB
/
compileCache.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
import { compile, recursiveReadDirectoryWithContents } from "./compileClient";
import { v4 as uuidv4 } from 'uuid';
import * as path from "path";
import * as fs from "fs";
import { Compiler } from "./compiler";
export type ProjectInformation = {
luaPrimaryFile: string,
path: string,
id: string,
}
export type ExtendedProjectInformation = ProjectInformation & {
// a record of files to their contents
files: Record<string, string>,
}
export type CompileCacheMetadata = {
/**
* A listing of projects.
*/
projects: ProjectInformation[];
}
export class CompileCache {
metadata: CompileCacheMetadata;
compiler: Compiler = new Compiler();
constructor(protected readonly cacheLocation: string) {
if (!fs.existsSync(this.cacheLocation)) {
fs.mkdirSync(this.cacheLocation);
}
if (!fs.statSync(this.cacheLocation).isDirectory()) {
throw new Error("Attempted to construct a CompileCache with a path that is not a directory. This is likely an issue with your config cache location set to that of a file.");
}
try {
this.metadata = JSON.parse(fs.readFileSync(path.join(this.cacheLocation, "meta.json"), "utf-8"));
} catch(err) {
this.metadata = {
projects: [],
}
this.storeMetadata();
}
}
protected storeMetadata() {
fs.writeFileSync(path.join(this.cacheLocation, "meta.json"), JSON.stringify(this.metadata));
}
has(project: string): boolean {
const projectPath = path.join(require.resolve(project), "..");
return this.metadata.projects.find(project => project.path === projectPath) !== undefined;
}
getMeta(project: string): ProjectInformation | undefined {
const projectPath = path.join(require.resolve(project), "..");
return this.metadata.projects.find(project => project.path === projectPath);
}
getMetaSafe(project: string): ProjectInformation {
const result = this.getMeta(project);
if (result === undefined) {
throw new Error(`Expected project metadata ${project}, but couldn't find it.`);
}
return result;
}
get(project: string): ExtendedProjectInformation | undefined {
const projectMetadata = this.getMeta(project);
if (projectMetadata === undefined) {
return undefined;
}
const projectCacheFiles = recursiveReadDirectoryWithContents(path.join(this.cacheLocation, projectMetadata.id));
return {
...projectMetadata,
files: projectCacheFiles,
}
}
getSafe(project: string): ExtendedProjectInformation {
const result = this.get(project);
if (result === undefined) {
throw new Error(`Expected project ${project}, but couldn't find it.`);
}
return result;
}
compile(project: string): ExtendedProjectInformation {
const projectId = this.has(project) ? this.getMetaSafe(project).id : uuidv4();
const { luaPrimaryFile, files } = this.compiler.compile(project, path.join(this.cacheLocation, projectId))
if (!this.has(project)) {
this.metadata.projects.push({
path: path.join(require.resolve(project), ".."),
id: projectId,
luaPrimaryFile
});
}
this.storeMetadata();
return {
path: path.join(require.resolve(project), ".."),
id: projectId,
files,
luaPrimaryFile
};
}
}