forked from adamlaska/ts-graphql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-host.ts
More file actions
54 lines (43 loc) · 1.5 KB
/
script-host.ts
File metadata and controls
54 lines (43 loc) · 1.5 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
import ts from 'typescript';
export class ScriptHost implements ts.LanguageServiceHost {
private readonly _fileMap = new Map<string, string | undefined>();
private readonly _fileVersionMap = new Map<string, number>();
constructor(private readonly _currentDirectory: string, private readonly _compilerOptions: ts.CompilerOptions) {}
readFile(fileName: string) {
const hit = this._fileMap.get(fileName);
if (hit != null) return hit;
return this.loadFromFileSystem(fileName);
}
loadFromFileSystem(fileName: string) {
const content = ts.sys.readFile(fileName, 'uts8');
this._updateFile(fileName, content);
return content;
}
getCurrentDirectory() {
return this._currentDirectory;
}
getScriptSnapshot(fileName: string) {
const file = this._fileMap.get(fileName);
if (file == null) return;
return ts.ScriptSnapshot.fromString(file);
}
getScriptVersion(fileName: string) {
const version = this._fileVersionMap.get(fileName);
if (!version) return '0';
return version + '';
}
getScriptFileNames() {
return [...this._fileMap.keys()];
}
getCompilationSettings() {
return this._compilerOptions;
}
getDefaultLibFileName(opt: ts.CompilerOptions) {
return ts.getDefaultLibFileName(opt);
}
protected _updateFile(fileName: string, content: string | undefined) {
this._fileMap.set(fileName, content);
const currentVersion = this._fileVersionMap.get(fileName) || 0;
this._fileVersionMap.set(fileName, currentVersion + 1);
}
}