1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import * as ts from 'typescript';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export interface VerifyTypingsResult {
|
18 | success: boolean;
|
19 | errorLog: string;
|
20 | }
|
21 |
|
22 | const compilerOptions = {
|
23 | target: ts.ScriptTarget.ES2015,
|
24 | module: ts.ModuleKind.ES2015,
|
25 | moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
26 | strict: true,
|
27 | noUnusedLocals: true,
|
28 | noUnusedParameters: true,
|
29 | declaration: true,
|
30 | lib: [
|
31 | 'lib.dom.d.ts',
|
32 | 'lib.esnext.d.ts',
|
33 | ],
|
34 | };
|
35 |
|
36 | const diagnosticsHost = {
|
37 | getNewLine: () => '\n',
|
38 | getCurrentDirectory: () => process.cwd(),
|
39 | getCanonicalFileName: (fileName: string) => fileName,
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function verifyTypings(filePaths: string[]): VerifyTypingsResult {
|
50 | const program = ts.createProgram(filePaths, compilerOptions);
|
51 | const emitResult = program.emit();
|
52 | const diagnostics =
|
53 | ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
54 | if (diagnostics.length > 0) {
|
55 | return {
|
56 | success: false,
|
57 | errorLog:
|
58 | ts.formatDiagnosticsWithColorAndContext(diagnostics, diagnosticsHost)
|
59 | };
|
60 | }
|
61 | return {success: true, errorLog: ''};
|
62 | }
|