diff --git a/packages/devextreme-schematics/src/add-layout/index.ts b/packages/devextreme-schematics/src/add-layout/index.ts index 56eb1e558..ccd378a34 100644 --- a/packages/devextreme-schematics/src/add-layout/index.ts +++ b/packages/devextreme-schematics/src/add-layout/index.ts @@ -67,6 +67,38 @@ import { Change } from '@schematics/angular/utility/change'; import { PatchNodePackageInstallTask } from '../utility/patch'; +const routes = [ + { + name: 'AuthGuardService', + type: 'service', + location: './shared/services' + }, + { + name: 'LoginFormComponent', + path: 'login-form', + type: 'component', + location: './shared/components' + }, + { + name: 'ResetPasswordFormComponent', + path: 'reset-password', + type: 'component', + location: './shared/components' + }, + { + name: 'CreateAccountFormComponent', + path: 'create-account', + type: 'component', + location: './shared/components' + }, + { + name: 'ChangePasswordFormComponent', + path: 'change-password/:recoveryCode', + type: 'component', + location: './shared/components' + } +]; + const projectFilesSource = './files/src'; const workspaceFilesSource = './files'; @@ -291,15 +323,23 @@ function updateDevextremeConfig(sourcePath: string = '') { const modifyRouting = (host: Tree, routingFilePath: string) => { // TODO: Try to use the isolated host to generate the result string let source = getSourceFile(host, routingFilePath)!; - const importChange = insertImport(source, routingFilePath, 'LoginFormComponent', './shared/components'); - applyChanges(host, [ importChange ], routingFilePath); - - source = getSourceFile(host, routingFilePath)!; - const routes = findRoutesInSource(source)!; - if (!hasComponentInRoutes(routes, 'login-form')) { - const loginFormRoute = getRoute('login-form'); - insertItemToArray(host, routingFilePath, routes, loginFormRoute); + const importChanges = []; + for (const route of routes) { + importChanges.push(insertImport(source, routingFilePath, route.name, route.location)); } + + applyChanges(host, importChanges, routingFilePath); + for (const route of routes) { + if (route.type === 'component' && route.path) { + source = getSourceFile(host, routingFilePath)!; + const routeInSource = findRoutesInSource(source)!; + if (!hasComponentInRoutes(routeInSource, route.path)) { + const routeToAdd = getRoute(route.name, route.name, route.path); + insertItemToArray(host, routingFilePath, routeInSource, routeToAdd); + } + } + } + }; export default function(options: any): Rule { diff --git a/packages/devextreme-schematics/src/add-layout/index_spec.ts b/packages/devextreme-schematics/src/add-layout/index_spec.ts index fcb8ad350..d22f07c93 100644 --- a/packages/devextreme-schematics/src/add-layout/index_spec.ts +++ b/packages/devextreme-schematics/src/add-layout/index_spec.ts @@ -243,9 +243,25 @@ describe('layout', () => { const routesContent = tree.readContent('/src/app/app.routes.ts'); expect(routesContent) - .toContain(`{\n path: 'login-form',\n component: LoginFormComponent,\n canActivate: [ AuthGuardService ]\n },`); - }); + .toContain(`import { AuthGuardService } from './shared/services';`); + const loginFormComponentMatch = routesContent.match(/\bLoginFormComponent\b/g); + const resetPasswordFormComponentMatch = routesContent.match(/\bResetPasswordFormComponent\b/g); + const createAccountFormComponentMatch = routesContent.match(/\bCreateAccountFormComponent\b/g); + const changePasswordFormComponentMatch = routesContent.match(/\bChangePasswordFormComponent\b/g); + expect(loginFormComponentMatch?.length).toBe(2); + expect(resetPasswordFormComponentMatch?.length).toBe(2); + expect(createAccountFormComponentMatch?.length).toBe(2); + expect(changePasswordFormComponentMatch?.length).toBe(2); + expect(routesContent) + .toContain(`path: 'login-form'`); + expect(routesContent) + .toContain(`path: 'reset-password'`); + expect(routesContent) + .toContain(`path: 'create-account'`); + expect(routesContent) + .toContain(`path: 'change-password/:recoveryCode'`); + }); it('should use selected layout', async () => { const runner = new SchematicTestRunner('schematics', collectionPath); options.layout = 'side-nav-inner-toolbar'; diff --git a/packages/devextreme-schematics/src/utility/routing.ts b/packages/devextreme-schematics/src/utility/routing.ts index 3e8542c43..153bd323a 100644 --- a/packages/devextreme-schematics/src/utility/routing.ts +++ b/packages/devextreme-schematics/src/utility/routing.ts @@ -20,10 +20,10 @@ export function hasComponentInRoutes(routes: Node, name: string) { return routesText.indexOf(componentName) !== -1; } -export function getRoute(name: string) { +export function getRoute(name: string, componentName?: string, componentPath?: string) { return ` { - path: '${strings.dasherize(name)}', - component: ${getRouteComponentName(name)}, + path: '${componentPath || strings.dasherize(name)}', + component: ${componentName || getRouteComponentName(name)}, canActivate: [ AuthGuardService ] }`; }