-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTypescriptCompiler.ts
More file actions
421 lines (359 loc) · 13.8 KB
/
Copy pathTypescriptCompiler.ts
File metadata and controls
421 lines (359 loc) · 13.8 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { EventEmitter } from "events";
import fs from "fs";
import path from "path";
import type {
CompilerOptions,
EmitOutput,
LanguageService,
LanguageServiceHost,
ModuleResolutionHost,
ResolvedModule,
} from "typescript";
import type { TypeScript } from "../AppsCompiler";
import type {
IAppSource,
ICompilerDiagnostic,
ICompilerFile,
ICompilerResult,
IMapCompilerFile,
} from "../definition";
import { normalizeDiagnostics } from "../misc/normalizeDiagnostics";
import { Utilities } from "../misc/Utilities";
import type { AppsEngineValidator } from "./AppsEngineValidator";
import logger from "../misc/logger";
export class TypescriptCompiler {
private readonly compilerOptions: CompilerOptions;
private libraryFiles: IMapCompilerFile;
constructor(
private readonly sourcePath: string,
private readonly ts: TypeScript,
private readonly appValidator: AppsEngineValidator,
) {
this.compilerOptions = {
target: this.ts.ScriptTarget.ES2017,
module: this.ts.ModuleKind.CommonJS,
moduleResolution: this.ts.ModuleResolutionKind.NodeJs,
declaration: false,
noImplicitAny: false,
removeComments: true,
strictNullChecks: true,
noImplicitReturns: true,
emitDecoratorMetadata: true,
experimentalDecorators: true,
types: ["node"],
skipLibCheck: true,
// Set this to true if you would like to see the module resolution process
traceResolution: false,
};
this.libraryFiles = {};
}
public transpileSource({
appInfo,
sourceFiles: files,
}: IAppSource): ICompilerResult {
// Normalize all file keys, file names, and classFile to POSIX separators
// once up front so getScriptFileNames(), getScriptVersion(), getScriptSnapshot(),
// and all subsequent lookups into result.files operate on consistent keys.
const posixClassFile = appInfo.classFile.replace(/\\/g, "/");
const normalizedFiles: IMapCompilerFile = {};
for (const [key, file] of Object.entries(files)) {
const posixKey = key.replace(/\\/g, "/");
normalizedFiles[posixKey] = {
...file,
name: file.name.replace(/\\/g, "/"),
};
}
files = normalizedFiles;
if (
!posixClassFile ||
!files[posixClassFile] ||
!this.isValidFile(files[posixClassFile])
) {
throw new Error(
`Invalid App package. Could not find the classFile (${appInfo.classFile}) file.`,
);
}
const startTime = Date.now();
this.appValidator.validateAppPermissionsSchema(appInfo.permissions);
const result: ICompilerResult = {
files,
implemented: [],
diagnostics: [],
duration: NaN,
name: appInfo.name,
version: appInfo.version,
typeScriptVersion: this.ts.version,
permissions: appInfo.permissions,
};
// Verify all file names are normalized
// and that the files are valid
Object.keys(result.files).forEach((key) => {
if (!this.isValidFile(result.files[key])) {
throw new Error(`Invalid TypeScript file: "${key}".`);
}
result.files[key].name = path.posix.normalize(
result.files[key].name,
);
});
let hasExternalDependencies = false;
let hasNativeDependencies = false;
const dependencyCheck = new EventEmitter();
dependencyCheck.on("dependencyCheck", (dependencyType) => {
switch (dependencyType) {
case "external":
if (!hasExternalDependencies) {
hasExternalDependencies = true;
logger.warn("App has external module(s) as dependency");
}
break;
case "native":
if (!hasNativeDependencies) {
hasNativeDependencies = true;
logger.warn("App has native module(s) as dependency");
}
break;
default:
break;
}
});
const modulesNotFound: ICompilerDiagnostic[] = [];
const host = {
getScriptFileNames: () => Object.keys(result.files),
getScriptVersion: (fileName) => {
fileName = path.posix.normalize(fileName.replace(/\\/g, "/"));
const file =
result.files[fileName] || this.getLibraryFile(fileName);
return file?.version?.toString();
},
getScriptSnapshot: (fileName) => {
fileName = path.posix.normalize(fileName.replace(/\\/g, "/"));
const file =
result.files[fileName] || this.getLibraryFile(fileName);
if (!file?.content) {
return;
}
return this.ts.ScriptSnapshot.fromString(file.content);
},
getCompilationSettings: () => this.compilerOptions,
getCurrentDirectory: () => this.sourcePath,
getDefaultLibFileName: () =>
this.ts.getDefaultLibFilePath(this.compilerOptions),
fileExists: (fileName: string): boolean =>
this.ts.sys.fileExists(fileName),
readFile: (fileName: string): string | undefined =>
this.ts.sys.readFile(fileName),
resolveModuleNames: (
moduleNames: Array<string>,
containingFile: string,
): Array<ResolvedModule> => {
const resolvedModules: ResolvedModule[] = [];
const moduleResHost: ModuleResolutionHost = {
fileExists: host.fileExists,
readFile: host.readFile,
trace: (traceDetail) => console.log(traceDetail),
};
for (const moduleName of moduleNames) {
const index = this.resolver(
moduleName,
resolvedModules,
containingFile,
result,
moduleResHost,
dependencyCheck,
);
if (index === -1) {
modulesNotFound.push({
filename: containingFile,
line: 0,
character: 0,
lineText: "",
message: `Failed to resolve module: ${moduleName}`,
originalMessage: `Module not found: ${moduleName}`,
originalDiagnostic: undefined,
});
}
}
return resolvedModules;
},
} as LanguageServiceHost;
const languageService = this.ts.createLanguageService(
host,
this.ts.createDocumentRegistry(),
);
try {
const coDiag = languageService.getCompilerOptionsDiagnostics();
if (coDiag.length !== 0) {
console.log(coDiag);
console.error(
"A VERY UNEXPECTED ERROR HAPPENED THAT SHOULD NOT!",
);
// console.error('Please report this error with a screenshot of the logs. ' +
// `Also, please email a copy of the App being installed/updated: ${ info.name } v${ info.version } (${ info.id })`);
throw new Error(
`Language Service's Compiler Options Diagnostics contains ${coDiag.length} diagnostics.`,
);
}
} catch (e) {
if (modulesNotFound.length !== 0) {
result.diagnostics = modulesNotFound;
result.duration = Date.now() - startTime;
return result;
}
throw e;
}
result.implemented = this.getImplementedInterfaces(
languageService,
posixClassFile,
);
Object.defineProperty(result, "diagnostics", {
value: normalizeDiagnostics(
this.ts.getPreEmitDiagnostics(languageService.getProgram()),
),
configurable: false,
writable: false,
});
Object.keys(result.files).forEach((key) => {
const file: ICompilerFile = result.files[key];
const output: EmitOutput = languageService.getEmitOutput(file.name);
file.name = key.replace(/\.ts$/g, ".js");
delete result.files[key];
result.files[file.name] = file;
file.compiled = output.outputFiles[0].text;
});
result.mainFile = result.files[posixClassFile.replace(/\.ts$/, ".js")];
this.appValidator.checkInheritance(
posixClassFile.replace(/\.ts$/, ""),
result,
);
result.duration = Date.now() - startTime;
return result;
}
private resolver(
moduleName: string,
resolvedModules: Array<ResolvedModule>,
containingFile: string,
result: ICompilerResult,
moduleResHost: ModuleResolutionHost,
dependencyCheck: EventEmitter,
): number {
// Keep compatibility with apps importing apps-ts-definition
moduleName = moduleName.replace(
/@rocket.chat\/apps-ts-definition\//,
"@rocket.chat/apps-engine/definition/",
);
// ignore @types/node/*.d.ts
if (/node_modules\/@types\/node\/\S+\.d\.ts$/.test(containingFile)) {
return resolvedModules.push(undefined);
}
if (Utilities.allowedInternalModuleRequire(moduleName)) {
dependencyCheck.emit("dependencyCheck", "native");
return resolvedModules.push({
resolvedFileName: `${moduleName}.js`,
});
}
const resolvedWithIndex = this.resolvePath(
containingFile,
`${moduleName}/index`,
);
if (result.files[resolvedWithIndex]) {
return resolvedModules.push({
resolvedFileName: resolvedWithIndex,
});
}
const resolvedPath = this.resolvePath(containingFile, moduleName);
if (result.files[resolvedPath]) {
return resolvedModules.push({ resolvedFileName: resolvedPath });
}
// Now, let's try the "standard" resolution but with our little twist on it
const rs = this.ts.resolveModuleName(
moduleName,
containingFile,
this.compilerOptions,
moduleResHost,
);
if (rs.resolvedModule) {
if (
rs.resolvedModule.isExternalLibraryImport &&
rs.resolvedModule.packageId &&
rs.resolvedModule.packageId.name !== "@rocket.chat/apps-engine"
) {
dependencyCheck.emit("dependencyCheck", "external");
}
return resolvedModules.push(rs.resolvedModule);
}
return -1;
}
private resolvePath(containingFile: string, moduleName: string): string {
const posixSourcePath = this.sourcePath
.replace(/\\/g, "/")
.replace(/\/$/, "");
const posixContainingDir = path.posix.dirname(
containingFile.replace(/\\/g, "/"),
);
const currentFolderPath = posixContainingDir.replace(
posixSourcePath,
"",
);
const modulePath = path.posix.join(
currentFolderPath || ".",
moduleName,
);
// Let's ensure we search for the App's modules first
const transformedModule =
Utilities.transformModuleForCustomRequire(modulePath);
if (transformedModule) {
return transformedModule;
}
}
private getImplementedInterfaces(
languageService: LanguageService,
classFile: string,
): ICompilerResult["implemented"] {
const result: ICompilerResult["implemented"] = [];
const src = languageService.getProgram().getSourceFile(classFile);
this.ts.forEachChild(src, (n) => {
if (!this.ts.isClassDeclaration(n)) {
return;
}
this.ts.forEachChild(n, (node) => {
if (!this.ts.isHeritageClause(node)) {
return;
}
this.ts.forEachChild(node, (nn) => {
const interfaceName = nn.getText();
if (
node.token === this.ts.SyntaxKind.ImplementsKeyword &&
this.appValidator.isValidAppInterface(interfaceName)
) {
result.push(interfaceName);
}
});
});
});
return result;
}
private getLibraryFile(fileName: string): ICompilerFile {
if (!fileName.endsWith(".d.ts")) {
return undefined;
}
const norm = path.posix.normalize(fileName.replace(/\\/g, "/"));
if (this.libraryFiles[norm]) {
return this.libraryFiles[norm];
}
if (!fs.existsSync(fileName)) {
return undefined;
}
this.libraryFiles[norm] = {
name: norm,
content: fs.readFileSync(fileName).toString(),
version: 0,
};
return this.libraryFiles[norm];
}
private isValidFile(file: ICompilerFile): boolean {
if (!file?.name || !file?.content) {
return false;
}
return file.name.trim() !== "" && file.content.trim() !== "";
}
}