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

57 lines 2.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoveRoute = RemoveRoute; const ts_morph_1 = require("@rxap/ts-morph"); const ts_morph_2 = require("ts-morph"); require("colors"); function RemoveRoute(sourceFile, options) { var _a; const { path, component, index, loadRemoteModule, name = 'ROUTES' } = options; if (!path && !component && index === undefined && !loadRemoteModule) { throw new Error('You must provide a path or a component or a index or a loadRemoteModule to remove a route'); } if (path) { throw new Error('Not implemented'); } const variableDeclaration = (0, ts_morph_1.CoerceVariableDeclaration)(sourceFile, name, { initializer: '[]', type: 'Route[]' }); const arrayLiteralExpression = variableDeclaration.getInitializerIfKindOrThrow(ts_morph_2.SyntaxKind.ArrayLiteralExpression); if (index !== undefined) { arrayLiteralExpression.removeElement(index); return; } const items = arrayLiteralExpression.getElements(); for (const item of items) { if (item.isKind(ts_morph_2.SyntaxKind.ObjectLiteralExpression)) { const obj = item.asKindOrThrow(ts_morph_2.SyntaxKind.ObjectLiteralExpression); if (component) { const componentProperty = obj.getProperty('component'); if (componentProperty) { const value = componentProperty.asKindOrThrow(ts_morph_2.SyntaxKind.PropertyAssignment).getInitializer(); if (value.getText() === component) { arrayLiteralExpression.removeElement(item); return; } } } if (loadRemoteModule) { const loadChildrenProperty = obj.getProperty('loadChildren'); if (loadChildrenProperty) { const value = loadChildrenProperty.asKindOrThrow(ts_morph_2.SyntaxKind.PropertyAssignment).getInitializer(); let regex; if (typeof loadRemoteModule === 'string') { regex = new RegExp(`^\\(\\) =>[\\s\\S]+loadRemoteModule\\('${loadRemoteModule}'`); } else { regex = new RegExp(`^\\(\\) =>[\\s\\S]+loadRemoteModule\\('${loadRemoteModule.name}',\\s*'${(_a = loadRemoteModule.entry) !== null && _a !== void 0 ? _a : './routes'}'\\)`); } if (value.getText().match(regex)) { arrayLiteralExpression.removeElement(item); return; } } } } } console.log('No route found to remove'.yellow); } //# sourceMappingURL=remove-route.js.map