Skip to content

Commit 49c916e

Browse files
ajivanyandevVasilyStrelyaev
authored andcommitted
fix(routing): Add logic to add missing imports (#959)
* fix(routing): Add logic to add missing import statements to the routing file * fix(tests): Add tests to ensure correct imports in toutes file * fix(routing): Fix dasherize logic in getRooute and fix tests * fix(routing): Improve getRoute logic, add path optional param * fix: combine services and components into one array
1 parent fa5140a commit 49c916e

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

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

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ import { Change } from '@schematics/angular/utility/change';
6868
import { PatchNodePackageInstallTask } from '../utility/patch';
6969
import { isAngularVersionHigherThan } from '../utility/angular-version';
7070

71+
const routes = [
72+
{
73+
name: 'AuthGuardService',
74+
type: 'service',
75+
location: './shared/services'
76+
},
77+
{
78+
name: 'LoginFormComponent',
79+
path: 'login-form',
80+
type: 'component',
81+
location: './shared/components'
82+
},
83+
{
84+
name: 'ResetPasswordFormComponent',
85+
path: 'reset-password',
86+
type: 'component',
87+
location: './shared/components'
88+
},
89+
{
90+
name: 'CreateAccountFormComponent',
91+
path: 'create-account',
92+
type: 'component',
93+
location: './shared/components'
94+
},
95+
{
96+
name: 'ChangePasswordFormComponent',
97+
path: 'change-password/:recoveryCode',
98+
type: 'component',
99+
location: './shared/components'
100+
}
101+
];
102+
71103
const projectFilesSource = './files/src';
72104
const workspaceFilesSource = './files';
73105

@@ -295,15 +327,23 @@ function updateDevextremeConfig(sourcePath: string = '') {
295327
const modifyRouting = (host: Tree, routingFilePath: string) => {
296328
// TODO: Try to use the isolated host to generate the result string
297329
let source = getSourceFile(host, routingFilePath)!;
298-
const importChange = insertImport(source, routingFilePath, 'LoginFormComponent', './shared/components');
299-
applyChanges(host, [ importChange ], routingFilePath);
300-
301-
source = getSourceFile(host, routingFilePath)!;
302-
const routes = findRoutesInSource(source)!;
303-
if (!hasComponentInRoutes(routes, 'login-form')) {
304-
const loginFormRoute = getRoute('login-form');
305-
insertItemToArray(host, routingFilePath, routes, loginFormRoute);
330+
const importChanges = [];
331+
for (const route of routes) {
332+
importChanges.push(insertImport(source, routingFilePath, route.name, route.location));
306333
}
334+
335+
applyChanges(host, importChanges, routingFilePath);
336+
for (const route of routes) {
337+
if (route.type === 'component' && route.path) {
338+
source = getSourceFile(host, routingFilePath)!;
339+
const routeInSource = findRoutesInSource(source)!;
340+
if (!hasComponentInRoutes(routeInSource, route.path)) {
341+
const routeToAdd = getRoute(route.name, route.name, route.path);
342+
insertItemToArray(host, routingFilePath, routeInSource, routeToAdd);
343+
}
344+
}
345+
}
346+
307347
};
308348

309349
function setPostfix(host: Tree, name: string) {

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,25 @@ describe('layout', () => {
243243
const routesContent = tree.readContent('/src/app/app.routes.ts');
244244

245245
expect(routesContent)
246-
.toContain(`{\n path: 'login-form',\n component: LoginFormComponent,\n canActivate: [ AuthGuardService ]\n },`);
247-
});
246+
.toContain(`import { AuthGuardService } from './shared/services';`);
247+
const loginFormComponentMatch = routesContent.match(/\bLoginFormComponent\b/g);
248+
const resetPasswordFormComponentMatch = routesContent.match(/\bResetPasswordFormComponent\b/g);
249+
const createAccountFormComponentMatch = routesContent.match(/\bCreateAccountFormComponent\b/g);
250+
const changePasswordFormComponentMatch = routesContent.match(/\bChangePasswordFormComponent\b/g);
251+
expect(loginFormComponentMatch?.length).toBe(2);
252+
expect(resetPasswordFormComponentMatch?.length).toBe(2);
253+
expect(createAccountFormComponentMatch?.length).toBe(2);
254+
expect(changePasswordFormComponentMatch?.length).toBe(2);
248255

256+
expect(routesContent)
257+
.toContain(`path: 'login-form'`);
258+
expect(routesContent)
259+
.toContain(`path: 'reset-password'`);
260+
expect(routesContent)
261+
.toContain(`path: 'create-account'`);
262+
expect(routesContent)
263+
.toContain(`path: 'change-password/:recoveryCode'`);
264+
});
249265
it('should use selected layout', async () => {
250266
const runner = new SchematicTestRunner('schematics', collectionPath);
251267
options.layout = 'side-nav-inner-toolbar';

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

Lines changed: 3 additions & 3 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, componentPath?: string) {
2424
return ` {
25-
path: '${strings.dasherize(name)}',
26-
component: ${getRouteComponentName(name)},
25+
path: '${componentPath || strings.dasherize(name)}',
26+
component: ${componentName || getRouteComponentName(name)},
2727
canActivate: [ AuthGuardService ]
2828
}`;
2929
}

0 commit comments

Comments
 (0)