UNPKG

@expressots/cli

Version:

Expressots CLI - modern, fast, lightweight nodejs web framework (@cli)

47 lines (46 loc) 1.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addControllerToModule = void 0; const node_fs_1 = __importDefault(require("node:fs")); async function addControllerToModule(filePath, controllerName, controllerPath) { const fileContent = await node_fs_1.default.promises.readFile(filePath, "utf8"); const imports = []; const notImports = []; fileContent.split("\n").forEach((line) => { if (line.startsWith("import")) { imports.push(line); } else { notImports.push(line); } }); const newImport = `import { ${controllerName} } from "${controllerPath}";`; if (imports.includes(newImport)) { return; } imports.push(newImport); const moduleDeclarationRegex = /CreateModule\(\s*\[([\s\S]*?)]/; const moduleDeclarationMatch = fileContent.match(moduleDeclarationRegex); if (!moduleDeclarationMatch) { return; } const controllers = moduleDeclarationMatch[1] .trim() .split(",") .map((c) => c.trim()) .filter((c) => c); if (controllers.includes(controllerName)) { return; } controllers.push(controllerName); const newControllers = controllers.join(", "); const newModuleDeclaration = `CreateModule([${newControllers}]`; const newFileContent = [...imports, ...notImports] .join("\n") .replace(moduleDeclarationRegex, newModuleDeclaration); await node_fs_1.default.promises.writeFile(filePath, newFileContent, "utf8"); } exports.addControllerToModule = addControllerToModule;