Skip to content

Commit 37b8853

Browse files
refactor
1 parent b6bf65c commit 37b8853

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

packages/devextreme-schematics/src/add-layout/files/src/app/__name__.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common';
33
import { RouterModule, RouterOutlet } from '@angular/router';
44
import { AuthService, ScreenService, AppInfoService } from './shared/services';
55
import { DxHttpModule } from 'devextreme-angular/http';
6-
import { SideNavOuterToolbarComponent, SideNavInnerToolbarComponent } from './layouts';
76
import { FooterComponent } from './shared/components';
87
import { UnauthenticatedContentComponent } from './unauthenticated-content';
98

@@ -18,8 +17,7 @@ import { UnauthenticatedContentComponent } from './unauthenticated-content';
1817
RouterOutlet,
1918
CommonModule,
2019
DxHttpModule,
21-
SideNavOuterToolbarComponent,
22-
SideNavInnerToolbarComponent,
20+
SideNavToolbarComponent,
2321
FooterComponent,
2422
UnauthenticatedContentComponent,
2523
],

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ import {
6060
findRoutesInSource
6161
} from '../utility/routing';
6262

63-
import {
64-
addImportToModule, addProviderToModule, insertImport
65-
} from '@schematics/angular/utility/ast-utils';
63+
import { insertImport } from '@schematics/angular/utility/ast-utils';
6664

6765
import { getWorkspace } from '@schematics/angular/utility/workspace';
6866
import { Change } from '@schematics/angular/utility/change';
@@ -168,29 +166,25 @@ function modifyFileRule(path: string, callback: (source: SourceFile) => Change[]
168166
};
169167
}
170168

171-
function updateAppModule(host: Tree, sourcePath: string) {
172-
const appModulePath = sourcePath + 'app.component.ts';
169+
function updateAppComponent(sourcePath: string, templateOptions: any = {}) {
170+
const appMComponentPath = sourcePath + 'app.component.ts';
173171

174-
const importSetter = (importName: string, path: string) => {
172+
const importSetter = (importName: string, path: string, alias: string) => {
175173
return (source: SourceFile) => {
176-
return addImportToModule(source, appModulePath, importName, path);
174+
return [insertImport(source, appMComponentPath, importName, path, false, alias)];
177175
};
178176
};
179177

180-
/*const providerSetter = (importName: string, path: string) => {
181-
return (source: SourceFile) => {
182-
return addProviderToModule(source, appModulePath, importName, path);
183-
};
184-
};*/
185-
186178
const rules = [
187-
modifyFileRule(appModulePath, importSetter('DxHttpModule', 'devextreme-angular/http')),
179+
modifyFileRule(appMComponentPath, importSetter(
180+
templateOptions.layout === 'side-nav-outer-toolbar'
181+
? 'SideNavOuterToolbarComponent'
182+
: 'SideNavInnerToolbarComponent',
183+
'./layouts',
184+
'SideNavToolbarComponent',
185+
)),
188186
];
189187

190-
if (!hasRoutingModule(host, sourcePath)) {
191-
rules.push(modifyFileRule(appModulePath, importSetter('routes', './app.routes')));
192-
}
193-
194188
return chain(rules);
195189
}
196190

@@ -214,7 +208,7 @@ function getComponentName(host: Tree, sourcePath: string) {
214208
return name;
215209
}
216210

217-
function hasRoutingModule(host: Tree, sourcePath: string) {
211+
function hasRouting(host: Tree, sourcePath: string) {
218212
return host.exists(sourcePath + 'app.routes.ts');
219213
}
220214

@@ -294,18 +288,17 @@ function updateDevextremeConfig(sourcePath: string = '') {
294288
return modifyContentByTemplate('./', workspaceFilesSource, devextremeConfigPath, templateOptions, modifyConfig);
295289
}
296290

297-
const modifyRoutingModule = (host: Tree, routingModulePath: string) => {
291+
const modifyRouting = (host: Tree, routingFilePath: string) => {
298292
// TODO: Try to use the isolated host to generate the result string
299-
let source = getSourceFile(host, routingModulePath)!;
300-
const importChange = insertImport(source, routingModulePath, 'LoginFormComponent', './shared/components');
301-
const providerChanges = addProviderToModule(source, routingModulePath, 'AuthGuardService', './shared/services');
302-
applyChanges(host, [ importChange, ...providerChanges], routingModulePath);
293+
let source = getSourceFile(host, routingFilePath)!;
294+
const importChange = insertImport(source, routingFilePath, 'LoginFormComponent', './shared/components');
295+
applyChanges(host, [ importChange ], routingFilePath);
303296

304-
source = getSourceFile(host, routingModulePath)!;
297+
source = getSourceFile(host, routingFilePath)!;
305298
const routes = findRoutesInSource(source)!;
306299
if (!hasComponentInRoutes(routes, 'login-form')) {
307300
const loginFormRoute = getRoute('login-form');
308-
insertItemToArray(host, routingModulePath, routes, loginFormRoute);
301+
insertItemToArray(host, routingFilePath, routes, loginFormRoute);
309302
}
310303
};
311304

@@ -336,8 +329,8 @@ export default function(options: any): Rule {
336329
return `${currentContent}\n${templateContent}`;
337330
}
338331

339-
if (basename(filePath) === 'app.routes.ts' && hasRoutingModule(host, appPath)) {
340-
modifyRoutingModule(host, filePath);
332+
if (basename(filePath) === 'app.routes.ts' && hasRouting(host, appPath)) {
333+
modifyRouting(host, filePath);
341334
return currentContent;
342335
}
343336

@@ -347,7 +340,7 @@ export default function(options: any): Rule {
347340
const rules = [
348341
modifyContentByTemplate(sourcePath, projectFilesSource, null, templateOptions, modifyContent),
349342
updateDevextremeConfig(sourcePath),
350-
updateAppModule(host, appPath),
343+
updateAppComponent(appPath, templateOptions),
351344
addBuildThemeScript(),
352345
() => addCustomThemeStyles(host, options, sourcePath) as any,
353346
addViewportToBody(sourcePath),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('layout', () => {
104104
const appContent = tree.readContent('/src/app/app.component.ts');
105105
expect(appContent).toContain('import { DxHttpModule }');
106106
expect(appContent)
107-
.toContain('import { SideNavOuterToolbarComponent, SideNavInnerToolbarComponent }');
107+
.toContain('import { SideNavOuterToolbarComponent as SideNavToolbarComponent }');
108108
expect(appContent)
109109
.toContain(`import { AuthService, ScreenService, AppInfoService } from './shared/services';`);
110110
expect(appContent)

0 commit comments

Comments
 (0)