UNPKG

debt-collector

Version:

a nodejs tool to identify, track and mesure technical debt

89 lines (88 loc) 5.41 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useEffect } from 'react'; import { Text, Box } from 'ink'; import fs from 'fs'; import Table from '../Table.js'; import { truncateString } from '../../lib/utils/index.js'; import compareMarkdownReport from '../../lib/reporters/compareMarkdownReport.js'; import { getResultColor } from '../../lib/utils/index.js'; const cachePath = `${process.cwd()}/node_modules/.cache/debt-collector`; const resultPath = `${cachePath}/report.html`; export function ResultsCompare({ results, currentResults, outputHtml }) { const tableResults = Object.entries(results[0] || {}) .map(([fileName, result]) => { return { file: truncateString(fileName, 60), rev: result.previousRevResult.totalScore, current: result.currentRevResult.totalScore, trend: result.tendency, }; }) .filter((file) => !(file.rev === 0 && file.current === 0)); const totalScores = tableResults.reduce((acc, res) => { const revScore = res.rev + acc.rev; const currentScore = res.current + acc.cur; return { rev: revScore, cur: currentScore, solde: currentScore - revScore, }; }, { rev: 0, cur: 0, solde: 0, }); const noChangesFiles = tableResults .filter((item) => item.trend === 0) .map((file) => ({ ...file, trend: 0, })); const moreDeptFiles = tableResults .filter((item) => item.trend > 0) .map((file) => ({ ...file, trend: file.trend, })); const lessDeptFiles = tableResults .filter((item) => item.trend < 0) .map((file) => ({ ...file, trend: file.trend, })); useEffect(() => { if (outputHtml) { setTimeout(() => { const html = compareMarkdownReport({ config: currentResults.config, previousRevResult: { hash: 'HEAD', date: new Date().toISOString(), name: 'HEAD', totalScore: totalScores.rev, }, currentRevResult: { hash: 'HEAD', date: new Date().toISOString(), name: 'HEAD', totalScore: totalScores.cur, results: currentResults.results }, noChangesFiles, moreDeptFiles, lessDeptFiles }); fs.mkdir(cachePath, { recursive: true }, (err) => { if (err) throw err; fs.writeFileSync(resultPath, html); }); }, 1000); } }, []); return (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1 }), noChangesFiles.length > 0 && (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { underline: true, bold: true, color: "grey", children: "Files with no changes in debt score" }), _jsx(Table, { data: noChangesFiles.map(file => ({ ...file, trend: '=' })) })] })), lessDeptFiles.length > 0 && (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { underline: true, bold: true, color: "green", children: "Files with less debt" }), _jsx(Table, { data: lessDeptFiles.map(file => ({ ...file, trend: `▼ ${file.trend}` })) })] })), moreDeptFiles.length > 0 && (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { underline: true, bold: true, color: "red", children: "Files with more debt" }), _jsx(Table, { data: moreDeptFiles.map(file => ({ ...file, trend: `▲ ${file.trend}` })) })] })), _jsx(Box, { marginTop: 2, borderTop: true, borderBottom: true, borderColor: "green", paddingTop: 1, paddingBottom: 1, children: _jsx(Text, { bold: true, color: "green", children: "The following modified files can be improved :" }) }), currentResults.results.filter((result) => result.totalScore !== 0).map((result) => (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { bold: true, color: "red", underline: true, children: result.fileShortPath }), _jsx(Table, { data: result.brokenRules.map(({ ruleTitle, occurences, ruleTotalSore }) => ({ title: ruleTitle, nb: occurences, score: ruleTotalSore, })) }), _jsxs(Text, { bold: true, color: "red", children: ["Total Debt Score: ", result.totalScore] })] }, result.fileShortPath))), _jsxs(Box, { marginTop: 3, children: [_jsx(Box, { paddingLeft: 1, paddingRight: 1, borderStyle: "round", flexDirection: "column", children: _jsxs(Text, { children: ["REVISION : ", _jsx(Text, { children: totalScores.rev.toString() })] }) }), _jsx(Box, { paddingLeft: 1, paddingRight: 1, borderStyle: "round", flexDirection: "column", children: _jsxs(Text, { children: ["CURRENT : ", _jsx(Text, { children: totalScores.cur.toString() })] }) }), _jsx(Box, { paddingLeft: 1, paddingRight: 1, borderStyle: "round", flexDirection: "column", children: _jsxs(Text, { bold: true, color: getResultColor(totalScores.solde), children: ["DIFF : ", totalScores.solde.toString()] }) })] })] })); }