/
analyzer.ts
174 lines (163 loc) · 6.35 KB
/
analyzer.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import ts from 'typescript';
import path from 'path';
import { ScriptSourceHelper } from '../ts-ast-util/types';
import { Extractor } from './extractor';
import {
createScriptSourceHelper,
getTemplateNodeUnder,
findAllNodes,
registerDocumentChangeEvent,
getSanitizedTemplateText,
parseTagConfig,
} from '../ts-ast-util';
import { FragmentRegistry } from '../gql-ast-util';
import { SchemaManager, SchemaBuildErrorInfo } from '../schema-manager/schema-manager';
import { TsGqlError, ErrorWithLocation, ErrorWithoutLocation } from '../errors';
import { location2pos } from '../string-util';
import { validate } from './validator';
import { ManifestOutput, TsGraphQLPluginConfig } from './types';
import { MarkdownReporter } from './markdown-reporter';
import { TypeGenerator } from './type-generator';
import { createFileNameFilter } from '../ts-ast-util/file-name-filter';
class TsGqlConfigError extends ErrorWithoutLocation {
constructor() {
const message =
'No GraphQL schema. Confirm your ts-graphql-plugin\'s "schema" configuration at tsconfig.json\'s compilerOptions.plugins section.';
super(message);
}
}
export function convertSchemaBuildErrorsToErrorWithLocation(errorInfo: SchemaBuildErrorInfo) {
const fileName = errorInfo.fileName;
const content = errorInfo.fileContent;
if (errorInfo.locations && errorInfo.locations[0]) {
const start = location2pos(errorInfo.fileContent, errorInfo.locations[0]);
const end = start + 1;
const errorContent = { fileName, content, start, end };
return new ErrorWithLocation(errorInfo.message, errorContent);
} else {
const start = 0;
const end = content.length - 1;
const errorContent = { fileName, content, start, end };
return new ErrorWithLocation(errorInfo.message, errorContent);
}
}
export class Analyzer {
private _extractor: Extractor;
private _scriptSourceHelper: ScriptSourceHelper;
private _typeGenerator: TypeGenerator;
constructor(
private readonly _pluginConfig: TsGraphQLPluginConfig,
private readonly _prjRootPath: string,
private readonly _languageServiceHost: ts.LanguageServiceHost,
private readonly _schemaManager: SchemaManager,
private readonly _debug: (msg: string) => void,
) {
const documentRegistry = ts.createDocumentRegistry();
const langService = ts.createLanguageService(this._languageServiceHost, documentRegistry);
const fragmentRegistry = new FragmentRegistry();
const projectName = path.join(this._prjRootPath, 'tsconfig.json');
const isExcluded = createFileNameFilter({ specs: this._pluginConfig.exclude, projectName });
this._scriptSourceHelper = createScriptSourceHelper(
{
languageService: langService,
languageServiceHost: this._languageServiceHost,
project: {
getProjectName: () => projectName,
},
},
{ exclude: this._pluginConfig.exclude },
);
this._extractor = new Extractor({
removeDuplicatedFragments: this._pluginConfig.removeDuplicatedFragments === false ? false : true,
scriptSourceHelper: this._scriptSourceHelper,
fragmentRegistry,
debug: this._debug,
});
this._typeGenerator = new TypeGenerator({
prjRootPath: this._prjRootPath,
extractor: this._extractor,
tag: parseTagConfig(this._pluginConfig.tag),
addonFactories: this._pluginConfig.typegen.addonFactories,
debug: this._debug,
});
if (this._pluginConfig.enabledGlobalFragments === true) {
const tag = parseTagConfig(this._pluginConfig.tag);
registerDocumentChangeEvent(documentRegistry, {
onAcquire: (fileName, sourceFile, version) => {
if (!isExcluded(fileName) && this._languageServiceHost.getScriptFileNames().includes(fileName)) {
fragmentRegistry.registerDocuments(
fileName,
version,
findAllNodes(sourceFile, node => getTemplateNodeUnder(node, tag)).map(node =>
getSanitizedTemplateText(node, sourceFile),
),
);
}
},
});
}
}
getPluginConfig() {
return this._pluginConfig;
}
extract(fileNameList?: string[]) {
const results = this._extractor.extract(
fileNameList || this._languageServiceHost.getScriptFileNames(),
parseTagConfig(this._pluginConfig.tag),
);
const errors = this._extractor.pickupErrors(results);
return [errors, results] as const;
}
extractToManifest() {
const [errors, results] = this.extract();
const manifest = this._extractor.toManifest(results, parseTagConfig(this._pluginConfig.tag));
return [errors, manifest] as const;
}
async validate() {
const [schemaErrors, schema] = await this._getSchema();
if (!schema) return { errors: schemaErrors };
const [extractedErrors, result] = this.extract();
if (extractedErrors.length) {
this._debug(`Found ${extractedErrors.length} extraction errors.`);
}
return {
errors: [...schemaErrors, ...extractedErrors, ...validate(result, schema)],
extractedResults: result,
schema,
};
}
report(outputFileName: string, manifest?: ManifestOutput, ignoreFragments = true) {
const reporter = new MarkdownReporter();
const reportOptions = {
baseDir: this._prjRootPath,
ignoreFragments,
outputDir: path.dirname(outputFileName),
};
if (manifest) {
return [[] as TsGqlError[], reporter.toMarkdownConntent(manifest, reportOptions)] as const;
} else {
const [errors, extractedManifest] = this.extractToManifest();
return [errors, reporter.toMarkdownConntent(extractedManifest, reportOptions)] as const;
}
}
async typegen() {
const [schemaErrors, schema] = await this._getSchema();
if (!schema) return { errors: schemaErrors };
const { errors, outputSourceFiles } = this._typeGenerator.generateTypes({
schema,
files: this._languageServiceHost.getScriptFileNames(),
});
return { errors: [...schemaErrors, ...errors], outputSourceFiles };
}
private async _getSchema() {
const errors: TsGqlError[] = [];
const { schema, errors: schemaBuildErrors } = await this._schemaManager.waitSchema();
if (schemaBuildErrors) {
schemaBuildErrors.forEach(info => errors.push(convertSchemaBuildErrorsToErrorWithLocation(info)));
}
if (!schema && !errors.length) {
errors.push(new TsGqlConfigError());
}
return [errors, schema] as const;
}
}