import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; import { dasherize, classify } from '@angular-devkit/core/src/utils/strings'; import { getPrjSrd } from '../../router.gen'; import { P3 } from '../../router.map'; export enum ComponentType { LIST, EDITION_PAGE, EDITION_DIALOG } const componentTypeMap = new Map([ [ComponentType.EDITION_PAGE, '/create'], [ComponentType.LIST, ''] ]); export function gen({componentName, componentType, url}: {componentName: string, componentType: ComponentType, url: string}): Rule { return (tree: Tree, _context: SchematicContext) => { // MODULE let frontendModulePath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/src/app/${getPrjSrd().prjNDashd}-frontend/${getPrjSrd().prjNDashd}-frontend.module.ts`; let updatedfrontendModule = updatefrontendModule(getFileContent(tree, frontendModulePath)!, componentName); tree.overwrite(frontendModulePath, updatedfrontendModule); if (componentType !== ComponentType.EDITION_DIALOG) { // ROUTING MODULE frontendModulePath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/src/app/${getPrjSrd().prjNDashd}-frontend/${getPrjSrd().prjNDashd}-frontend-routing.module.ts`; updatedfrontendModule = updatefrontendRoutingModule(getFileContent(tree, frontendModulePath)!, componentName, componentType, url); tree.overwrite(frontendModulePath, updatedfrontendModule); } // ENVIRONMENT PROD let environmentPath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/src/environments/environment.prod.ts`; let updatedEnvironment = updateEnvironment(getFileContent(tree, environmentPath)!); tree.overwrite(environmentPath, updatedEnvironment); // ENVIRONMENTS DEV environmentPath = `ywtc-output/${getPrjSrd().prjNDashd}-frontend/src/environments/environment.ts`; updatedEnvironment = updateEnvironment(getFileContent(tree, environmentPath)!); tree.overwrite(environmentPath, updatedEnvironment); 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 updatefrontendModule(content: string, componentName: string) { content = content.replace('/*jtcImport*/', getComponentImport(componentName)); content = content.replace('/*jtcDeclaration*/', getComponentDeclaration(componentName)); return content; } function updatefrontendRoutingModule(content: string, componentName: string, componentType: ComponentType, url: string) { content = content.replace('/*jtcImport*/', getComponentImport(componentName)); content = content.replace('/*jtcDeclaration*/', getComponentRoutingDeclaration(componentName, componentType, url)); return content; } function getComponentImport(componentDeclaration: string) { return `import { ${classify(componentDeclaration)} } from './${getPrjSrd().mdlNDashd}/${dasherize(componentDeclaration).replace('-component', '.component')}';\n/*jtcImport*/`; } function getComponentDeclaration(componentDeclaration: string) { return `,\n${P3}${componentDeclaration}/*jtcDeclaration*/`; } function getComponentRoutingDeclaration(componentDeclaration: string, componentType: ComponentType, url: string) { let dec = `${P3}{ path: '${url + componentTypeMap.get(componentType)}', component: ${componentDeclaration} },\n`; if (componentType == ComponentType.EDITION_PAGE) { dec += `${P3}{ path: '${url}/:id', component: ${componentDeclaration} },\n`; } return `${dec}/*jtcDeclaration*/`; } function updateEnvironment(content: string) { if (content.includes(`${getPrjSrd().mdlNCamld}: '${getPrjSrd().mdlNDashd}`)) { return content; } content = content.replace('/*jtc*/', ` \t\t${getPrjSrd().mdlNCamld}: '${getPrjSrd().mdlNDashd}',/*jtc*/`); return content; }