/
report-to-stdout.ts
113 lines (95 loc) · 2.59 KB
/
report-to-stdout.ts
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
105
106
107
108
109
110
111
112
113
import cliui from '@isaacs/cliui';
import chalk from 'chalk';
import Table from 'cli-table3';
import { NEW_LINE } from './md';
import {
CODE_PUSHUP_DOMAIN,
FOOTER_PREFIX,
countCategoryAudits,
formatReportScore,
reportHeadlineText,
reportRawOverviewTableHeaders,
} from './report';
import { ScoredReport } from './scoring';
function addLine(line = ''): string {
return line + NEW_LINE;
}
export function reportToStdout(report: ScoredReport): string {
let output = '';
output += addLine(reportToHeaderSection(report));
output += addLine();
output += addLine(reportToDetailSection(report));
output += addLine(reportToOverviewSection(report));
output += addLine(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
return output;
}
function reportToHeaderSection(report: ScoredReport): string {
const { packageName, version } = report;
return `${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`;
}
function reportToOverviewSection({
categories,
plugins,
}: ScoredReport): string {
let output = addLine(chalk.magentaBright.bold('Categories'));
output += addLine();
const table = new Table({
head: reportRawOverviewTableHeaders,
colAligns: ['left', 'right', 'right'],
style: {
head: ['cyan'],
},
});
table.push(
...categories.map(({ title, score, refs }) => [
title,
withColor({ score }),
countCategoryAudits(refs, plugins),
]),
);
output += addLine(table.toString());
return output;
}
function reportToDetailSection(report: ScoredReport): string {
const { plugins } = report;
let output = '';
plugins.forEach(({ title, audits }) => {
output += addLine();
output += addLine(chalk.magentaBright.bold(`${title} audits`));
output += addLine();
const ui = cliui({ width: 80 });
audits.forEach(({ score, title, displayValue, value }) => {
ui.div(
{
text: withColor({ score, text: '●' }),
width: 2,
padding: [0, 1, 0, 0],
},
{
text: title,
padding: [0, 3, 0, 0],
},
{
text: chalk.cyanBright(displayValue || `${value}`),
width: 10,
padding: [0, 0, 0, 0],
},
);
});
output += addLine(ui.toString());
output += addLine();
});
return output;
}
function withColor({ score, text }: { score: number; text?: string }) {
let str = text ?? formatReportScore(score);
const style = text ? chalk : chalk.bold;
if (score < 0.5) {
str = style.red(str);
} else if (score < 0.9) {
str = style.yellow(str);
} else {
str = style.green(str);
}
return str;
}