import { fcmFileNames } from "./androidConstants"; import { failToRegisterForNotificationSnippet, importSmartPush, registerForNotificationSnippet, smartechPushNotificationDelegateSnippet, smartechPushReactNative, userNotificationDelegateSnippet } from "./iosConstants"; import { SmartechPushProps } from "./smartechPushProps"; import { promises as fsPromises } from 'fs'; import path from 'path'; export class Helper { static modifyImportContents(lines: string[]): string { const importIndex = lines.findIndex((line) => /^#import "AppDelegate.h"/.test(line) ); const interfaceDeclarationIndex = lines.findIndex((line) => /@interface AppDelegate \(\)/.test(line) ); const importsToAdd = [ smartechPushReactNative.fileImport, smartechPushReactNative.importSmartechBaseReactNative, importSmartPush, smartechPushReactNative.userNotification, smartechPushReactNative.userNotificationUI, ]; // Check and add imports if they don't already exist const uniqueImportsToAdd = importsToAdd.filter(importLine => !lines.some(line => line === importLine)); const importContents = [ ...lines.slice(0, importIndex + 1), ...uniqueImportsToAdd, interfaceDeclarationIndex < 0 ? smartechPushReactNative.interfaceLine : "", ...lines.slice(importIndex + 1), ].join("\n"); let allImportContents = importContents; if (interfaceDeclarationIndex >= 0) { const userNotificationCenterDelegateLines = importContents.split("\n"); const userNotificationCenterDelegateIndex = userNotificationCenterDelegateLines.findIndex((line) => /@interface AppDelegate \(\)/.test(line) ); const userNotificationCenterContents = [ ...userNotificationCenterDelegateLines.slice(0, userNotificationCenterDelegateIndex + 1), "", ...userNotificationCenterDelegateLines.slice(userNotificationCenterDelegateIndex + 1), ].join("\n"); allImportContents = userNotificationCenterContents; } return allImportContents; } static modifyDidFinishLaunchContents(allImportContents: string, props: SmartechPushProps): string { // Split the input contents into lines const didFinishLaunchLines = allImportContents.split("\n"); // Find the index where the target line is located const didLaunchIndex = didFinishLaunchLines.findIndex((line) => /\[super application:application didFinishLaunchingWithOptions/.test(line) ); // Get the snippet based on autoAskNotificationPermission config const snippet = smartechPushNotificationDelegateSnippet(props.autoAskNotificationPermission); // Create a new array with the additional lines using the spread operator const didFinishLaunchContents = [ ...didFinishLaunchLines.slice(0, didLaunchIndex), ...snippet, ...didFinishLaunchLines.slice(didLaunchIndex), ].join("\n"); return didFinishLaunchContents; }; static modifyRegisterForRemoteNotificationsContents(content: string): string { const allAppDelegateContent = content.split("\n"); let remoteIndex = allAppDelegateContent.findIndex( (line) => line.includes("return") && line.includes("didRegisterForRemoteNotificationsWithDeviceToken") ); let didRegisterForRemoteIndex = remoteIndex - 1; let registerRemoteString = registerForNotificationSnippet.registerString // If the function was not found, add the function declaration if (didRegisterForRemoteIndex < 0) { const lastLine = this.getLastLine(allAppDelegateContent); didRegisterForRemoteIndex = lastLine; registerRemoteString = registerForNotificationSnippet.registerNotificationFunction; } const registerRemoteNotiContents = [ ...allAppDelegateContent.slice(0, didRegisterForRemoteIndex + 1), registerRemoteString, ...allAppDelegateContent.slice(didRegisterForRemoteIndex + 1), ].join("\n"); return registerRemoteNotiContents; }; static modifyFailToRegisterForRemoteNotificationsContents(content: string): string { const allAppDelegateContent = content.split("\n"); let remoteIndex = allAppDelegateContent.findIndex( (line) => line.includes("return") && line.includes("didFailToRegisterForRemoteNotificationsWithError") ); let didRegisterForRemoteIndex = remoteIndex - 1; let registerRemoteString = failToRegisterForNotificationSnippet.registerString; // If the function was not found, add the function declaration if (didRegisterForRemoteIndex < 0) { const lastLine = this.getLastLine(allAppDelegateContent); didRegisterForRemoteIndex = lastLine; registerRemoteString = failToRegisterForNotificationSnippet.failToRegisterFunction; } const registerRemoteNotiContents = [ ...allAppDelegateContent.slice(0, didRegisterForRemoteIndex + 1), registerRemoteString, ...allAppDelegateContent.slice(didRegisterForRemoteIndex + 1), ].join("\n"); return registerRemoteNotiContents; }; static addUserNotifictionCenterDelegate(content: string): string { const smartechDelegateLines = content.split("\n"); const lastLine = this.getLastLine(smartechDelegateLines); const lineToAddSmartechDelegate = userNotificationDelegateSnippet; const smartechDelegateContents = [ ...smartechDelegateLines.slice(0, lastLine + 1), lineToAddSmartechDelegate, ...smartechDelegateLines.slice(lastLine + 1), ].join("\n"); return smartechDelegateContents; }; static getLastLine = (lineArray: string[]): number => { return lineArray.lastIndexOf("@end") - 1; }; static Android = class { static addDependency(appContents: string, dependency: string, version: string): string { const dependencyTemplate = `api "${dependency}:${version}"`; return appContents.replace('dependencies {', `dependencies {\n ${dependencyTemplate}`); } static containsDependency(contents: string, dependency: string) { const regex = new RegExp(`api\\s+"${dependency}[^"]*"`); return regex.test(contents); } static getFCMFileNames(isKotlin: boolean): { sourceFileName: string, targetFileName: string } { const fileName = isKotlin ? fcmFileNames.kotlin : fcmFileNames.java; return { sourceFileName: fileName, targetFileName: fileName, }; } } static Ios = class { static insert(contents: string, customCode: string, interfaceIdentifier: string): string { const lines = contents.split("\n"); let indexOfInterface = lines.findIndex((line) => line.includes(interfaceIdentifier)); if (indexOfInterface < 0) { indexOfInterface = 1; } return [ ...lines.slice(0, indexOfInterface), customCode, ...lines.slice(indexOfInterface), ].join("\n"); } static addSmartPushToAppDelegate(contents: string, customCode: string, interfaceIdentifier: string): string { const lines = contents.split("\n"); let indexOfInterface = lines.findIndex((line) => line.includes(interfaceIdentifier)); if (indexOfInterface < 0) { indexOfInterface = 1; } return [ ...lines.slice(0, indexOfInterface), customCode, ...lines.slice(indexOfInterface), ].join("\n"); } // Utility function to detect if project uses Swift static isSwiftProject = async (platformProjectRoot: string, projectName: string): Promise => { try { const appDelegateSwiftPath = path.join(platformProjectRoot, projectName, 'AppDelegate.swift'); await fsPromises.access(appDelegateSwiftPath); return true; } catch { return false; } }; // Utility function to get the correct AppDelegate file path for Objective-C projects static getObjCAppDelegatePath = async (platformProjectRoot: string, projectName: string): Promise => { const appDelegateMmPath = path.join(platformProjectRoot, projectName, 'AppDelegate.mm'); const appDelegateMPath = path.join(platformProjectRoot, projectName, 'AppDelegate.m'); try { await fsPromises.access(appDelegateMmPath); return appDelegateMmPath; } catch { return appDelegateMPath; // Fallback to .m even if it doesn't exist } }; } }