44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
const ts = require('typescript');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
function report(d) {
|
|
const file = d.file ? path.relative(process.cwd(), d.file.fileName) : null;
|
|
const msg = ts.flattenDiagnosticMessageText(d.messageText, '\n');
|
|
const line = d.file && d.start !== undefined ? d.file.getLineAndCharacterOfPosition(d.start).line + 1 : null;
|
|
const col = d.file && d.start !== undefined ? d.file.getLineAndCharacterOfPosition(d.start).character + 1 : null;
|
|
console.log([file ? file : '', line ? ':' + line : '', col ? ':' + col : '', '-', msg].join(''));
|
|
}
|
|
|
|
async function run(tsconfigPath) {
|
|
const abs = path.resolve(tsconfigPath);
|
|
if (!fs.existsSync(abs)) {
|
|
console.error('tsconfig not found:', abs);
|
|
process.exit(2);
|
|
}
|
|
const configFile = ts.readConfigFile(abs, ts.sys.readFile);
|
|
if (configFile.error) {
|
|
report(configFile.error);
|
|
process.exit(1);
|
|
}
|
|
const parseConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(abs));
|
|
if (parseConfig.errors && parseConfig.errors.length) {
|
|
parseConfig.errors.forEach(report);
|
|
process.exit(1);
|
|
}
|
|
const program = ts.createProgram(parseConfig.fileNames, parseConfig.options);
|
|
const emitResult = program.emit();
|
|
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
if (allDiagnostics.length === 0) {
|
|
console.log('TSC: no errors');
|
|
process.exit(0);
|
|
}
|
|
allDiagnostics.forEach(report);
|
|
console.log('TSC: errors=', allDiagnostics.length);
|
|
process.exit(1);
|
|
}
|
|
|
|
const arg = process.argv[2] || 'packages/backend-api/tsconfig.json';
|
|
run(arg).catch(e => { console.error('runner failed', e); process.exit(3); });
|
|
|