UNPKG

@avleon/cli

Version:
117 lines (115 loc) 4.57 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createController = createController; const chalk_1 = __importDefault(require("chalk")); const fs_1 = require("fs"); const promises_1 = require("fs/promises"); const path_1 = __importDefault(require("path")); const prettier_1 = __importDefault(require("prettier")); async function createController(name, options = { force: false, resource: false, model: null }, p) { if (!name) { throw new Error("Controller name not found. use --name or node artisan make:controller HomeController"); } const controllerPath = path_1.default.join(process.cwd(), "./src/controllers"); const controllerFolderExists = (0, fs_1.existsSync)(controllerPath); if (!controllerFolderExists) { await (0, promises_1.mkdir)(controllerPath, { recursive: true, }); } const controllerName = name.at(0).toUpperCase() + name.slice(1); const controllerNameLowerCase = name.replace(/Controller/ig, '').match(/[A-Z][a-z]+|[0-9]+/g).join("-").toLowerCase(); const controllerNameCap = controllerName.replace(/Controller/ig, ''); const controllerExists = (0, fs_1.existsSync)(`${controllerPath}/${name.toLowerCase()}.controller.ts`); if (controllerExists && !options.force) { console.log(chalk_1.default.bold(chalk_1.default.red("controller already exists!")), chalk_1.default.greenBright("Use -f or --force to overwrite.\r")); return; } if (options.model) { const file = await Promise.resolve(`${path_1.default.join(process.cwd(), "./src/models/" + options.model.toUpperCase() + ".ts")}`).then(s => __importStar(require(s))); if (file) { Object.getOwnPropertyNames(file); } } const restController = ` import { ApiController, Get, Post, Put, Delete, Param, Body } from '@avleon/core'; \t @ApiController("/${controllerNameLowerCase}") export class ${controllerName}{\n @Get() async findAll(){ return "okay"; }\n @Get('/:id') async findOne(@Param('id') id:number){ return "okay"; }\n @Post() async create(@Body() body:any){ return true; }\n @Put('/:id') async update(@Param('id') id:number, @Body() body:any){ return true; }\n @Delete('/:id') async delete(@Param('id') id:number){ return true; } } `; const basicContorller = `import {ApiController, Get} from '@avleon/core' \n @ApiController("/${controllerNameLowerCase}") export class ${controllerName}{ \n @Get() index(){ return [] } } `; const formatcontroller = await prettier_1.default.format(options.resource ? restController : basicContorller, { singleQuote: true, singleAttributePerLine: true, parser: "typescript", }); await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), "./src/controllers/" + controllerName.toLowerCase().replace("controller", "") + ".controller." + ".ts"), formatcontroller); console.log("Controller created succssessfully!"); }