-
Notifications
You must be signed in to change notification settings - Fork 11
/
report-to-stdout.ts
108 lines (93 loc) · 3.1 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
import chalk from 'chalk';
import cliui from 'cliui';
import { NEW_LINE } from './md';
import { CODE_PUSHUP_DOMAIN, FOOTER_PREFIX, countWeightedRefs } from './report';
import { ScoredReport } from './scoring';
import { reportHeadlineText, reportOverviewTableHeaders } from './utils';
const ui = cliui({ width: 60 }); // @TODO check display width
export function reportToStdout(report: ScoredReport): void {
reportToHeaderSection(report);
reportToMetaSection(report);
console.log(NEW_LINE); // @TODO just use '' and \n does only work in markdown
reportToOverviewSection(report);
console.log(NEW_LINE);
reportToDetailSection(report);
console.log(NEW_LINE);
console.log(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
}
function reportToHeaderSection(report: ScoredReport): void {
const { packageName, version } = report;
console.log(`${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`);
}
function reportToMetaSection(report: ScoredReport): void {
const { date, duration, version, packageName, plugins } = report;
const _print = (text: string) => console.log(chalk.italic(chalk.gray(text)));
_print(`---`);
_print(`Package Name: ${packageName}`);
_print(`Version: ${version}`);
_print(
`Commit: feat(cli): add logic for markdown report - 7eba125ad5643c2f90cb21389fc3442d786f43f9`,
);
_print(`Date: ${date}`);
_print(`Duration: ${duration}ms`);
_print(`Plugins: ${plugins?.length}`);
_print(
`Audits: ${plugins?.reduce((sum, { audits }) => sum + audits.length, 0)}`,
);
_print(`---`);
}
function reportToOverviewSection(report: ScoredReport): void {
const base = {
width: 20,
padding: [0, 1, 0, 1],
};
// table header
ui.div(...reportOverviewTableHeaders.map(text => ({ text, ...base })));
// table content
report.categories.forEach(({ title, refs, score }) => {
const audits = `${refs.length.toString()}/${countWeightedRefs(refs)}`;
ui.div(
{
text: `${title}`,
...base,
},
{
text: score.toString(),
...base,
},
{
text: audits,
...base,
},
);
});
console.log(ui.toString());
}
function reportToDetailSection(report: ScoredReport): void {
const { categories, plugins } = report;
categories.forEach(category => {
const { title, refs, score } = category;
console.log(chalk.bold(`${title} ${score}`));
refs.forEach(
({ slug: auditSlugInCategoryRefs, weight, plugin: pluginSlug }) => {
const audit = plugins
.find(({ slug }) => slug === pluginSlug)
?.audits.find(
({ slug: auditSlugInPluginAudits }) =>
auditSlugInPluginAudits === auditSlugInCategoryRefs,
);
if (audit) {
let content = `${audit.description}` + NEW_LINE;
if (audit.docsUrl) {
content += ` ${audit.docsUrl} ${NEW_LINE}`;
}
console.log(`- ${audit.title} (${weight})`);
console.log(` ${content}`);
} else {
// this should never happen
throw new Error(`No audit found for ${auditSlugInCategoryRefs}`);
}
},
);
});
}