UNPKG

meocord

Version:

MeoCord is a lightweight and modular framework for building scalable Discord bots using TypeScript and Discord.js. It simplifies bot development with an extensible architecture, TypeScript-first approach, and powerful CLI tools.

50 lines 7.95 kB
function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(b,c){if(b){if("string"==typeof b)return _arrayLikeToArray(b,c);var a={}.toString.call(b).slice(8,-1);return"Object"===a&&b.constructor&&(a=b.constructor.name),"Map"===a||"Set"===a?Array.from(b):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_arrayLikeToArray(b,c):void 0}}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _arrayLikeToArray(b,c){(null==c||c>b.length)&&(c=b.length);for(var d=0,f=Array(c);d<c;d++)f[d]=b[d];return f}function _classCallCheck(b,a){if(!(b instanceof a))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,_toPropertyKey(c.key),c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==_typeof(b)?b:b+""}function _toPrimitive(a,b){if("object"!=_typeof(a)||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=_typeof(d))return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}/** * MeoCord Framework * Copyright (C) 2025 Ukasyah Rahmatullah Zada * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */import path from"path";import{ControllerType}from"../../enum/controller.enum.js";import{createDirectoryIfNotExists,generateFile,populateTemplate,validateAndFormatName}from"../../util/generator-cli.util.js";import{fileURLToPath}from"url";var __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename);export var ControllerGeneratorHelper=/*#__PURE__*/function(){function ControllerGeneratorHelper(){_classCallCheck(this,ControllerGeneratorHelper)}return _createClass(ControllerGeneratorHelper,[{key:"generateController",value:/** * Generates a new controller file and an associated structure based on the provided arguments and controller type. * @param args - The arguments for generating the controller, including the optional controller name. * @param type - The type of the controller to generate, defined in the `ControllerType` enum. * @throws Will throw an error if the controller name is invalid or if the controller type is unsupported. */function generateController(a,b){var c=validateAndFormatName(a.controllerName),d=c.parts,e=c.kebabCaseName,f=c.className,g=path.join.apply(path,[process.cwd(),"src","controllers",b].concat(_toConsumableArray(d))),h=this.buildControllerTemplate(f,b);this.generateControllerStructure(g,e,f,b,h)}/** * Builds the controller template content by populating a template with variables. * @param className - The name of the controller class. * @param type - The type of the controller, defined in the `ControllerType` enum. * @returns The populated template string for the controller. * @throws Will throw an error if the controller type is unsupported. */},{key:"buildControllerTemplate",value:function buildControllerTemplate(a,b){var c=this.getTemplateConfig(b,a);if(!c)throw new Error("Unsupported controller type: ".concat(b));return populateTemplate(c.template,c.variables)}/** * Retrieves the template configuration for a specific controller type and class name. * @param type - The type of the controller, defined in the `ControllerType` enum. * @param className - The name of the controller class. * @returns An object containing the template path and variables, or `undefined` if not found. */},{key:"getTemplateConfig",value:function getTemplateConfig(a,b){var c=path.resolve(__dirname,"..","builder-template","controller"),d=_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({},ControllerType.BUTTON,"button.controller.template"),ControllerType.MODAL_SUBMIT,"modal-submit.controller.template"),ControllerType.SELECT_MENU,"select-menu.controller.template"),ControllerType.REACTION,"reaction.controller.template"),ControllerType.MESSAGE,"message.controller.template"),ControllerType.CONTEXT_MENU,"context-menu.controller.template"),ControllerType.SLASH,"slash.controller.template"),e=d[a]?path.resolve(c,d[a]):void 0;return e?{template:e,variables:{className:b}}:void 0}/** * Generates the controller file and its associated structure (e.g., builder files, directories). * @param controllerDir - The absolute path to the controller directory. * @param kebabCaseName - The kebab-case name of the controller file. * @param className - The name of the controller class. * @param type - The type of the controller, defined in the `ControllerType` enum. * @param controllerTemplate - The populated template string for the controller file. */},{key:"generateControllerStructure",value:function generateControllerStructure(a,b,c,d,e){this.generateBuilderFile(c,d,a),createDirectoryIfNotExists(a);var f=path.join(a,"".concat(b,".").concat(d,".controller.ts"));generateFile(f,e)}/** * Generates a builder file for the specified controller type and stores it in the controller directory. * @param className - The name of the controller class. * @param type - The type of the controller, defined in the `ControllerType` enum. * @param controllerDir - The absolute path to the controller directory. */},{key:"generateBuilderFile",value:function generateBuilderFile(a,b,c){var d=this.getBuilderConfig(b,a);if(d){var e=populateTemplate(d.template,d.variables),f=path.join(c,"builders");createDirectoryIfNotExists(f);var g=path.join(f,"sample.builder.ts");generateFile(g,e)}}/** * Retrieves the configuration for generating a builder file based on the controller type and class name. * @param type - The type of the controller, defined in the `ControllerType` enum. * @param className - The name of the controller class. * @returns An object containing the builder template path and variables, or `undefined` if not found. */},{key:"getBuilderConfig",value:function getBuilderConfig(a,b){var c=path.resolve(__dirname,"..","builder-template","builder"),d=_defineProperty(_defineProperty({},ControllerType.CONTEXT_MENU,"context-menu.builder.template"),ControllerType.SLASH,"slash.builder.template"),e=d[a]?path.resolve(c,d[a]):void 0;return e?{template:e,variables:{className:b}}:void 0}}])}();