UNPKG

ursamu-mud

Version:

Ursamu - Modular MUD Engine with sandboxed scripting and plugin system

185 lines 4.73 kB
/** * Base command class with common functionality */ import chalk from 'chalk'; import ora from 'ora'; import { Logger } from '../utils/Logger.js'; export class BaseCommand { config; logger; spinner; constructor(config) { this.config = config; this.logger = new Logger(config); } /** * Start a spinner with message */ startSpinner(message) { if (this.spinner) { this.spinner.stop(); } this.spinner = ora({ text: message, color: 'blue', spinner: 'dots' }); if (this.config.get('colorOutput')) { this.spinner.start(); } else { console.log(message); } return this.spinner; } /** * Update spinner message */ updateSpinner(message) { if (this.spinner && this.config.get('colorOutput')) { this.spinner.text = message; } else { console.log(message); } } /** * Stop spinner with success message */ succeedSpinner(message) { if (this.spinner && this.config.get('colorOutput')) { this.spinner.succeed(message); this.spinner = undefined; } else if (message) { console.log(chalk.green('✓'), message); } } /** * Stop spinner with failure message */ failSpinner(message) { if (this.spinner && this.config.get('colorOutput')) { this.spinner.fail(message); this.spinner = undefined; } else if (message) { console.log(chalk.red('✗'), message); } } /** * Stop spinner with warning message */ warnSpinner(message) { if (this.spinner && this.config.get('colorOutput')) { this.spinner.warn(message); this.spinner = undefined; } else if (message) { console.log(chalk.yellow('⚠'), message); } } /** * Stop spinner with info message */ infoSpinner(message) { if (this.spinner && this.config.get('colorOutput')) { this.spinner.info(message); this.spinner = undefined; } else if (message) { console.log(chalk.blue('ℹ'), message); } } /** * Log verbose message */ verbose(message, ...args) { if (this.config.get('verbose')) { console.log(chalk.gray('[VERBOSE]'), message, ...args); } } /** * Log error message */ error(message, error) { console.error(chalk.red('Error:'), message); if (error && this.config.get('verbose')) { console.error(chalk.gray(error.stack)); } } /** * Log warning message */ warn(message) { console.warn(chalk.yellow('Warning:'), message); } /** * Log info message */ info(message) { console.log(chalk.blue('Info:'), message); } /** * Log success message */ success(message) { console.log(chalk.green('Success:'), message); } /** * Handle async command execution with error handling */ async handleCommand(operation, errorMessage = 'Command failed') { try { return await operation(); } catch (error) { this.failSpinner(); this.error(errorMessage, error); process.exit(1); } } /** * Validate required options */ validateRequired(options, required) { const missing = required.filter(key => !options[key]); if (missing.length > 0) { throw new Error(`Missing required options: ${missing.join(', ')}`); } } /** * Format duration in human readable format */ formatDuration(ms) { if (ms < 1000) { return `${ms}ms`; } else if (ms < 60000) { return `${(ms / 1000).toFixed(1)}s`; } else { const minutes = Math.floor(ms / 60000); const seconds = ((ms % 60000) / 1000).toFixed(1); return `${minutes}m ${seconds}s`; } } /** * Format bytes in human readable format */ formatBytes(bytes) { const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`; } /** * Clean up resources */ cleanup() { if (this.spinner) { this.spinner.stop(); this.spinner = undefined; } } } //# sourceMappingURL=BaseCommand.js.map