UNPKG

@tsclean/scaffold

Version:

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

112 lines (106 loc) 4.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InterfaceCreateCommand = 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 InterfaceCreateCommand { constructor() { this.command = "create:interface"; this.describe = "Generate a new interface"; } builder(args) { return args .option("n", { alias: "name", describe: "Name the Interface", demandOption: true }) .option("p", { alias: "path", describe: "File location", demandOption: true }); } ; async handler(args) { let spinner; let basePath; let fileName; let path; let fileExists; try { const fileContent = InterfaceCreateCommand.getTemplateInterface(args.name, args.path); setTimeout(() => (spinner = (0, ora_1.default)('Installing...').start()), 1000); if (args.path === "entities" || args.path === "service" || args.path === "infra") { switch (args.path) { case "entities": basePath = `${process.cwd()}/src/domain/entities/contracts`; fileName = `${args.name}-repository.ts`; break; case "service": basePath = `${process.cwd()}/src/domain/use-cases`; fileName = `${args.name}-service.ts`; break; case "infra": basePath = `${process.cwd()}/src/infrastructure/entry-points/contracts`; fileName = `${args.name}.ts`; break; } path = `${basePath}/${fileName}`; fileExists = await CommandUtils_1.CommandUtils.fileExists(path); 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('Interface', path) }); }, 1000 * 5); } else { throw messages_1.MESSAGES.ERROR_INTERFACE(args.path); } } catch (error) { setTimeout(() => (spinner.fail("Installation fail"), (0, helpers_1.errorMessage)(error, 'interface')), 2000); } } /** * Get contents interface files * @param param * @param path * @protected */ static getTemplateInterface(param, path) { const nameRef = param.toUpperCase().replace(/-/g, "_"); const string = param .split("-") .map((p) => p.charAt(0).toUpperCase() + p.slice(1)) .join(""); const repositoryConst = `export const ${nameRef}_REPOSITORY = '${nameRef}_REPOSITORY';`; switch (path) { case 'entities': return `${repositoryConst} export interface I${string}Repository { }`; case 'service': return `${repositoryConst} export interface I${string}Service { }`; case 'infra': return `${repositoryConst} export interface I${string} { }`; } } } exports.InterfaceCreateCommand = InterfaceCreateCommand; //# sourceMappingURL=CommandCreateInterface.js.map