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

42 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceRoutes = CoerceRoutes; const ts_morph_1 = require("ts-morph"); const coerce_default_export_1 = require("../coerce-default-export"); const coerce_imports_1 = require("../coerce-imports"); const coerce_variable_declaration_1 = require("../coerce-variable-declaration"); const add_route_1 = require("./add-route"); function CoerceRoutes(sourceFile, options = {}) { var _a, _b; const { name = 'ROUTES', initializer = `[{ path: '**', redirectTo: '' }]` } = options; // region remove a route declaration if the initializer is an empty array to ensure the initializer is set let variableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, name, { initializer: '[]', type: 'Route[]' }); if (variableDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression).getElements().length === 0) { variableDeclaration.getVariableStatementOrThrow().remove(); } // endregion variableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, name, { initializer, type: 'Route[]' }); (0, coerce_default_export_1.CoerceDefaultExport)(variableDeclaration); (0, coerce_imports_1.CoerceImports)(sourceFile, [ { namedImports: ['Route'], moduleSpecifier: '@angular/router' }, ]); if ((_a = options.itemList) === null || _a === void 0 ? void 0 : _a.some(item => item.route.loadRemoteModule)) { (0, coerce_imports_1.CoerceImports)(sourceFile, [ { namedImports: ['loadRemoteModule'], moduleSpecifier: '@nx/angular/mf' }, ]); } for (const item of (_b = options.itemList) !== null && _b !== void 0 ? _b : []) { (0, add_route_1.AddRoute)(sourceFile, Object.assign(Object.assign({}, item), { name })); } return variableDeclaration; } //# sourceMappingURL=coerce-routes.js.map