UNPKG

meocord

Version:

Decorator-based Discord bot framework built on discord.js. Brings NestJS-style controllers, dependency injection, guards, and testing utilities to bot development — with a full CLI and TypeScript-first design.

93 lines (90 loc) 3.69 kB
import { Argument } from 'commander'; import { ControllerGeneratorHelper } from './helper/controller-generator.helper.js'; import { Logger } from '../common/logger.js'; import '../common/theme.js'; import { ServiceGeneratorHelper } from './helper/service-generator.helper.js'; import { GuardGeneratorHelper } from './helper/guard-generator.helper.js'; import wait from '../util/wait.util.js'; class GeneratorCLI { register(program) { const generatorCommand = program.command('generate').alias('g').description('Generate components'); generatorCommand.command('controller').alias('co').description('Generate a controller component').addArgument(new Argument('<type>', 'Type of the controller (e.g., button, context-menu, etc.)').choices([ 'button', 'context-menu', 'message', 'modal-submit', 'reaction', 'select-menu', 'slash' ])).addArgument(new Argument('<name>', 'Name of the controller')).action(async (type, name)=>{ await this.handleGenerateComponent({ component: 'controller', type: type, name }); }); generatorCommand.command('service').alias('s').addArgument(new Argument('<name>', 'Name of the service.')).description('Generate a service component').action(async (name)=>{ await this.handleGenerateComponent({ component: 'service', name }); }); generatorCommand.command('guard').alias('gu').addArgument(new Argument('<name>', 'Name of the guard.')).description('Generate a guard component').action(async (name)=>{ await this.handleGenerateComponent({ component: 'guard', name }); }); return program; } async handleGenerateComponent(args) { const { component, name, type } = args; if (!name) { this.logger.error('Name is required'); await wait(100); process.exit(1); } switch(component){ case 'controller': if (!type) { this.logger.error('Type is required for controllers'); await wait(100); process.exit(1); } await this.handleGenerateController({ name, type }); break; case 'service': this.serviceGeneratorHelper.generateService(name); break; case 'guard': this.guardGeneratorHelper.generateGuard(name); break; default: this.logger.error(`Unsupported component type: ${component}`); await wait(100); process.exit(1); } } async handleGenerateController(args) { try { this.controllerGeneratorHelper.generateController({ controllerName: args.name }, args.type); } catch (error) { this.logger.error(`Error generating controller: ${error instanceof Error ? error.message : String(error)}`); await wait(100); process.exit(1); } } constructor(appName){ this.appName = appName; this.logger = new Logger(this.appName); this.controllerGeneratorHelper = new ControllerGeneratorHelper(); this.serviceGeneratorHelper = new ServiceGeneratorHelper(this.appName); this.guardGeneratorHelper = new GuardGeneratorHelper(this.appName); } } export { GeneratorCLI };