useasdemo
Version:
108 lines (101 loc) • 5 kB
text/typescript
import { Tree } from '@angular-devkit/schematics';
export const MODULE_ROUTE_CONTENTS = {
LOG: `\n\t\t\t{ path: 'log', loadChildren: './log/log.module#LogModule' },`,
USER: `\n\t\t\t{ path: 'user', loadChildren: './user/user.module#UserModule' },`,
PERMISSION: `\n\t\t\t{ path: 'permission', loadChildren: './permission/permission.module#PermissionModule' },`,
SYSTEM: `\n\t\t\t{ path: 'system', loadChildren: './system/system.module#SystemModule' },`,
APP: `\n\t\t\t{ path: 'app', loadChildren: './app/app.module#AppModule' },`,
AUTH: `\n\t\t\t{ path: 'auth', loadChildren: './auth/auth.module#AuthModule' },`,
}
export function routingModuleContent(addcontents: string[]): string {
const routingModuleContentStart = `import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { environment } from '@env/environment';
// layout
import { LayoutDefaultComponent } from '../layout/default/default.component';
import { LayoutFullScreenComponent } from '../layout/fullscreen/fullscreen.component';
import { LayoutPassportComponent } from '../layout/passport/passport.component';
// dashboard pages
import { DashboardComponent } from './dashboard/dashboard.component';
// passport pages
import { UserLoginComponent } from './passport/login/login.component';
import { UserRegisterComponent } from './passport/register/register.component';
import { UserRegisterResultComponent } from './passport/register-result/register-result.component';
// single pages
import { CallbackComponent } from './callback/callback.component';
import { UserLockComponent } from './passport/lock/lock.component';
import { Exception403Component } from './exception/403.component';
import { Exception404Component } from './exception/404.component';
import { Exception500Component } from './exception/500.component';
const routes: Routes = [
{
path: '',
component: LayoutDefaultComponent,
children: [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent, data: { title: '主页'} },
// 业务子模块
// { path: 'widgets', loadChildren: './widgets/widgets.module#WidgetsModule' }
`;
var fullContent = routingModuleContentStart;
addcontents.forEach(content => {
fullContent += content;
});
fullContent += ` \n]
},
// 全屏布局
// {
// path: 'fullscreen',
// component: LayoutFullScreenComponent,
// children: [
// ]
// },
// passport
{
path: 'passport',
component: LayoutPassportComponent,
children: [
{ path: 'login', component: UserLoginComponent, data: { title: '登录', titleI18n: 'pro-login' } },
{ path: 'register', component: UserRegisterComponent, data: { title: '注册', titleI18n: 'pro-register' } },
{ path: 'register-result', component: UserRegisterResultComponent, data: { title: '注册结果', titleI18n: 'pro-register-result' } }
]
},
// 单页不包裹Layout
{ path: 'callback/:type', component: CallbackComponent },
{ path: 'lock', component: UserLockComponent, data: { title: '锁屏', titleI18n: 'lock' } },
{ path: '403', component: Exception403Component },
{ path: '404', component: Exception404Component },
{ path: '500', component: Exception500Component },
{ path: '**', redirectTo: 'dashboard' }
];
export class RouteRoutingModule { }`;
return fullContent;
}
export function insertContent(source: string, start: number, newContent: string) {
return source.slice(0, start) + newContent + source.slice(start);
}
/** 字符串匹配,判断是否已存在对应子路由**/
export function routeModules(tree: Tree, path: string): number[] {
const routes: number[] = [];
const orginalRoutingModuleContent = tree.get(`${path}/app/routes/routes-routing.module.ts`).content.toString();
const appModuleIndex = orginalRoutingModuleContent.lastIndexOf(`${MODULE_ROUTE_CONTENTS.APP}`.trim());
const authModuleIndex = orginalRoutingModuleContent.indexOf(`${MODULE_ROUTE_CONTENTS.AUTH}`.trim());
const permissionModuleIndex = orginalRoutingModuleContent.indexOf(`${MODULE_ROUTE_CONTENTS.PERMISSION}`.trim());
const systemModuleIndex = orginalRoutingModuleContent.lastIndexOf(`${MODULE_ROUTE_CONTENTS.SYSTEM}`.trim());
const userModuleIndex = orginalRoutingModuleContent.indexOf(`${MODULE_ROUTE_CONTENTS.USER}`.trim());
const logModuleIndex = orginalRoutingModuleContent.indexOf(`${MODULE_ROUTE_CONTENTS.LOG}`.trim());
routes.push(appModuleIndex, authModuleIndex,
permissionModuleIndex, systemModuleIndex,
userModuleIndex, logModuleIndex);
routes.sort();
console.log(routes);
return routes;
}