Skip to content

Instantly share code, notes, and snippets.

@marvinroger
Created July 23, 2021 14:53
Show Gist options
  • Save marvinroger/32099aec9e222d25664f8ae87b664e97 to your computer and use it in GitHub Desktop.
Save marvinroger/32099aec9e222d25664f8ae87b664e97 to your computer and use it in GitHub Desktop.
medium/flow-to-ts/extract-ts-errors
/**
* Usage: tsc | node ./extract-ts-errors.js
*
* This script prints the list of encountered TS errors, ordered by ascending occurences.
* Also, it logs the output of `tsc` into `./tsc.log`, for easier searching.
*/
const readline = require('readline');
const fs = require('fs');
const path = require('path');
const Table = require('cli-table');
const chalk = require('chalk');
const OUTPUT_TXT_FILE = path.join(__dirname, 'tsc.log');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
const GENERIC_ERROR_REGEX = /^(.+)\(.+\): error TS([0-9]+): (.+)$/;
const errorsPerCode = new Map();
const errorsPerFile = new Map();
let errorsCount = 0;
const lines = [];
rl.on('line', line => {
lines.push(line);
const match = line.match(GENERIC_ERROR_REGEX);
if (!match) {
return;
}
const [, filePath, code, _message] = match;
const codeErrorCount = errorsPerCode.get(code) ?? 0;
const fileErrorCount = errorsPerFile.get(filePath) ?? 0;
errorsPerCode.set(code, codeErrorCount + 1);
errorsPerFile.set(filePath, fileErrorCount + 1);
errorsCount += 1;
});
rl.on('close', () => {
console.log(chalk.bold.italic('\n# Errors per code (desc by error count)\n'));
console.log(`Numbers of different TS errors: ${errorsPerCode.size}\n`);
const codeTable = new Table({
head: ['Code', '# of errors'],
colWidths: [10, 20],
});
[...errorsPerCode]
.sort((a, b) => b[1] - a[1])
.forEach(([code, count]) => {
codeTable.push([`TS${code}`, count]);
});
console.log(codeTable.toString());
console.log(chalk.bold.italic('\n# Errors per file (desc by error count)\n'));
console.log(`Numbers of files: ${errorsPerFile.size}\n`);
const fileTable = new Table({
head: ['File', '# of errors'],
colWidths: [90, 20],
});
[...errorsPerFile]
.sort((a, b) => b[1] - a[1])
.forEach(([path, count]) => {
fileTable.push([path, count]);
});
console.log(fileTable.toString());
console.log(`\n${chalk.bold.yellow.underline(`Remaining errors: ${errorsCount}`)}`);
fs.writeFileSync(OUTPUT_TXT_FILE, lines.join('\n'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment