@re-shell/cli
Version:
Full-stack development platform uniting microservices and microfrontends. Build complete applications with .NET (ASP.NET Core Web API, Minimal API), Java (Spring Boot, Quarkus, Micronaut, Vert.x), Rust (Actix-Web, Warp, Rocket, Axum), Python (FastAPI, Dja
446 lines (445 loc) ⢠22.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listPluginCommands = listPluginCommands;
exports.showCommandConflicts = showCommandConflicts;
exports.resolveCommandConflicts = resolveCommandConflicts;
exports.showCommandStats = showCommandStats;
exports.registerTestCommand = registerTestCommand;
exports.unregisterCommand = unregisterCommand;
exports.showCommandInfo = showCommandInfo;
const chalk_1 = __importDefault(require("chalk"));
const spinner_1 = require("../utils/spinner");
const error_handler_1 = require("../utils/error-handler");
const plugin_command_registry_1 = require("../utils/plugin-command-registry");
const plugin_system_1 = require("../utils/plugin-system");
// List registered plugin commands
async function listPluginCommands(options = {}) {
const { verbose = false, json = false, plugin, category, active, conflicts } = options;
try {
const pluginRegistry = (0, plugin_system_1.createPluginRegistry)();
await pluginRegistry.initialize();
// Create a temporary command registry to inspect registered commands
// In a real implementation, this would be injected from the main CLI
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
// Get registered commands
let commands = commandRegistry.getCommands();
// Apply filters
if (plugin) {
commands = commands.filter(cmd => cmd.pluginName === plugin);
}
if (category) {
commands = commands.filter(cmd => cmd.definition.category === category);
}
if (active !== undefined) {
commands = commands.filter(cmd => cmd.isActive === active);
}
if (conflicts) {
commands = commands.filter(cmd => cmd.conflicts.length > 0);
}
if (json) {
console.log(JSON.stringify(commands, null, 2));
return;
}
console.log(chalk_1.default.cyan('\nš Registered Plugin Commands\n'));
if (commands.length === 0) {
console.log(chalk_1.default.yellow('No plugin commands found matching criteria.'));
return;
}
// Group by plugin
const commandsByPlugin = commands.reduce((acc, cmd) => {
if (!acc[cmd.pluginName]) {
acc[cmd.pluginName] = [];
}
acc[cmd.pluginName].push(cmd);
return acc;
}, {});
Object.entries(commandsByPlugin).forEach(([pluginName, pluginCommands]) => {
console.log(chalk_1.default.blue(`${pluginName} (${pluginCommands.length} commands)`));
pluginCommands.forEach(cmd => {
const statusIcon = cmd.isActive ? chalk_1.default.green('ā') : chalk_1.default.red('ā');
const conflictBadge = cmd.conflicts.length > 0 ? chalk_1.default.red(' [CONFLICT]') : '';
const deprecatedBadge = cmd.definition.deprecated ? chalk_1.default.yellow(' [DEPRECATED]') : '';
console.log(` ${statusIcon} ${chalk_1.default.white(cmd.definition.name)}${conflictBadge}${deprecatedBadge}`);
console.log(` ${chalk_1.default.gray(cmd.definition.description)}`);
if (cmd.definition.aliases && cmd.definition.aliases.length > 0) {
console.log(` ${chalk_1.default.gray(`Aliases: ${cmd.definition.aliases.join(', ')}`)}`);
}
if (verbose) {
console.log(` ${chalk_1.default.gray(`ID: ${cmd.id}`)}`);
console.log(` ${chalk_1.default.gray(`Registered: ${new Date(cmd.registeredAt).toLocaleString()}`)}`);
console.log(` ${chalk_1.default.gray(`Usage: ${cmd.usageCount} times`)}`);
if (cmd.lastUsed) {
console.log(` ${chalk_1.default.gray(`Last used: ${new Date(cmd.lastUsed).toLocaleString()}`)}`);
}
if (cmd.definition.category) {
console.log(` ${chalk_1.default.gray(`Category: ${cmd.definition.category}`)}`);
}
if (cmd.definition.priority !== undefined) {
console.log(` ${chalk_1.default.gray(`Priority: ${cmd.definition.priority}`)}`);
}
if (cmd.conflicts.length > 0) {
console.log(` ${chalk_1.default.red(`Conflicts: ${cmd.conflicts.join(', ')}`)}`);
}
}
console.log('');
});
});
// Summary
const totalCommands = commands.length;
const activeCommands = commands.filter(cmd => cmd.isActive).length;
const conflictingCommands = commands.filter(cmd => cmd.conflicts.length > 0).length;
console.log(chalk_1.default.yellow('Summary:'));
console.log(` Total commands: ${totalCommands}`);
console.log(` Active: ${chalk_1.default.green(activeCommands)}`);
console.log(` Inactive: ${chalk_1.default.red(totalCommands - activeCommands)}`);
if (conflictingCommands > 0) {
console.log(` Conflicts: ${chalk_1.default.red(conflictingCommands)}`);
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to list plugin commands: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Show command conflicts
async function showCommandConflicts(options = {}) {
const { verbose = false, json = false } = options;
try {
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const conflicts = commandRegistry.getConflicts();
if (json) {
const conflictData = Object.fromEntries(conflicts);
console.log(JSON.stringify(conflictData, null, 2));
return;
}
console.log(chalk_1.default.cyan('\nā ļø Command Conflicts\n'));
if (conflicts.size === 0) {
console.log(chalk_1.default.green('No command conflicts detected.'));
return;
}
conflicts.forEach((conflictingIds, commandName) => {
if (conflictingIds.length > 1) {
console.log(chalk_1.default.red(`Command: ${commandName}`));
console.log(chalk_1.default.yellow(` ${conflictingIds.length} conflicting registrations:`));
conflictingIds.forEach(commandId => {
const command = commandRegistry.getCommand(commandId);
if (command) {
const statusIcon = command.isActive ? chalk_1.default.green('ā') : chalk_1.default.red('ā');
console.log(` ${statusIcon} ${chalk_1.default.white(command.id)} (${command.pluginName})`);
if (verbose) {
console.log(` Priority: ${command.definition.priority || 0}`);
console.log(` Registered: ${new Date(command.registeredAt).toLocaleString()}`);
console.log(` Usage: ${command.usageCount} times`);
}
}
});
console.log('');
}
});
console.log(chalk_1.default.yellow('\nš” Resolution suggestions:'));
console.log(' ⢠Use plugin command priorities to auto-resolve conflicts');
console.log(' ⢠Disable conflicting commands manually');
console.log(' ⢠Use unique command names in plugin development');
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to show command conflicts: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Resolve command conflicts
async function resolveCommandConflicts(commandName, resolution, options = {}) {
const { verbose = false } = options;
try {
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const spinner = (0, spinner_1.createSpinner)(`Resolving conflicts for '${commandName}' using ${resolution} strategy...`);
spinner.start();
const success = await commandRegistry.resolveConflicts(commandName, resolution);
spinner.stop();
if (success) {
console.log(chalk_1.default.green(`ā Successfully resolved conflicts for '${commandName}'`));
if (verbose) {
const conflicts = commandRegistry.getConflicts().get(commandName);
if (conflicts) {
console.log(chalk_1.default.yellow('\nResolution details:'));
conflicts.forEach(commandId => {
const command = commandRegistry.getCommand(commandId);
if (command) {
const status = command.isActive ? chalk_1.default.green('active') : chalk_1.default.red('disabled');
console.log(` ${command.id}: ${status}`);
}
});
}
}
}
else {
console.log(chalk_1.default.red(`ā Failed to resolve conflicts for '${commandName}'`));
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to resolve command conflicts: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Show command registry statistics
async function showCommandStats(options = {}) {
const { verbose = false, json = false, usage = false } = options;
try {
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const stats = commandRegistry.getStats();
if (json) {
console.log(JSON.stringify(stats, null, 2));
return;
}
console.log(chalk_1.default.cyan('\nš Command Registry Statistics\n'));
// Basic stats
console.log(chalk_1.default.yellow('Overview:'));
console.log(` Total commands: ${stats.totalCommands}`);
console.log(` Active commands: ${chalk_1.default.green(stats.activeCommands)}`);
console.log(` Inactive commands: ${chalk_1.default.red(stats.totalCommands - stats.activeCommands)}`);
console.log(` Total aliases: ${stats.totalAliases}`);
console.log(` Command conflicts: ${stats.totalConflicts > 0 ? chalk_1.default.red(stats.totalConflicts) : chalk_1.default.green(0)}`);
// Commands by plugin
if (Object.keys(stats.commandsByPlugin).length > 0) {
console.log(chalk_1.default.yellow('\nCommands by Plugin:'));
Object.entries(stats.commandsByPlugin).forEach(([plugin, count]) => {
console.log(` ${plugin}: ${count}`);
});
}
// Usage statistics
if (usage && (stats.mostUsedCommands.length > 0 || stats.recentCommands.length > 0)) {
if (stats.mostUsedCommands.length > 0) {
console.log(chalk_1.default.yellow('\nMost Used Commands:'));
stats.mostUsedCommands.forEach((cmd, index) => {
console.log(` ${index + 1}. ${cmd.name} (${cmd.plugin}): ${cmd.usageCount} times`);
});
}
if (stats.recentCommands.length > 0) {
console.log(chalk_1.default.yellow('\nRecently Used Commands:'));
stats.recentCommands.forEach((cmd, index) => {
const lastUsed = new Date(cmd.lastUsed).toLocaleString();
console.log(` ${index + 1}. ${cmd.name} (${cmd.plugin}): ${lastUsed}`);
});
}
}
if (verbose) {
console.log(chalk_1.default.yellow('\nRegistry Configuration:'));
// Would show registry configuration details
console.log(' Conflict resolution: priority');
console.log(' Middleware enabled: true');
console.log(' Usage tracking: true');
console.log(' Permission validation: true');
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to show command statistics: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Register a command from a plugin (for testing/development)
async function registerTestCommand(pluginName, commandDefinition, options = {}) {
const { verbose = false } = options;
try {
// Parse command definition (would be JSON or YAML)
let definition;
try {
definition = JSON.parse(commandDefinition);
}
catch {
throw new error_handler_1.ValidationError('Command definition must be valid JSON');
}
const pluginRegistry = (0, plugin_system_1.createPluginRegistry)();
await pluginRegistry.initialize();
const plugin = pluginRegistry.getPlugin(pluginName);
if (!plugin) {
throw new error_handler_1.ValidationError(`Plugin '${pluginName}' not found`);
}
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const spinner = (0, spinner_1.createSpinner)(`Registering command '${definition.name}' from plugin '${pluginName}'...`);
spinner.start();
const result = await commandRegistry.registerCommand(plugin, definition);
spinner.stop();
if (result.success) {
console.log(chalk_1.default.green(`ā Successfully registered command '${definition.name}'`));
console.log(` Command ID: ${result.commandId}`);
if (result.warnings.length > 0) {
console.log(chalk_1.default.yellow(' Warnings:'));
result.warnings.forEach(warning => {
console.log(` ${chalk_1.default.yellow('ā ')} ${warning}`);
});
}
if (verbose && result.conflicts.length > 0) {
console.log(chalk_1.default.red(' Conflicts detected:'));
result.conflicts.forEach(conflict => {
console.log(` ${chalk_1.default.red('ā')} ${conflict}`);
});
}
}
else {
console.log(chalk_1.default.red(`ā Failed to register command '${definition.name}'`));
if (result.errors.length > 0) {
console.log(chalk_1.default.red(' Errors:'));
result.errors.forEach(error => {
console.log(` ${chalk_1.default.red('ā')} ${error}`);
});
}
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to register test command: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Unregister a command
async function unregisterCommand(commandId, options = {}) {
const { verbose = false } = options;
try {
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const command = commandRegistry.getCommand(commandId);
if (!command) {
console.log(chalk_1.default.yellow(`Command '${commandId}' not found`));
return;
}
const spinner = (0, spinner_1.createSpinner)(`Unregistering command '${commandId}'...`);
spinner.start();
const success = await commandRegistry.unregisterCommand(commandId);
spinner.stop();
if (success) {
console.log(chalk_1.default.green(`ā Successfully unregistered command '${commandId}'`));
if (verbose) {
console.log(` Command: ${command.definition.name}`);
console.log(` Plugin: ${command.pluginName}`);
console.log(` Usage count: ${command.usageCount}`);
}
}
else {
console.log(chalk_1.default.red(`ā Failed to unregister command '${commandId}'`));
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to unregister command: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Show detailed information about a specific command
async function showCommandInfo(commandId, options = {}) {
const { verbose = false, json = false } = options;
try {
const { Command } = require('commander');
const tempProgram = new Command();
const commandRegistry = (0, plugin_command_registry_1.createPluginCommandRegistry)(tempProgram);
await commandRegistry.initialize();
const command = commandRegistry.getCommand(commandId);
if (!command) {
console.log(chalk_1.default.red(`Command '${commandId}' not found`));
return;
}
if (json) {
console.log(JSON.stringify(command, null, 2));
return;
}
console.log(chalk_1.default.cyan(`\nš Command Information: ${command.definition.name}\n`));
// Basic information
console.log(chalk_1.default.yellow('Basic Information:'));
console.log(` Name: ${chalk_1.default.white(command.definition.name)}`);
console.log(` Description: ${command.definition.description}`);
console.log(` Plugin: ${chalk_1.default.blue(command.pluginName)}`);
console.log(` ID: ${command.id}`);
console.log(` Status: ${command.isActive ? chalk_1.default.green('Active') : chalk_1.default.red('Inactive')}`);
if (command.definition.category) {
console.log(` Category: ${command.definition.category}`);
}
if (command.definition.priority !== undefined) {
console.log(` Priority: ${command.definition.priority}`);
}
// Aliases
if (command.definition.aliases && command.definition.aliases.length > 0) {
console.log(` Aliases: ${command.definition.aliases.join(', ')}`);
}
// Arguments
if (command.definition.arguments && command.definition.arguments.length > 0) {
console.log(chalk_1.default.yellow('\nArguments:'));
command.definition.arguments.forEach(arg => {
const required = arg.required ? chalk_1.default.red('required') : chalk_1.default.green('optional');
console.log(` ${arg.name} (${required}): ${arg.description}`);
if (arg.type)
console.log(` Type: ${arg.type}`);
if (arg.choices)
console.log(` Choices: ${arg.choices.join(', ')}`);
if (arg.defaultValue !== undefined)
console.log(` Default: ${arg.defaultValue}`);
});
}
// Options
if (command.definition.options && command.definition.options.length > 0) {
console.log(chalk_1.default.yellow('\nOptions:'));
command.definition.options.forEach(opt => {
const required = opt.required ? chalk_1.default.red('required') : chalk_1.default.green('optional');
console.log(` ${opt.flag} (${required}): ${opt.description}`);
if (opt.type)
console.log(` Type: ${opt.type}`);
if (opt.choices)
console.log(` Choices: ${opt.choices.join(', ')}`);
if (opt.defaultValue !== undefined)
console.log(` Default: ${opt.defaultValue}`);
if (opt.conflicts)
console.log(` Conflicts: ${opt.conflicts.join(', ')}`);
if (opt.implies)
console.log(` Implies: ${opt.implies.join(', ')}`);
});
}
// Examples
if (command.definition.examples && command.definition.examples.length > 0) {
console.log(chalk_1.default.yellow('\nExamples:'));
command.definition.examples.forEach(example => {
console.log(` ${chalk_1.default.gray(example)}`);
});
}
// Usage statistics
console.log(chalk_1.default.yellow('\nUsage Statistics:'));
console.log(` Usage count: ${command.usageCount}`);
console.log(` Registered: ${new Date(command.registeredAt).toLocaleString()}`);
if (command.lastUsed) {
console.log(` Last used: ${new Date(command.lastUsed).toLocaleString()}`);
}
// Conflicts
if (command.conflicts.length > 0) {
console.log(chalk_1.default.red('\nConflicts:'));
command.conflicts.forEach(conflictId => {
const conflictCmd = commandRegistry.getCommand(conflictId);
if (conflictCmd) {
console.log(` ${conflictId} (${conflictCmd.pluginName})`);
}
else {
console.log(` ${conflictId}`);
}
});
}
// Additional details in verbose mode
if (verbose) {
console.log(chalk_1.default.yellow('\nAdditional Details:'));
console.log(` Hidden: ${command.definition.hidden || false}`);
console.log(` Deprecated: ${command.definition.deprecated || false}`);
console.log(` Permission: ${command.definition.permission || 'none'}`);
console.log(` Middleware count: ${command.definition.middleware?.length || 0}`);
console.log(` Subcommands: ${command.definition.subcommands?.length || 0}`);
}
}
catch (error) {
throw new error_handler_1.ValidationError(`Failed to show command information: ${error instanceof Error ? error.message : String(error)}`);
}
}