Skip to content

Commit

Permalink
fix(schematics): disable subpaging for page schematic if parent page …
Browse files Browse the repository at this point in the history
…cannot be found
  • Loading branch information
dhhyi committed Aug 21, 2020
1 parent bcbd276 commit f01c0ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 12 additions & 4 deletions schematics/src/page/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '..' : '.'
Expand Down Expand Up @@ -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';
Expand Down
14 changes: 12 additions & 2 deletions schematics/src/page/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit f01c0ce

Please sign in to comment.