UNPKG

tamim-cli

Version:

A CLI tool for generating module boilerplate code including routes, controllers, services, and more

52 lines (51 loc) 2.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateRouterFile = updateRouterFile; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); function updateRouterFile(folderName, camelCaseName) { try { const routerPath = path_1.default.join(process.cwd(), "src", "routes", "index.ts"); const routeImport = `import { ${camelCaseName}Routes } from '../app/modules/${folderName}/${folderName}.route';`; const routeEntry = ` { path: '/${folderName}', route: ${camelCaseName}Routes, }`; let routerFileContent = fs_1.default.readFileSync(routerPath, "utf-8"); // Add import if not exists if (!routerFileContent.includes(routeImport)) { // Find the last import statement const lastImportIndex = routerFileContent.lastIndexOf("import"); const endOfLastImport = routerFileContent.indexOf(";", lastImportIndex) + 1; routerFileContent = routerFileContent.slice(0, endOfLastImport) + "\n" + routeImport + routerFileContent.slice(endOfLastImport); } // Find and update apiRoutes array const apiRoutesMatch = routerFileContent.match(/const apiRoutes\s*=\s*\[([\s\S]*?)\];/); if (apiRoutesMatch) { const currentRoutes = apiRoutesMatch[1].trim(); // Check if route already exists if (!currentRoutes.includes(`path: '/${folderName}'`)) { const updatedRoutes = currentRoutes ? `${currentRoutes}\n${routeEntry},` : routeEntry; routerFileContent = routerFileContent.replace(/const apiRoutes\s*=\s*\[([\s\S]*?)\];/, `const apiRoutes = [\n${updatedRoutes}\n];`); fs_1.default.writeFileSync(routerPath, routerFileContent); console.log(`Successfully updated routes for ${folderName}`); } } else { throw new Error("Could not find apiRoutes array in router file"); } } catch (error) { console.error("Error updating router file:", error); throw error; } }