@rayburst/sharity
Version:
Analyze shared package usage across monorepos - calculate symbol sharing percentages, track exclusive imports, and identify unused exports
151 lines • 5.83 kB
JavaScript
;
/**
* CLI table formatter
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAsTable = formatAsTable;
exports.formatAsJson = formatAsJson;
exports.formatThresholdResult = formatThresholdResult;
const cli_table3_1 = __importDefault(require("cli-table3"));
const chalk_1 = __importDefault(require("chalk"));
const consumer_tracker_1 = require("../core/consumer-tracker");
/**
* Format analysis result as CLI tables
*/
function formatAsTable(result) {
let output = '';
// Main summary table
output += formatSummaryTable(result);
output += '\n\n';
// Symbol breakdown table
output += formatSymbolBreakdownTable(result);
output += '\n\n';
// Per-consumer table
output += formatConsumerTable(result);
output += '\n';
return output;
}
/**
* Format main summary table
*/
function formatSummaryTable(result) {
const table = new cli_table3_1.default({
colWidths: [40, 20],
wordWrap: true,
style: {
head: ['cyan', 'bold'],
border: ['gray'],
},
});
const passSymbolThreshold = result.sharedSymbolsPercentage >= 50;
const passLineThreshold = result.sharedLinesPercentage >= 50;
table.push([
{ content: chalk_1.default.bold.cyan(`Shared Package Analysis: ${result.packageName}`), colSpan: 2 },
], ['', ''], [chalk_1.default.bold('Total Symbols'), result.totalSymbols.toString()], [
chalk_1.default.green('Shared (2+ apps)'),
`${result.sharedSymbols} (${result.sharedSymbolsPercentage}%) ${passSymbolThreshold ? '✓' : '⚠️'}`,
], [
chalk_1.default.yellow('Exclusive (1 app)'),
`${result.exclusiveSymbols} (${result.exclusiveSymbolsPercentage}%) ${result.exclusiveSymbolsPercentage > 50 ? '⚠️' : ''}`,
], [
chalk_1.default.gray('Unused (0 apps)'),
`${result.unusedSymbols} (${result.unusedSymbolsPercentage}%)`,
], ['', ''], [chalk_1.default.bold('Total Lines'), result.totalLines.toLocaleString()], [
chalk_1.default.green('Shared lines'),
`${result.sharedLines.toLocaleString()} (${result.sharedLinesPercentage}%) ${passLineThreshold ? '✓' : '⚠️'}`,
], [
chalk_1.default.yellow('Exclusive lines'),
`${result.exclusiveLines.toLocaleString()} (${result.exclusiveLinesPercentage}%) ${result.exclusiveLinesPercentage > 50 ? '⚠️' : ''}`,
], [
chalk_1.default.gray('Unused lines'),
`${result.unusedLines.toLocaleString()} (${result.unusedLinesPercentage}%)`,
]);
return table.toString();
}
/**
* Format symbol breakdown table
*/
function formatSymbolBreakdownTable(result) {
const table = new cli_table3_1.default({
head: [
chalk_1.default.bold('Metric'),
chalk_1.default.bold('Shared (2+ apps)'),
chalk_1.default.bold('Exclusive (1 app)'),
chalk_1.default.bold('Unused (0 apps)'),
],
colWidths: [25, 18, 18, 18],
style: {
head: ['cyan', 'bold'],
border: ['gray'],
},
});
table.push([
'Symbol Count',
`${result.sharedSymbols} (${result.sharedSymbolsPercentage}%)`,
`${result.exclusiveSymbols} (${result.exclusiveSymbolsPercentage}%)`,
`${result.unusedSymbols} (${result.unusedSymbolsPercentage}%)`,
], [
'Lines of Code',
`${result.sharedLines.toLocaleString()} (${result.sharedLinesPercentage}%)`,
`${result.exclusiveLines.toLocaleString()} (${result.exclusiveLinesPercentage}%)`,
`${result.unusedLines.toLocaleString()} (${result.unusedLinesPercentage}%)`,
], [
'Avg Lines/Symbol',
result.avgLinesPerSharedSymbol.toString(),
result.avgLinesPerExclusiveSymbol.toString(),
result.avgLinesPerUnusedSymbol.toString(),
]);
return chalk_1.default.bold('Current State: What\'s Truly Shared\n') + table.toString();
}
/**
* Format per-consumer analysis table
*/
function formatConsumerTable(result) {
const table = new cli_table3_1.default({
head: [
chalk_1.default.bold('Consumer'),
chalk_1.default.bold('Total'),
chalk_1.default.bold('Exclusive'),
chalk_1.default.bold('Exclusive %'),
chalk_1.default.bold('Est. Lines'),
chalk_1.default.bold('Risk'),
],
colWidths: [30, 10, 12, 14, 14, 8],
style: {
head: ['cyan', 'bold'],
border: ['gray'],
},
});
for (const consumer of result.consumerAnalyses) {
const risk = (0, consumer_tracker_1.getRiskLevel)(consumer.exclusivePercentage);
table.push([
consumer.name,
consumer.totalSymbols.toString(),
consumer.exclusiveSymbols.toString(),
`${consumer.exclusivePercentage.toFixed(1)}%`,
consumer.estimatedExclusiveLines.toLocaleString(),
risk,
]);
}
return chalk_1.default.bold('Per-Consumer Analysis\n') + table.toString();
}
/**
* Format analysis result as JSON
*/
function formatAsJson(result) {
return JSON.stringify(result, null, 2);
}
/**
* Format threshold check result
*/
function formatThresholdResult(result, threshold) {
const passed = result.sharedSymbolsPercentage >= threshold;
const message = passed
? chalk_1.default.green(`✓ PASS: Shared percentage (${result.sharedSymbolsPercentage}%) meets threshold (${threshold}%)`)
: chalk_1.default.red(`✗ FAIL: Shared percentage (${result.sharedSymbolsPercentage}%) below threshold (${threshold}%)`);
return { passed, message };
}
//# sourceMappingURL=formatter.js.map