UNPKG

@tsclean/scaffold

Version:

This CLI creates an initial structure of a project based on clean architecture.

95 lines (91 loc) 3.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ControllerCreateCommand = void 0; const ora_1 = __importDefault(require("ora")); const emojis_1 = require("../utils/emojis"); const messages_1 = require("../utils/messages"); const CommandUtils_1 = require("./CommandUtils"); const helpers_1 = require("../utils/helpers"); class ControllerCreateCommand { constructor() { this.command = "create:controller"; this.describe = "Generates a new controller."; } builder(args) { return args .option('n', { alias: "name", describe: "Name the Controller class", demandOption: true }); } async handler(args) { let spinner; try { const directoryImplementation = `${process.cwd()}/src/domain/use-cases/impl`; const files = await CommandUtils_1.CommandUtils.injectServiceAdapter(directoryImplementation); const fileContent = ControllerCreateCommand.getTemplateController(args.name, files); const basePath = `${process.cwd()}/src/infrastructure/entry-points/api/`; const filename = `${args.name}-controller.ts`; const path = `${basePath}${filename}`; const fileExists = await CommandUtils_1.CommandUtils.fileExists(path); setTimeout(() => (spinner = (0, ora_1.default)('Installing...').start()), 1000); if (fileExists) throw messages_1.MESSAGES.FILE_EXISTS(path); await CommandUtils_1.CommandUtils.createFile(path, fileContent); setTimeout(() => { spinner.succeed("Installation completed"); spinner.stopAndPersist({ symbol: emojis_1.EMOJIS.ROCKET, text: messages_1.MESSAGES.FILE_SUCCESS('Controller', path) }); }, 1000 * 5); } catch (error) { setTimeout(() => (spinner.fail("Installation fail"), (0, helpers_1.errorMessage)(error, 'controller')), 2000); } } /** * Get content controllers files * @param param * @param files * @protected */ static getTemplateController(param, files) { let nameService = ""; let nameCapitalize = CommandUtils_1.CommandUtils.capitalizeString(param); // This loop when the name of the controller matches the service, to then be injected through the constructor. for (const file of files) { let name = file.slice(0, -16); if (name === param) { nameService = name; const nameCapitalizeService = CommandUtils_1.CommandUtils.capitalizeString(nameService); return `import {Mapping, Get} from "@tsclean/core"; @Mapping('api/v1/${nameService}') export class ${nameCapitalizeService}Controller { constructor() { } // Example function @Get() async getWelcome(): Promise<any> { return 'Welcome to the world of clean architecture' } } `; } ; } return `import {Mapping} from "@tsclean/core"; @Mapping('') export class ${nameCapitalize}Controller { constructor() { } } `; } } exports.ControllerCreateCommand = ControllerCreateCommand; //# sourceMappingURL=CommandCreateController.js.map