|
| 1 | +import type { ObjectLiteralExpression, Project, SourceFile } from "ts-morph"; |
| 2 | +import { SyntaxKind } from "ts-morph"; |
| 3 | +import type { CliOptions } from "../../../types/cli-options"; |
| 4 | +import { saveFileChanges } from "../../utils/log-utils"; |
| 5 | +import { migrateProvideIonicAngularImportDeclarations } from "../../utils/ionic-utils"; |
| 6 | + |
| 7 | +export const migrateAngularAppConfig = async ( |
| 8 | + project: Project, |
| 9 | + cliOptions: CliOptions, |
| 10 | +) => { |
| 11 | + const sourceFile = project |
| 12 | + .getSourceFiles() |
| 13 | + .find((sourceFile) => sourceFile.getFilePath().endsWith("app.config.ts")); |
| 14 | + |
| 15 | + if (sourceFile === undefined) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + const appConfigVariableStatement = sourceFile |
| 20 | + .getVariableStatements() |
| 21 | + .find((variableStatement) => { |
| 22 | + const declarationList = variableStatement.getDeclarationList(); |
| 23 | + const variableDeclaration = declarationList.getDeclarations()[0]; |
| 24 | + return variableDeclaration.getName() === "appConfig"; |
| 25 | + }); |
| 26 | + |
| 27 | + if (appConfigVariableStatement === undefined) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const appConfigVariableStatementDeclarationList = |
| 32 | + appConfigVariableStatement.getDeclarationList(); |
| 33 | + const appConfigVariableDeclaration = |
| 34 | + appConfigVariableStatementDeclarationList.getDeclarations()[0]; |
| 35 | + const appConfigInitializer = appConfigVariableDeclaration.getInitializer(); |
| 36 | + if (appConfigInitializer === undefined) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const appConfigObjectLiteralExpression = |
| 41 | + appConfigInitializer as ObjectLiteralExpression; |
| 42 | + const providersPropertyAssignment = |
| 43 | + appConfigObjectLiteralExpression.getProperty("providers"); |
| 44 | + if (providersPropertyAssignment === undefined) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const providersArray = providersPropertyAssignment.getFirstChildByKind( |
| 49 | + SyntaxKind.ArrayLiteralExpression, |
| 50 | + ); |
| 51 | + |
| 52 | + if (providersArray === undefined) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const importProvidersFromFunctionCall = providersArray |
| 57 | + .getChildrenOfKind(SyntaxKind.CallExpression) |
| 58 | + .find((callExpression) => { |
| 59 | + const identifier = callExpression.getFirstChildByKind( |
| 60 | + SyntaxKind.Identifier, |
| 61 | + ); |
| 62 | + return ( |
| 63 | + identifier !== undefined && |
| 64 | + identifier.getText() === "importProvidersFrom" |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + if (importProvidersFromFunctionCall === undefined) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const importProvidersFromFunctionCallIdentifier = |
| 73 | + importProvidersFromFunctionCall.getFirstChildByKind(SyntaxKind.Identifier); |
| 74 | + |
| 75 | + if (importProvidersFromFunctionCallIdentifier === undefined) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if ( |
| 80 | + importProvidersFromFunctionCallIdentifier.getText() !== |
| 81 | + "importProvidersFrom" |
| 82 | + ) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const importProvidersFromFunctionCallArguments = |
| 87 | + importProvidersFromFunctionCall.getArguments(); |
| 88 | + |
| 89 | + if (importProvidersFromFunctionCallArguments.length === 0) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const importProvidersFromFunctionCallIonicModuleForRootCallExpression = |
| 94 | + importProvidersFromFunctionCallArguments.find((argument) => { |
| 95 | + return argument.getText().includes("IonicModule.forRoot"); |
| 96 | + }); |
| 97 | + |
| 98 | + if ( |
| 99 | + importProvidersFromFunctionCallIonicModuleForRootCallExpression === |
| 100 | + undefined |
| 101 | + ) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if ( |
| 106 | + importProvidersFromFunctionCallIonicModuleForRootCallExpression.isKind( |
| 107 | + SyntaxKind.CallExpression, |
| 108 | + ) |
| 109 | + ) { |
| 110 | + const importProvidersFromFunctionCallIonicModuleForRootCallExpressionArguments = |
| 111 | + importProvidersFromFunctionCallIonicModuleForRootCallExpression.getArguments(); |
| 112 | + const ionicConfigObjectLiteralExpression = |
| 113 | + importProvidersFromFunctionCallIonicModuleForRootCallExpressionArguments[0]; |
| 114 | + |
| 115 | + const ionicConfigValue = ionicConfigObjectLiteralExpression |
| 116 | + ? ionicConfigObjectLiteralExpression.getText() |
| 117 | + : ""; |
| 118 | + |
| 119 | + providersArray.addElement(`provideIonicAngular(${ionicConfigValue})`); |
| 120 | + |
| 121 | + // Remove the IonicModule.forRoot from the importProvidersFrom function call. |
| 122 | + importProvidersFromFunctionCall.removeArgument( |
| 123 | + importProvidersFromFunctionCallIonicModuleForRootCallExpression, |
| 124 | + ); |
| 125 | + |
| 126 | + if (importProvidersFromFunctionCall.getArguments().length === 0) { |
| 127 | + // If there are no remaining arguments, remove the importProvidersFrom function call. |
| 128 | + providersArray.removeElement(importProvidersFromFunctionCall); |
| 129 | + } |
| 130 | + |
| 131 | + providersArray.formatText(); |
| 132 | + |
| 133 | + migrateProvideIonicAngularImportDeclarations(sourceFile); |
| 134 | + |
| 135 | + return await saveFileChanges(sourceFile, cliOptions); |
| 136 | + } |
| 137 | +}; |
0 commit comments