UNPKG

pr-sizewise

Version:

A CLI tool that measures and reports pull request sizes for GitHub and GitLab, helping teams maintain manageable code changes.

116 lines 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupAnalyzeCommand = setupAnalyzeCommand; exports.setupInitCommand = setupInitCommand; const analyzer_1 = require("../analyzer"); const providers_1 = require("../providers"); const config_1 = require("./config"); const env_1 = require("./env"); const output_1 = require("./output"); const wizard_1 = require("./wizard"); const errors_1 = require("../utils/errors"); const logger_1 = require("../utils/logger"); const logger = (0, logger_1.createDefaultLogger)(); /** * Setup analyze command (default) */ function setupAnalyzeCommand(program) { program .option('--pr-id <id>', 'Pull/Merge request ID to analyze') .option('--mr-id <id>', 'Merge request ID to analyze (alias for --pr-id)') .option('--project-id <id>', 'Project ID (GitLab: project-id, GitHub: owner/repo)') .option('--token <token>', 'API token for authentication') .option('--host <url>', 'Platform host URL (e.g., https://gitlab.com, https://github.com)') .option('--platform <platform>', 'Platform to use (gitlab, github) - auto-detected if not specified') .option('-c, --config <path>', 'Path to configuration file') .option('-v, --verbose', 'Show detailed output', false) .option('-j, --json', 'Output results in JSON format', false) .option('--no-exit-code', 'Don\'t exit with error code for large PRs/MRs', false) .action(handleAnalyzeCommand); } /** * Setup init command */ function setupInitCommand(program) { program .command('init') .description('Initialize a configuration file through an interactive wizard') .option('--platform <platform>', 'Platform to use (gitlab, github) - skips platform selection') .option('--force', 'Force overwrite of existing configuration file') .option('--no-wizard', 'Use default configuration without wizard') .action(async (options) => { try { if (options.platform && !['github', 'gitlab'].includes(options.platform)) { throw new errors_1.ValidationError('Platform must be either "github" or "gitlab"'); } if (options.wizard === false) { // Use old behavior with platform if (!options.platform) { throw new errors_1.ValidationError('Platform is required when not using wizard'); } (0, config_1.createConfigFile)(options.platform, options.force ?? false); } else { // Run interactive wizard await (0, wizard_1.runConfigWizard)(); } process.exit(0); } catch (error) { (0, output_1.displayError)(error, false); process.exit(1); } }); } /** * Handle analyze command */ async function handleAnalyzeCommand(options) { try { // Auto-detect platform if not specified const detectedPlatform = options.platform ?? (0, providers_1.detectPlatform)(); if (!detectedPlatform) { throw new errors_1.PlatformError('Could not auto-detect platform. Please specify --platform (gitlab, github) or ensure you\'re running in a supported CI environment.'); } const platform = detectedPlatform; if (!options.json) { logger.info(`Detected platform: ${platform.toUpperCase()}`); } // Get and validate required values const values = (0, env_1.getRequiredValues)(options, platform); const errors = (0, env_1.validateRequiredValues)(values); // Check for either PR ID or MR ID if (!values.prId) { throw new errors_1.ValidationError('Pull/Merge request ID is required. Please provide a valid PR/MR ID using --pr-id or --mr-id.'); } if (errors.length > 0) { throw new errors_1.ValidationError(`Missing required values: ${errors.join(', ')}`); } // Load configuration const config = (0, config_1.loadConfig)(options.config); // Create and run analyzer const analyzer = await analyzer_1.UniversalSizeWiseAnalyzer.createWithAutoDetect(config, { platform, token: values.token, host: values.host, projectId: values.projectId, }); const result = await analyzer.analyzePullRequest(values.prId); // Output results if (options.json) { logger.json((0, output_1.formatJsonOutput)(result, true, platform)); } else { (0, output_1.displayConsoleOutput)(result, platform, options.verbose ?? false); } // Check size warning and exit code if (options.exitCode !== false && (0, output_1.checkAndDisplaySizeWarning)(result, config.thresholds, platform, options.json ?? false)) { process.exit(1); } } catch (error) { (0, output_1.displayError)(error, options.json ?? false); process.exit(1); } } //# sourceMappingURL=commands.js.map