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

93 lines 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceAppNavigation = CoerceAppNavigation; const utilities_1 = require("@rxap/utilities"); const ts_morph_1 = require("ts-morph"); const coerce_imports_1 = require("../coerce-imports"); const coerce_variable_declaration_1 = require("../coerce-variable-declaration"); function CoerceAppNavigation(sourceFile, options = {}) { var _a, _b, _c, _d, _e; const variableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, 'APP_NAVIGATION', { type: '() => NavigationWithInserts', initializer: '() => []', }); (0, coerce_imports_1.CoerceImports)(sourceFile, [ { namedImports: ['NavigationWithInserts', 'RXAP_NAVIGATION_CONFIG'], moduleSpecifier: '@rxap/layout', }, ]); if (!((_a = options.itemList) === null || _a === void 0 ? void 0 : _a.length)) { return; } const arrowFunction = variableDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrowFunction); const array = arrowFunction.getBody().asKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); const arrayLiteralExpression = array.asKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); const existingItems = arrayLiteralExpression.getElements() .filter(e => e.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) .map(e => e.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression)); const existingItemLabels = existingItems.map(e => { var _a; return (_a = e.getProperty('label')) === null || _a === void 0 ? void 0 : _a.asKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment).getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.StringLiteral).getText().trim().replace(/['"]/g, ''); }); for (const item of (_b = options.itemList) !== null && _b !== void 0 ? _b : []) { const existingItemIndex = existingItemLabels.findIndex(e => e === item.label); const routerLink = w => { w.write('['); for (let i = 0; i < item.routerLink.length; i++) { const link = item.routerLink[i]; w.quote(link); if (i < item.routerLink.length - 1) { w.write(','); } } w.write(']'); }; let icon = null; if (item.icon) { const normalizedIcon = (0, utilities_1.NormalizeIconConfig)(item.icon); const iconObj = {}; if (normalizedIcon.color) { iconObj['color'] = w => w.quote(normalizedIcon.color); } if ((0, utilities_1.IsSvgIcon)(normalizedIcon)) { const name = normalizedIcon.svgIcon; iconObj['svgIcon'] = w => w.quote(name); } if ((0, utilities_1.IsMaterialIcon)(normalizedIcon)) { const name = normalizedIcon.icon; iconObj['icon'] = w => w.quote(name); } icon = ts_morph_1.Writers.object(iconObj); } if (existingItemIndex === -1) { const obj = { routerLink, label: w => w.quote(item.label), }; if (icon) { obj['icon'] = icon; } arrayLiteralExpression.addElement(ts_morph_1.Writers.object(obj)); } else { const existingItem = existingItems[existingItemIndex]; (_c = existingItem.getProperty('routerLink')) === null || _c === void 0 ? void 0 : _c.asKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment).setInitializer(routerLink); if (icon) { if (!existingItem.getProperty('icon')) { existingItem.addPropertyAssignment({ name: 'icon', initializer: icon, }); } else if (options.overwrite) { (_d = existingItem.getProperty('icon')) === null || _d === void 0 ? void 0 : _d.asKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment).setInitializer(icon); } } else if (options.overwrite) { (_e = existingItem.getProperty('icon')) === null || _e === void 0 ? void 0 : _e.remove(); } } } } //# sourceMappingURL=coerce-app-navigation.js.map