Skip to content

Commit

Permalink
feat: add cli table builder for results
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcarman committed May 16, 2024
1 parent 7026932 commit 50941c1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/commands/metalint/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Messages } from '@salesforce/core';
import { fileExists, readAllFiles } from '../util.js';
import { generateSarifResults } from '../../common/sarif-builder.js';
import { generateCsvResults } from '../../common/csv-builder.js';
import { generateTableResults } from '../../common/table-builder.js';
import { ruleClassMap } from '../../common/types.js';
import * as rulesModule from '../../rules/_rules.js';
import type { RuleClasses, RuleResults } from '../../common/types.js';
Expand Down Expand Up @@ -54,6 +55,8 @@ export default class MetalintRun extends SfCommand<MetalintRunResult> {
// eslint-disable-next-line no-console
console.log(sarifResults);

generateTableResults(ruleResults);

return { outcome: 'Complete' };
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/common/table-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Table from 'cli-table3';
import chalk from 'chalk';
import type { RuleResults } from '../common/types.js';

export function generateTableResults(ruleResults: RuleResults): void {
const headerValues = ['ruleId', 'filePath', 'level', 'startLn', 'endLn', 'startCol', 'endCol'];
const headers = headerValues.map((header) => chalk.bold(header));

const table = new Table({
head: headers,
style: {
head: [],
border: [],
},
colWidths: [40, 80, 10, 10, 10, 10, 10],
});

for (const ruleId in ruleResults) {
if (Object.hasOwn(ruleResults, ruleId)) {
const rule = ruleResults[ruleId];
for (const result of rule.results) {
const row = [];
row.push(
rule.ruleId,
result.filePath,
rule.level === 'warning'
? chalk.yellow(rule.level)
: rule.level === 'error'
? chalk.red(rule.level)
: rule.level,
result.startLine,
result.endLine,
result.startColumn,
result.endColumn
);
table.push(row);
}
}
}

process.stdout.write(table.toString());
}

0 comments on commit 50941c1

Please sign in to comment.