-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathts-check.js
More file actions
executable file
·62 lines (52 loc) · 1.99 KB
/
Copy pathts-check.js
File metadata and controls
executable file
·62 lines (52 loc) · 1.99 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
#!/usr/bin/env node
import * as ts from "typescript";
// Cannot use __basepath because we have "type": "module" in package.json
const basepath = process.argv[1].slice(0, process.argv[1].lastIndexOf('/'));
const ignorePaths = [`${basepath}/node_modules`, `${basepath}/pkg`];
/**
* @param {string} filePath
* @returns {boolean}
*/
const shouldIgnore = (filePath) => {
for (const path of ignorePaths) {
if (filePath.startsWith(path))
return true;
}
return false;
}
/**
* @param {string[]} fileNames
* @param {ts.CompilerOptions} options
*/
function checkTypes(fileNames, options) {
const program = ts.createProgram(fileNames, options);
const emitResult = program.emit();
let success = true;
const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
if (shouldIgnore(diagnostic.file.fileName)) {
return;
}
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
const fileName = diagnostic.file.fileName.replace(basepath + '/', '');
console.log(`${fileName}:${line + 1}:${character + 1}:\n${message}\n`);
success = false;
} else {
console.log("Unknown diagnostic error:");
console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
success = false;
}
});
const exitCode = success ? 0 : 1;
console.log(`Process exiting with code '${exitCode}'.`);
process.exit(exitCode);
}
const { config } = ts.readConfigFile("tsconfig.json", ts.sys.readFile);
const tsConfig = ts.parseJsonConfigFileContent(config, ts.sys, basepath);
// Make sure this script never emits anything
tsConfig.options.noEmit = true
checkTypes(tsConfig.fileNames, tsConfig.options);