Browse Source

Merge pull request #24138 from abpframework/auto-merge/rel-10-0/4115

Merge branch dev with rel-10.0
pull/24170/head
Ma Liming 10 months ago
committed by GitHub
parent
commit
4a5b670ec1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 33
      npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts

33
npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts

@ -303,7 +303,7 @@ export function addRoutingToAppRoutingModule(
? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)` ? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)`
: `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`; : `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`;
const routeToAdd = `{ path: '${routePath}', loadChildren: ${routeExpr} }`; const routeToAdd = `{ path: '${routePath}', loadChildren: ${routeExpr} }`;
const change = addRouteToRoutesArray(source, 'APP_ROUTES', routeToAdd); const change = addRouteToRoutesArray(source, routeToAdd);
if (change instanceof InsertChange) { if (change instanceof InsertChange) {
const recorder = tree.beginUpdate(appRoutesPath); const recorder = tree.beginUpdate(appRoutesPath);
@ -355,29 +355,30 @@ export function addRoutingToAppRoutingModule(
export function addRouteToRoutesArray( export function addRouteToRoutesArray(
source: ts.SourceFile, source: ts.SourceFile,
arrayName: string,
routeToAdd: string, routeToAdd: string,
): InsertChange | null { ): InsertChange | null {
const routesVar = source.statements.find(
stmt => // Find all variable declarations that are array literals and contain route definitions
ts.isVariableStatement(stmt) && const routeArrays = source.statements
stmt.declarationList.declarations.some( .filter(ts.isVariableStatement)
.flatMap(stmt =>
stmt.declarationList.declarations.filter(
decl => decl =>
ts.isVariableDeclaration(decl) && ts.isVariableDeclaration(decl) &&
(decl.name.getText() === arrayName || decl.name.getText() === arrayName.toUpperCase()) && decl.initializer &&
decl.initializer !== undefined && ts.isArrayLiteralExpression(decl.initializer) &&
ts.isArrayLiteralExpression(decl.initializer), decl.initializer.elements.some(el =>
el.getText().includes('path:')
),
), ),
); );
if (!routesVar || !ts.isVariableStatement(routesVar)) { if (routeArrays.length === 0) {
throw new Error(`Could not find routes array named "${arrayName}".`); throw new Error('No routes array found in source file.');
} }
const declaration = routesVar.declarationList.declarations.find( // Select the first routes array found
decl => decl.name.getText() === arrayName, const declaration = routeArrays[0];
) as ts.VariableDeclaration;
const arrayLiteral = declaration.initializer as ts.ArrayLiteralExpression; const arrayLiteral = declaration.initializer as ts.ArrayLiteralExpression;
const getPathValue = (routeText: string): string | null => { const getPathValue = (routeText: string): string | null => {

Loading…
Cancel
Save