1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const os = require('os');
|
11 | const codeFrame = require('@babel/code-frame').codeFrameColumns;
|
12 | const chalk = require('chalk');
|
13 | const fs = require('fs');
|
14 |
|
15 | const issueOrigins = {
|
16 | typescript: 'TypeScript',
|
17 | internal: 'fork-ts-checker-webpack-plugin',
|
18 | };
|
19 |
|
20 | function formatter(issue) {
|
21 | const { origin, severity, file, line, message, code, character } = issue;
|
22 |
|
23 | const colors = new chalk.constructor();
|
24 | const messageColor = severity === 'warning' ? colors.yellow : colors.red;
|
25 | const fileAndNumberColor = colors.bold.cyan;
|
26 |
|
27 | const source = file && fs.existsSync(file) && fs.readFileSync(file, 'utf-8');
|
28 | const frame = source
|
29 | ? codeFrame(source, { start: { line: line, column: character } })
|
30 | .split('\n')
|
31 | .map(str => ' ' + str)
|
32 | .join(os.EOL)
|
33 | : '';
|
34 |
|
35 | return [
|
36 | messageColor.bold(`${issueOrigins[origin]} ${severity.toLowerCase()} in `) +
|
37 | fileAndNumberColor(`${file}(${line},${character})`) +
|
38 | messageColor(':'),
|
39 | message + ' ' + messageColor.underline(`TS${code}`),
|
40 | '',
|
41 | frame,
|
42 | ].join(os.EOL);
|
43 | }
|
44 |
|
45 | module.exports = formatter;
|