UNPKG

tspace-spear

Version:

tspace-spear is a lightweight, high-performance API framework for Node.js that leverages the native HTTP server and supports uWebSockets.js (C++) for maximum speed and efficiency.

173 lines (149 loc) • 4.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createModule = createModule; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const shared_1 = require("../shared"); function createModule(root, name) { if (!name) { console.log("Missing module path, try spear g module dogs"); process.exit(1); } const modulePath = path_1.default.resolve(process.cwd(), root, 'modules', (0, shared_1.toPlural)(name)); fs_1.default.mkdirSync(modulePath, { recursive: true }); const controllerPath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.controller.ts`); const servicePath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.service.ts`); const dtoPath = path_1.default.resolve(process.cwd(), root, "modules", (0, shared_1.toPlural)(name), `${(0, shared_1.toSingular)(name)}.dto.ts`); const controllerName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Controller"; const serviceName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Service"; const dtoName = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Dto"; fs_1.default.writeFileSync(controllerPath, `import { type T, Controller, Get, Post, Put, Patch, Delete, ValidateDto } from "tspace-spear"; import { ${serviceName} } from "./${(0, shared_1.toSingular)(name)}.service"; import { Create${dtoName}, Update${dtoName} } from "./${(0, shared_1.toSingular)(name)}.dto"; @Controller("/${name}") class ${controllerName} { constructor( private ${(0, shared_1.toSingular)(name)}Service: ${serviceName} = new ${serviceName}() ) {} @Get("/") async index() { return this.${(0, shared_1.toSingular)(name)}Service.index(); } @Get("/:id") async show({ params }: T.Context<{ params: { id: number } }>) { return this.${(0, shared_1.toSingular)(name)}Service.show(+params.id); } @Post("/") @ValidateDto(Create${dtoName}) async create({ body }: T.Context<{ body: Create${dtoName} }>) { return this.${(0, shared_1.toSingular)(name)}Service .create({ name: body.name, age: body.age }); } @Put("/:id") @Patch("/:id") @ValidateDto(Update${dtoName}) async update({ params, body }: T.Context<{ params: { id: number }; body: Update${dtoName}; }>) { return this.${(0, shared_1.toSingular)(name)}Service .update(+params.id, { name: body.name, age: body.age }); } @Delete("/:id") async remove({ params }: T.Context<{ params: { id: number } }>) { return this.${(0, shared_1.toSingular)(name)}Service .remove(+params.id); } } export { ${controllerName} }; export default ${controllerName}; `); fs_1.default.writeFileSync(servicePath, `import { Create${dtoName}, Update${dtoName} } from "./${(0, shared_1.toSingular)(name)}.dto"; class ${serviceName} { public async index() { return []; }; public async show(id: number) { return {}; }; public async create(body : Create${dtoName}) { return {}; } public async update(id: number, body: Update${dtoName}) { return {}; } public async remove(id: number) { return {}; } } export { ${serviceName} }; export default ${serviceName}; `); fs_1.default.writeFileSync(dtoPath, `import { IsString, Min, IsNotEmpty, IsNumber, } from "class-validator"; export class Create${dtoName} { @IsString() @IsNotEmpty() name!: string; @IsNumber() @Min(0.1) age!: number; } export class Update${dtoName} { @IsString() @IsNotEmpty() name!: string; @IsNumber() @Min(0.1) age!: number; } `); console.log(` CREATE ${modulePath} CREATE ${controllerPath} CREATE ${servicePath} CREATE ${dtoPath} āœ” Successfully generated module "${name}" Controller ${controllerName} Service ${serviceName} DTO ${dtoName} `); } //# sourceMappingURL=index.js.map