@rayburst/sharity
Version:
Analyze shared package usage across monorepos - calculate symbol sharing percentages, track exclusive imports, and identify unused exports
116 lines • 4.99 kB
JavaScript
;
/**
* CLI entry point for sharity
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const analyzer_1 = require("../core/analyzer");
const formatter_1 = require("./formatter");
const program = new commander_1.Command();
program
.name('sharity')
.description('Analyze shared package usage across monorepos')
.version('0.1.0')
.argument('<package-path>', 'Path to the package to analyze')
.option('-c, --consumers <globs...>', 'Glob patterns for consumer packages (e.g., "apps/*" "packages/*")', ['apps/*', 'packages/*'])
.option('-e, --exclude <patterns...>', 'Patterns to exclude from analysis', [])
.option('-t, --threshold <number>', 'Minimum sharing percentage threshold for CI (exit 1 if below)', parseFloat)
.option('-f, --format <type>', 'Output format: table or json', 'table')
.option('--include-tests', 'Include test files in analysis', false)
.option('--config <path>', 'Load configuration from file')
.option('-v, --verbose', 'Verbose output', false)
.action(async (packagePath, options) => {
try {
// Load config from file if specified
let config = {};
if (options.config) {
const configPath = path.resolve(process.cwd(), options.config);
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
}
else {
console.error(`Config file not found: ${configPath}`);
process.exit(1);
}
}
// Resolve package path
const resolvedPackagePath = path.resolve(process.cwd(), packagePath);
if (!fs.existsSync(resolvedPackagePath)) {
console.error(`Package path does not exist: ${resolvedPackagePath}`);
process.exit(1);
}
// Build final config (CLI options override config file)
const analyzerConfig = {
packagePath: resolvedPackagePath,
consumerGlobs: options.consumers || config.consumerGlobs || ['apps/*', 'packages/*'],
excludePatterns: options.exclude || config.excludePatterns || [],
includeTests: options.includeTests ?? config.includeTests ?? false,
sharingThreshold: options.threshold ?? config.sharingThreshold,
outputFormat: options.format || config.outputFormat || 'table',
verbose: options.verbose ?? config.verbose ?? false,
};
// Run analysis
const analyzer = new analyzer_1.SharedPackageAnalyzer(analyzerConfig);
const result = await analyzer.analyze();
// Format output
if (analyzerConfig.outputFormat === 'json') {
console.log((0, formatter_1.formatAsJson)(result));
}
else {
console.log((0, formatter_1.formatAsTable)(result));
}
// Check threshold if specified
if (analyzerConfig.sharingThreshold !== undefined) {
const thresholdCheck = (0, formatter_1.formatThresholdResult)(result, analyzerConfig.sharingThreshold);
console.log('\n' + thresholdCheck.message);
if (!thresholdCheck.passed) {
process.exit(1);
}
}
}
catch (error) {
console.error('Error during analysis:', error);
if (error instanceof Error) {
console.error(error.stack);
}
process.exit(1);
}
});
program.parse();
//# sourceMappingURL=index.js.map