-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
104 lines (78 loc) · 2.6 KB
/
index.js
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
#!/usr/bin/env node
const
ts = require('typescript'),
fs = require('fs'),
path = require('upath'),
process = require('process'),
arg = require('arg'),
log = require('./log');
log.info(`Using Typescript ${ts.version}`);
const
defaultTypescriptConfigFilename = 'tsconfig.json';
const
cwd = process.cwd(),
cliArgs = arg({'--tsconfig-filename': String, '--max-errors': Number}, {permissive: true}),
typescriptConfigFilename = cliArgs['--tsconfig-filename'] || defaultTypescriptConfigFilename,
pathToTypescriptConfig = path.join(cwd, typescriptConfigFilename),
typescriptConfigExists = fs.existsSync(pathToTypescriptConfig);
let
tsconfigFile;
if (!typescriptConfigExists) {
log.setFailed(`Config file ${typescriptConfigFilename} doesn't exists in ${cwd}`);
} else {
tsconfigFile = ts.readJsonConfigFile(pathToTypescriptConfig, (path) => fs.readFileSync(path, {encoding: 'utf-8'}));
}
const config = ts.parseJsonSourceFileConfigFileContent(
tsconfigFile,
{
useCaseSensitiveFileNames: true,
// Temporary fix of the undocumented breaking change in Typescript 4.4
// https://github.com/microsoft/TypeScript/issues/45990
readDirectory: (relativePath, ...restArgs) => {
const
absolutePath = path.resolve(relativePath);
return ts.sys.readDirectory(absolutePath, ...restArgs);
},
readFile: ts.sys.readFile,
fileExists: ts.sys.fileExists
},
'.',
);
const formatConfig = {
getNewLine: () => ts.sys.newLine,
getCanonicalFileName: (fileName) => fileName,
getCurrentDirectory: ts.sys.getCurrentDirectory
}
log.info(`${typescriptConfigFilename} has been successfully parsed`);
const
typescriptProgram = ts.createProgram(config.fileNames, config.options);
log.info('Typescript program has been created');
const
result = ts.getPreEmitDiagnostics(typescriptProgram);
log.info('Diagnostic has been completed');
let
totalErrors = 0;
const
maxErrors = cliArgs['--max-errors'] || 0;
for (let i = 0; i < result.length; i++) {
const
d = result[i];
if (d.category === ts.DiagnosticCategory.Error) {
if (d.file != null && d.file.fileName.includes('node_modules')) {
continue;
}
log.error(ts.formatDiagnostic(d, formatConfig));
totalErrors++;
}
if (d.category === ts.DiagnosticCategory.Warning) {
log.warning(ts.formatDiagnostic(d, formatConfig))
}
if (d.category === ts.DiagnosticCategory.Suggestion) {
log.info(ts.formatDiagnostic(d, formatConfig));
}
}
if (totalErrors > maxErrors) {
log.setFailed(`Errors found: ${totalErrors}${maxErrors > 0 ? `. Errors allowed: ${maxErrors}` : ''}`);
} else if (maxErrors > 0) {
log.warning(`Errors found: ${totalErrors}. Errors allowed: ${maxErrors}`);
}