import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; import { Model } from '@vmfvmf/ywtc-lib'; import { error } from "console"; import { getPrjSrd } from '../../router.gen'; import { P4 } from '../../router.map'; const file = 'SwaggerConfigurations.java'; export function gen(model: Model): Rule { return (tree: Tree, _context: SchematicContext) => { const swaggerConfigPath = `ywtc-output//${getPrjSrd().prjNDashd}-backend/src/main/java/com/vmfvmf/${getPrjSrd().artifactId}/config/swagger/${file}`; const updatedSwaggerConfigPath = updateSwaggerConfigPath(getFileContent(tree, swaggerConfigPath)!, model); tree.overwrite(swaggerConfigPath, updatedSwaggerConfigPath); return tree; }; } function getFileContent(tree: Tree, fileName: string) { if (!tree.exists(fileName)) { throw new Error(`File: ${fileName} NOT FOUND.`) } return tree.read(fileName)?.toString(); } function updateSwaggerConfigPath(content: string, model: Model) { const modelName = model.name.toUpperCase(); const jtcAdd = '/*JTC*/'; const jtcApend = ')/*JTC*/'; if (content.includes(jtcApend) ) { content = content.replace(jtcApend, `),\n${P4}UrlList.wChilds(UrlList.${modelName})${jtcAdd}`); } else if (content.includes(jtcAdd) ) { content = content.replace(jtcAdd, `${P4}UrlList.wChilds(UrlList.${modelName})${jtcAdd}`); } else { throw error(`The ${file} file is missing ${jtcAdd}!!!`); } return content; }