import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; import { ModelRelation, ClassRelationEnum, Model } from '@vmfvmf/ywtc-lib'; import { error } from "console"; import { classify, dasherize } from '@angular-devkit/core/src/utils/strings'; import { getPrjSrd } from '../../router.gen'; import { P2 } from '../../router.map'; import { UcType } from '@vmfvmf/ywtc-lib/dist/model.enums'; export function gen(model: Model): Rule { return (tree: Tree, _context: SchematicContext) => { if (model.ucType == UcType.CHILD_FORM) { return; } const globalE2eSpecPath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/cypress/e2e/global.e2e-spec.cy.ts`; const updatedGlobalE2eSpec = updateGlobalE2eSpec(getFileContent(tree, globalE2eSpecPath)!, model); tree.overwrite(globalE2eSpecPath, updatedGlobalE2eSpec); 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 updateGlobalE2eSpec(content: string, model: Model) { const jtcAdd = '/*JTC*/'; const jtcImports = '/*JTCIMPORTS*/'; if (content.includes(jtcAdd) && content.includes(jtcImports)) { let nameDashed = getPrjSrd().mdlNDashd; let ucNo = model.ucNo!; let imports = new Array(); let newSts = new Array(); if (model.ucType != UcType.CHILD_TABLE) { imports.push( `import { ST${ucNo}01 } from "cypress/src/uc-${ucNo}-manage-${nameDashed}/01-search-${nameDashed}/tests/st-${ucNo}-01-search-${nameDashed}";`, `import { ST${ucNo}02 } from "cypress/src/uc-${ucNo}-manage-${nameDashed}/02-update-${nameDashed}/tests/st-${ucNo}-02-update-${nameDashed}";` ); newSts = [`${P2}new ST${ucNo}01(),`, `${P2}new ST${ucNo}02(),`]; } model.relations .filter((rel: ModelRelation) => [ClassRelationEnum.OneToMany].includes(rel.relation)) .forEach((rel: ModelRelation) => { let relNameDashed = dasherize(rel.toModelName); let relNameClassed = classify(relNameDashed); imports.push(`import { ST${ucNo}02${relNameClassed} } from "cypress/src/uc-${ucNo}-manage-${nameDashed}/02-update-${nameDashed}/tests/st-${ucNo}-02-update-${relNameDashed}";`); newSts.push(`\tnew ST${ucNo}02${relNameClassed}(),`); }); content = content.replace(jtcImports, `${imports.join('\n')}\n${jtcImports}`); content = content.replace(jtcAdd, `${newSts.join('\n')}\n${jtcAdd}`); } else { throw error(`The global.e2e.-pec.cy.ts file is missing ${jtcAdd} or ${jtcImports}!!!`); } return content; }