Skip to content

Commit

Permalink
feat(schematics): add lazy option to page schematic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed May 18, 2020
1 parent bd92e7e commit d07a378
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 178 deletions.
79 changes: 51 additions & 28 deletions schematics/src/page/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,47 @@ import * as ts from 'typescript';
import { applyNameAndPath, detectExtension, determineArtifactName } from '../utils/common';
import { readIntoSourceFile } from '../utils/filesystem';
import { applyLintFix } from '../utils/lint-fix';
import { addImportToFile } from '../utils/registration';

import { PWAPageOptionsSchema as Options } from './schema';

function addRouteToArray(
options: { name?: string; routingModule?: string; child?: string },
options: { name?: string; routingModule?: string; child?: string; lazy?: boolean },
host: Tree,
position: number,
insertComma: boolean
) {
const dasherizedName = strings.dasherize(options.name);
const path = options.child ? options.child : dasherizedName.replace(/-/, '/');
if (options.lazy) {
const loadChildren = `() => import('${
options.child ? '..' : '.'
}/${dasherizedName}/${dasherizedName}-page.module').then(m => m.${strings.classify(dasherizedName)}PageModule)`;

const loadChildren = `() => import('${
options.child ? '..' : '.'
}/${dasherizedName}/${dasherizedName}-page.module').then(m => m.${strings.classify(dasherizedName)}PageModule)`;
const path = options.child ? options.child : dasherizedName;

const recorder = host.beginUpdate(options.routingModule);
recorder.insertRight(position, `${insertComma ? ', ' : ''}{ path: '${path}', loadChildren: ${loadChildren} }`);
host.commitUpdate(recorder);
const recorder = host.beginUpdate(options.routingModule);
recorder.insertRight(position, `${insertComma ? ', ' : ''}{ path: '${path}', loadChildren: ${loadChildren} }`);
host.commitUpdate(recorder);
} else {
const recorder = host.beginUpdate(options.routingModule);
recorder.insertRight(
position,
`${insertComma ? ', ' : ''}{ path: '${path}', component: ${strings.classify(options.name)}PageComponent }`
);
host.commitUpdate(recorder);
}
}

function determineRoutingModule(host: Tree, options: { name?: string; project?: string; extension?: string }) {
function determineRoutingModule(
host: Tree,
options: { name?: string; project?: string; extension?: string; lazy?: boolean }
) {
const project = getProject(host, options.project);

let routingModuleLocation: string;
let child: string;

const match = options.name.match(/(.*)\-([a-z0-9]+)/);
if (match && match[1] && match[2]) {
if (options.lazy && match && match[1] && match[2]) {
const parent = match[1];
child = match[2];
// tslint:disable-next-line:no-console
Expand All @@ -68,10 +80,14 @@ function determineRoutingModule(host: Tree, options: { name?: string; project?:
};
}

export function addRouteToRoutingModule(options: { extension?: string; project?: string; name?: string }): Rule {
export function addRouteToRoutingModule(options: {
extension?: string;
project?: string;
name?: string;
routingModule?: string;
}): Rule {
return host => {
const newOptions = determineRoutingModule(host, options);
const source = readIntoSourceFile(host, newOptions.routingModule);
const source = readIntoSourceFile(host, options.routingModule);
forEachToken(source, node => {
if (node.kind === ts.SyntaxKind.Identifier && /^[a-zA-Z0-9]*(R|r)outes$/.test(node.getText())) {
const parent = node.parent;
Expand All @@ -82,9 +98,9 @@ export function addRouteToRoutingModule(options: { extension?: string; project?:
if (routes.getChildCount() > 0) {
const lastChild = routes.getChildren()[routes.getChildCount() - 1];
const insertComma = lastChild.kind !== ts.SyntaxKind.CommaToken;
addRouteToArray(newOptions, host, lastChild.end, insertComma);
addRouteToArray(options, host, lastChild.end, insertComma);
} else {
addRouteToArray(newOptions, host, routes.end, false);
addRouteToArray(options, host, routes.end, false);
}
}
}
Expand All @@ -102,19 +118,26 @@ export function createPage(options: Options): Rule {
options = detectExtension('page', host, options);
options = applyNameAndPath('page', host, options);
options = determineArtifactName('page', host, options);
options = determineRoutingModule(host, options);

const operations: Rule[] = [];
operations.push(
mergeWith(
apply(url('./files'), [
template({
...strings,
...options,
}),
move(options.path),
])
)
);

if (options.lazy) {
operations.push(
mergeWith(
apply(url('./files'), [
template({
...strings,
...options,
}),
move(options.path),
])
)
);
} else {
operations.push(addImportToFile({ ...options, module: options.routingModule }));
}

operations.push(
schematic('component', {
...options,
Expand All @@ -123,8 +146,8 @@ export function createPage(options: Options): Rule {
flat: true,
})
);
operations.push(addRouteToRoutingModule(options));

operations.push(addRouteToRoutingModule(options));
operations.push(applyLintFix());

return chain(operations);
Expand Down
Loading

0 comments on commit d07a378

Please sign in to comment.