From f01c0ceb32c56586105775ee42e9f17acda531f2 Mon Sep 17 00:00:00 2001 From: Danilo Hoffmann Date: Mon, 17 Aug 2020 11:35:21 +0200 Subject: [PATCH] fix(schematics): disable subpaging for page schematic if parent page cannot be found --- schematics/src/page/factory.ts | 16 ++++++++++++---- schematics/src/page/factory_spec.ts | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/schematics/src/page/factory.ts b/schematics/src/page/factory.ts index b240a67227..085566d1b2 100644 --- a/schematics/src/page/factory.ts +++ b/schematics/src/page/factory.ts @@ -29,7 +29,7 @@ function addRouteToArray( insertComma: boolean ) { const dasherizedName = strings.dasherize(options.name); - const path = options.child ? options.child : dasherizedName.replace(/-/, '/'); + const path = options.child ? options.child : options.lazy ? dasherizedName : dasherizedName.replace(/-/g, '/'); if (options.lazy) { const loadChildren = `() => import('${ options.child ? '..' : '.' @@ -57,16 +57,24 @@ async function determineRoutingModule( let routingModuleLocation: string; let child: string; + let subPaging: boolean; const match = options.name.match(/(.*)\-([a-z0-9]+)/); if (options.lazy && match && match[1] && match[2]) { const parent = match[1]; - child = match[2]; - console.log(`detected subpage, will insert '${child}' as sub page of '${parent}'`); + const possibleChild = match[2]; routingModuleLocation = options.extension ? `extensions/${options.extension}/pages/${parent}/${parent}-page.module.ts` : `pages/${parent}/${parent}-page.module.ts`; - } else { + + subPaging = host.exists(`${project.sourceRoot}/app/${routingModuleLocation}`); + if (subPaging) { + child = possibleChild; + console.log(`detected subpage, will insert '${child}' as sub page of '${parent}'`); + } + } + + if (!subPaging) { routingModuleLocation = options.extension ? `extensions/${options.extension}/pages/${options.extension}-routing.module.ts` : (project.root ? 'pages/' + project.root.replace(/^.*?\//g, '') : 'pages/app') + '-routing.module.ts'; diff --git a/schematics/src/page/factory_spec.ts b/schematics/src/page/factory_spec.ts index 34ede5a0ed..8f98431625 100644 --- a/schematics/src/page/factory_spec.ts +++ b/schematics/src/page/factory_spec.ts @@ -167,15 +167,25 @@ describe('Page Schematic', () => { .toPromise(); const appRoutingModule = tree.readContent('/src/app/pages/app-routing.module.ts'); expect(appRoutingModule).toContain(`path: 'foo'`); - expect(appRoutingModule).toContain('foo-page.module'); + expect(appRoutingModule).toContain("import('./foo/foo-page.module')"); expect(appRoutingModule).toContain('FooPageModule'); expect(appRoutingModule).not.toContain('FooBar'); const fooRoutingModule = tree.readContent('/src/app/pages/foo/foo-page.module.ts'); expect(fooRoutingModule).toContain(`path: 'bar'`); - expect(fooRoutingModule).toContain('foo-bar-page.module'); + expect(fooRoutingModule).toContain("import('../foo-bar/foo-bar-page.module')"); expect(fooRoutingModule).toContain('FooBarPageModule'); }); + + it('should not register route in not existing page routing module even when subpaging is detected', async () => { + const tree = await schematicRunner + .runSchematicAsync('page', { ...defaultOptions, name: 'foo-bar' }, appTree) + .toPromise(); + const appRoutingModule = tree.readContent('/src/app/pages/app-routing.module.ts'); + expect(appRoutingModule).toContain(`path: 'foo-bar'`); + expect(appRoutingModule).toContain("import('./foo-bar/foo-bar-page.module')"); + expect(appRoutingModule).toContain('FooBarPageModule'); + }); }); describe('with lazy === false', () => {