mira-consciousness
Version:
Memory & Intelligence Retention Archive - Preserving The Spark
104 lines • 2.64 kB
JavaScript
import { Command } from 'commander';
/**
* Builder class for creating commands with reduced complexity
*/
export class CommandBuilder {
command;
constructor(name) {
this.command = new Command(name);
}
/**
* Add command description
*/
description(desc) {
this.command.description(desc);
return this;
}
/**
* Add command arguments
*/
arguments(args) {
this.command.arguments(args);
return this;
}
/**
* Add command aliases
*/
aliases(aliases) {
aliases.forEach(alias => {
this.command.alias(alias);
});
return this;
}
/**
* Add command options
*/
options(options) {
options.forEach(option => {
if (option.defaultValue !== undefined) {
this.command.option(option.flags, option.description, option.defaultValue);
}
else {
this.command.option(option.flags, option.description);
}
});
return this;
}
/**
* Set command action
*/
action(handler) {
this.command.action(handler);
return this;
}
/**
* Get the built command
*/
build() {
return this.command;
}
/**
* Create a command from configuration
*/
static fromConfig(config) {
const builder = new CommandBuilder(config.name);
builder.description(config.description);
if (config.arguments) {
builder.arguments(config.arguments);
}
if (config.aliases) {
builder.aliases(config.aliases);
}
if (config.options) {
builder.options(config.options);
}
return builder;
}
}
/**
* Create action handler with proper error handling
*/
export function createActionHandler(action, importPath, commandActions) {
return async (...args) => {
try {
if (typeof action === 'function') {
await action(...args);
}
else if (importPath) {
const module = await import(importPath);
await module[action](...args);
}
else if (commandActions && commandActions[action]) {
await commandActions[action](...args);
}
else {
throw new Error(`Action ${action} not found`);
}
}
catch (error) {
console.error(`Error executing command:`, error);
process.exit(1);
}
};
}
//# sourceMappingURL=command-builder.js.map