Skip to content

Commit

Permalink
feat(utils): implement a new design for stdout
Browse files Browse the repository at this point in the history
Closes #190
  • Loading branch information
IKatsuba committed Nov 6, 2023
1 parent 213a4b8 commit 5d88568
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 63 deletions.
3 changes: 1 addition & 2 deletions e2e/cli-e2e/tests/collect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { cleanFolderPutGitKeep } from '../mocks/fs.mock';

describe('CLI collect', () => {
const exampleCategoryTitle = 'Code style';
const exampleAuditTitle =
'Require `const` declarations for variables that are never reassigned after declared';
const exampleAuditTitle = 'Require `const` declarations for variables';

const omitVariableData = ({
date,
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@swc/helpers": "~0.5.0",
"bundle-require": "^4.0.1",
"chalk": "^5.3.0",
"cli-table3": "^0.6.3",
"cliui": "^8.0.1",
"multi-progress-bars": "^5.0.3",
"simple-git": "^3.20.0",
Expand All @@ -26,7 +27,6 @@
"@commitlint/config-conventional": "^17.7.0",
"@commitlint/config-nx-scopes": "^17.6.4",
"@commitlint/cz-commitlint": "^17.7.1",
"commitlint-plugin-tense": "^1.0.2",
"@nx/devkit": "^16.8.1",
"@nx/esbuild": "16.7.4",
"@nx/eslint-plugin": "16.7.4",
Expand All @@ -52,6 +52,7 @@
"@vitest/ui": "~0.32.0",
"benchmark": "^2.1.4",
"commitizen": "^4.3.0",
"commitlint-plugin-tense": "^1.0.2",
"dotenv": "^16.3.1",
"esbuild": "^0.17.17",
"eslint": "~8.46.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"chalk": "^5.3.0",
"cliui": "^8.0.1",
"simple-git": "^3.20.0",
"multi-progress-bars": "^5.0.3"
"multi-progress-bars": "^5.0.3",
"cli-table3": "^0.6.3"
}
}
126 changes: 67 additions & 59 deletions packages/utils/src/lib/report-to-stdout.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import chalk from 'chalk';
import Table from 'cli-table3';
import cliui from 'cliui';
import { NEW_LINE } from './md';
import { CODE_PUSHUP_DOMAIN, FOOTER_PREFIX, countWeightedRefs } from './report';
import {
CODE_PUSHUP_DOMAIN,
FOOTER_PREFIX,
countCategoryAudits,
} from './report';
import { ScoredReport } from './scoring';
import { reportHeadlineText, reportOverviewTableHeaders } from './utils';

const ui = cliui({ width: 60 }); // @TODO check display width
import {
formatReportScore,
reportHeadlineText,
reportRawOverviewTableHeaders,
} from './utils';

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);
reportToOverviewSection(report);
console.log(NEW_LINE);
console.log(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
}

Expand Down Expand Up @@ -42,67 +49,68 @@ function reportToMetaSection(report: ScoredReport): void {
_print(`---`);
}

function reportToOverviewSection(report: ScoredReport): void {
const base = {
width: 20,
padding: [0, 1, 0, 1],
};

// table header
ui.div(...reportOverviewTableHeaders.map(text => ({ text, ...base })));
function reportToOverviewSection({ categories, plugins }: ScoredReport): void {
console.log(chalk.magentaBright.bold('Categories') + '\n');

// 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,
},
);
const table = new Table({
head: reportRawOverviewTableHeaders,
colAligns: ['left', 'right', 'right'],
style: {
head: ['cyan'],
},
});

console.log(ui.toString());
table.push(
...categories.map(({ title, score, refs }) => [
title,
withColor({ score }),
countCategoryAudits(refs, plugins),
]),
);

console.log(table.toString());
}

function reportToDetailSection(report: ScoredReport): void {
const { categories, plugins } = report;
const ui = cliui({ width: 80 });

categories.forEach(category => {
const { title, refs, score } = category;
const { plugins } = report;

console.log(chalk.bold(`${title} ${score}`));
plugins.forEach(({ title, audits }) => {
ui.div(chalk.magentaBright.bold(`${title} audits`) + '\n');

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}`);
}
},
);
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],
},
);
});
});

console.log(ui.toString());
}

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;
}
1 change: 1 addition & 0 deletions packages/utils/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const reportOverviewTableHeaders = [
'⭐ Score',
'🛡 Audits',
];
export const reportRawOverviewTableHeaders = ['Category', 'Score', 'Audits'];
export const reportMetaTableHeaders: string[] = [
'Commit',
'Version',
Expand Down

0 comments on commit 5d88568

Please sign in to comment.