Skip to content

Commit 7918b3c

Browse files
committed
fix(routing): Add logic to add missing import statements to the routing file
1 parent a745285 commit 7918b3c

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

packages/devextreme-schematics/src/add-layout/index.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ import { Change } from '@schematics/angular/utility/change';
6767

6868
import { PatchNodePackageInstallTask } from '../utility/patch';
6969

70+
const routes = [
71+
{
72+
name: 'LoginFormComponent',
73+
path: 'login-form'
74+
},
75+
{
76+
name: 'ResetPasswordFormComponent',
77+
path: 'reset-password'
78+
},
79+
{
80+
name: 'CreateAccountFormComponent',
81+
path: 'create-account'
82+
},
83+
{
84+
name: 'ChangePasswordFormComponent',
85+
path: 'change-password'
86+
}
87+
];
88+
7089
const projectFilesSource = './files/src';
7190
const workspaceFilesSource = './files';
7291

@@ -291,15 +310,23 @@ function updateDevextremeConfig(sourcePath: string = '') {
291310
const modifyRouting = (host: Tree, routingFilePath: string) => {
292311
// TODO: Try to use the isolated host to generate the result string
293312
let source = getSourceFile(host, routingFilePath)!;
294-
const importChange = insertImport(source, routingFilePath, 'LoginFormComponent', './shared/components');
295-
applyChanges(host, [ importChange ], routingFilePath);
296-
297-
source = getSourceFile(host, routingFilePath)!;
298-
const routes = findRoutesInSource(source)!;
299-
if (!hasComponentInRoutes(routes, 'login-form')) {
300-
const loginFormRoute = getRoute('login-form');
301-
insertItemToArray(host, routingFilePath, routes, loginFormRoute);
313+
const importChanges = [];
314+
importChanges.push(insertImport(source, routingFilePath, 'AuthGuardService', './shared/services'));
315+
316+
for (const route of routes) {
317+
importChanges.push(insertImport(source, routingFilePath, route.name, './shared/components'));
302318
}
319+
320+
applyChanges(host, importChanges, routingFilePath);
321+
for (const route of routes) {
322+
source = getSourceFile(host, routingFilePath)!;
323+
const routeInSource = findRoutesInSource(source)!;
324+
if (!hasComponentInRoutes(routeInSource, route.path)) {
325+
const routeToAdd = getRoute(route.path, route.name);
326+
insertItemToArray(host, routingFilePath, routeInSource, routeToAdd);
327+
}
328+
}
329+
303330
};
304331

305332
export default function(options: any): Rule {

packages/devextreme-schematics/src/utility/routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export function hasComponentInRoutes(routes: Node, name: string) {
2020
return routesText.indexOf(componentName) !== -1;
2121
}
2222

23-
export function getRoute(name: string) {
23+
export function getRoute(name: string, componentName?: string) {
2424
return ` {
2525
path: '${strings.dasherize(name)}',
26-
component: ${getRouteComponentName(name)},
26+
component: ${componentName || getRouteComponentName(name)},
2727
canActivate: [ AuthGuardService ]
2828
}`;
2929
}

0 commit comments

Comments
 (0)