UNPKG

@rxap/ts-morph

Version:

Provides utilities for manipulating TypeScript code using the ts-morph library. It offers a fluent API to add, modify, and remove code elements such as classes, decorators, imports, and properties in both Angular and NestJS projects. This package simplifi

33 lines 1.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceRouteGuard = CoerceRouteGuard; const ts_morph_1 = require("@rxap/ts-morph"); const ts_morph_2 = require("ts-morph"); function CoerceRouteGuard(sourceFile, path, guard, options = {}) { var _a; const { routeArrayName = 'ROUTES', type = 'canActivate' } = options; const routes = sourceFile.getVariableDeclaration(routeArrayName); if (routes) { const initializer = (0, ts_morph_1.FindParentRouteByPath)(routes.getInitializerIfKindOrThrow(ts_morph_2.SyntaxKind.ArrayLiteralExpression), path); if (initializer) { const guardArray = (_a = initializer.getProperty(type)) !== null && _a !== void 0 ? _a : initializer.addPropertyAssignment({ name: type, initializer: '[]', }); if (guardArray.isKind(ts_morph_2.SyntaxKind.PropertyAssignment)) { const guardArrayInitializer = guardArray.getInitializerIfKindOrThrow(ts_morph_2.SyntaxKind.ArrayLiteralExpression); (0, ts_morph_1.CoerceArrayElement)(guardArrayInitializer, guard); } else { throw new Error('Guard array is not a PropertyAssignment'); } } else { console.warn('Initializer not found'); } } else { throw new Error(`${routeArrayName} variable not found`); } } //# sourceMappingURL=coerce-route-guard.js.map