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

72 lines 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceAppConfigProvider = CoerceAppConfigProvider; const ts_morph_1 = require("ts-morph"); const add_provider_to_array_1 = require("../add-provider-to-array"); const coerce_imports_1 = require("../coerce-imports"); const coerce_variable_declaration_1 = require("../coerce-variable-declaration"); const get_coerce_array_literal_form_object_literal_1 = require("../get-coerce-array-literal-form-object-literal"); function CoerceAppConfigProvider(sourceFile, options) { const { providers = [], httpInterceptors = [], importProvidersFrom = [], overwrite = false } = options; const appConfigVariableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, 'appConfig', { initializer: '{ providers: [\n] }', type: 'ApplicationConfig', }); (0, coerce_imports_1.CoerceImports)(sourceFile, { namedImports: ['ApplicationConfig'], moduleSpecifier: '@angular/core', }); const appConfig = appConfigVariableDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); const providerArray = (0, get_coerce_array_literal_form_object_literal_1.GetCoerceArrayLiteralFromObjectLiteral)(appConfig, 'providers'); for (const providerObject of providers) { (0, add_provider_to_array_1.AddProviderToArray)(providerObject, providerArray, overwrite); } if (httpInterceptors.length) { const phcExpression = (0, add_provider_to_array_1.AddProviderToArray)('provideHttpClient()', providerArray, overwrite) .asKindOrThrow(ts_morph_1.SyntaxKind.CallExpression); (0, coerce_imports_1.CoerceImports)(sourceFile, { moduleSpecifier: '@angular/common/http', namedImports: ['provideHttpClient'], }); const index = phcExpression.getArguments().findIndex(a => a.getText().startsWith('withInterceptors(')); let wiAExpression; let wiExpression; if (index === -1) { wiExpression = phcExpression.addArgument('withInterceptors()').asKindOrThrow(ts_morph_1.SyntaxKind.CallExpression); (0, coerce_imports_1.CoerceImports)(sourceFile, { moduleSpecifier: '@angular/common/http', namedImports: ['withInterceptors'], }); } else { wiExpression = phcExpression.getArguments()[index].asKindOrThrow(ts_morph_1.SyntaxKind.CallExpression); } if (wiExpression.getArguments()[0]) { wiAExpression = wiExpression.getArguments()[0].asKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); } else { wiAExpression = wiExpression.addArgument('[]').asKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); } for (const hi of httpInterceptors) { const index = wiAExpression.getElements().findIndex(a => a.getText() === hi); if (index === -1) { wiAExpression.addElement(hi); } } } if (importProvidersFrom.length) { const ipfExpression = (0, add_provider_to_array_1.AddProviderToArray)('importProvidersFrom()', providerArray, overwrite) .asKindOrThrow(ts_morph_1.SyntaxKind.CallExpression); (0, coerce_imports_1.CoerceImports)(sourceFile, { moduleSpecifier: '@angular/core', namedImports: ['importProvidersFrom'], }); for (const ipf of importProvidersFrom) { const index = ipfExpression.getArguments().findIndex(a => a.getText() === ipf); if (index === -1) { ipfExpression.addArgument(ipf); } } } } //# sourceMappingURL=coerce-app-config-provider.js.map