UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

97 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRoutesDeclaration = getRoutesDeclaration; exports.getRoutesNodeArray = getRoutesNodeArray; exports.insertRoute = insertRoute; const ts = require("typescript"); /** * Indicates if the given Route expression has the given path. * @param route Route expression * @param path Path to test */ function hasRoutePath(route, path) { return route.properties.some((property) => property.kind === ts.SyntaxKind.PropertyAssignment && property.name.text === 'path' && property.initializer.text === path); } /** * Indicates if the given variable declaration is a Routes declaration. * @param declaration Declaration to test */ function isRoutesDeclaration(declaration) { if (declaration.type) { if (declaration.type.kind === ts.SyntaxKind.TypeReference) { return declaration.type.typeName.getText() === 'Routes'; } if (declaration.type.kind === ts.SyntaxKind.ArrayType) { return declaration.type.elementType.typeName.getText() === 'Routes'; } } return false; } /** * Get the Routes variable declaration from the given App Routing Module path. * @param tree File tree * @param context Context of the rule * @param appRoutingModulePath Path of the App Routing Module */ function getRoutesDeclaration(tree, context, appRoutingModulePath) { const buffer = tree.read(appRoutingModulePath); if (!buffer) { context.logger.error(`Cannot read ${appRoutingModulePath}`); return null; } const sourceFile = ts.createSourceFile(appRoutingModulePath, buffer.toString(), ts.ScriptTarget.ES2015, true); return sourceFile.statements .filter((statement) => statement.kind === ts.SyntaxKind.VariableStatement) .map((statement) => statement.declarationList.declarations) .map((declarations) => declarations.filter((route) => isRoutesDeclaration(route))) .reduce((declaration, declarations) => declarations.length > 0 ? declarations[0] : declaration, null); } /** * Gets the Routes Node array of the App Routing Module of the given path. * @param tree File tree * @param context Context of the rule * @param appRoutingModulePath */ function getRoutesNodeArray(tree, context, appRoutingModulePath) { const routesDeclaration = getRoutesDeclaration(tree, context, appRoutingModulePath); if (!routesDeclaration) { context.logger.error('No Routes declaration found'); return null; } const rootElements = routesDeclaration.initializer.elements; const emptyRoute = rootElements.find((route) => hasRoutePath(route, '')); if (emptyRoute) { const childrenNode = emptyRoute.properties .filter((property) => property.kind === ts.SyntaxKind.PropertyAssignment) .filter((property) => property.name.text === 'children') .map((property) => property.initializer); if (childrenNode[0]) { return childrenNode[0].elements; } } return rootElements; } /** * Inserts a route in the App Routing Module of the given path. * @param tree File tree * @param context Context of the rule * @param appRoutingModulePath Path of the App Routing Module * @param route The Route to insert * @param standalone Whether the page component is standalone */ function insertRoute(tree, context, appRoutingModulePath, route, standalone = true) { const routes = getRoutesNodeArray(tree, context, appRoutingModulePath); if (routes) { const noStarRoutes = routes.filter((r) => !hasRoutePath(r, '**')); const index = noStarRoutes.length > 0 ? noStarRoutes.at(-1).end : routes.end; const routeString = `{path: '${route.path}', load${standalone ? 'Component' : 'Children'}: () => import('${route.import}').then((m) => m.${route.module})}`; const content = noStarRoutes.length > 0 ? `,\n${routeString}` : routeString; const recorder = tree.beginUpdate(appRoutingModulePath); recorder.insertLeft(index, content); tree.commitUpdate(recorder); } return tree; } //# sourceMappingURL=routes.js.map