/
schema-manager-host.ts
62 lines (58 loc) · 1.87 KB
/
schema-manager-host.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
import path from 'node:path';
import ts from 'typescript';
import type { SchemaManagerHost, SchemaConfig } from './types';
import type { TsGraphQLPluginConfigOptions } from '../types';
class SystemSchemaManagerHost implements SchemaManagerHost {
constructor(
private readonly _pluginConfig: TsGraphQLPluginConfigOptions,
private readonly _prjRootPath: string,
private readonly _debug: (msg: string) => void,
) {}
log(msg: string): void {
return this._debug(msg);
}
watchFile(path: string, cb: (fileName: string) => void, interval: number): { close(): void } {
return ts.sys.watchFile!(path, cb, interval);
}
readFile(path: string, encoding?: string | undefined): string | undefined {
return ts.sys.readFile(path, encoding);
}
fileExists(path: string): boolean {
return ts.sys.fileExists(path);
}
getConfig(): SchemaConfig {
return this._pluginConfig;
}
getProjectRootPath(): string {
return this._prjRootPath;
}
}
export function createSchemaManagerHostFromTSGqlPluginConfig(
pluginConfig: Omit<TsGraphQLPluginConfigOptions, 'typegen'>,
prjRootPath: string,
debug: (msg: string) => void = () => {},
) {
return new SystemSchemaManagerHost(pluginConfig, prjRootPath, debug);
}
export function createSchemaManagerHostFromLSPluginInfo(info: ts.server.PluginCreateInfo): SchemaManagerHost {
return {
getConfig() {
return info.config as SchemaConfig;
},
fileExists(path) {
return info.serverHost.fileExists(path);
},
readFile(path, encoding) {
return info.serverHost.readFile(path, encoding);
},
watchFile(path, cb, interval) {
return info.serverHost.watchFile(path, cb, interval);
},
getProjectRootPath() {
return path.dirname(info.project.getProjectName());
},
log(msg) {
info.project.projectService.logger.info(`[ts-graphql-plugin] ${msg}`);
},
};
}