import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; import { Model, ClassRelationEnum, ModelRelation } from '@vmfvmf/ywtc-lib'; import { error } from "console"; import { dasherize } from '@angular-devkit/core/src/utils/strings'; import { getPrjSrd } from '../../router.gen'; import { P3 } from '../../router.map'; export function gen(modelUcf: Model): Rule { return (tree: Tree, _context: SchematicContext) => { const appTestSuitPath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/cypress/src/${getPrjSrd().prjNDashd}-test-suit.ts`; const updatedAppTestSuite = updateAppTestSuite(getFileContent(tree, appTestSuitPath)!, modelUcf); tree.overwrite(appTestSuitPath, updatedAppTestSuite); 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 updateAppTestSuite(content: string, modelUcf: Model) { const jtcAdd = '/*JTC*/'; const jtcRel = '/*JTCREL*/'; if (content.includes(jtcAdd) && content.includes(jtcRel)) { let tables = [`${P3}'${getPrjSrd().dbName}.${dasherize(modelUcf.name) }'`]; let tablesRel = new Array(); // modelUcf.relations // ?.filter((rel: ModelRelation) => rel.childRelationId && [ClassRelationEnum.OneToMany].includes(rel.relation)) // .forEach((rel: ModelRelation) => tables.push(`${P3}'${getPrjSrd().dbName}.${dasherize(rel.toModelName)}'`)); modelUcf.relations ?.filter((rel: ModelRelation) => [ClassRelationEnum.ManyToMany].includes(rel.relation)) .forEach((rel: ModelRelation) => tablesRel.push(`${P3}'${getPrjSrd().dbName}.rel_${dasherize(modelUcf.name)}_${dasherize(rel.toModelName)}',`)); content = content.replace(jtcAdd, `${tables.join(',\n')},\n${jtcAdd}`); content = content.replace(jtcRel, `${tablesRel.join('\n')}\n${jtcRel}`); } else { throw error(`The ${getPrjSrd().prjNDashd}-test-suite.ts file is missing ${jtcAdd} or ${jtcRel}!!!`); } return content; }