import path from "path"; import fs from "fs"; import { FileManager } from "./fileManager"; import { BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, BUNDLE_VERSION_TEMPLATE_REGEX, DEFAULT_BUNDLE_SHORT_VERSION, DEFAULT_BUNDLE_VERSION, GROUP_IDENTIFIER_TEMPLATE_REGEX, NCE_GROUP_REGEX, NCE_TARGET_NAME, NSE_GROUP_REGEX, NSE_TARGET_NAME } from "./iosConstants"; import { SmartechPushProps } from "./smartechPushProps"; export class NSEUpdaterManager { private nsePath: string; constructor(iosPath: string) { this.nsePath = iosPath; } async updateNSEFiles(props: SmartechPushProps): Promise { await this.updateNSEEntitlements(props.ios.groupIdentifier || ''); await this.updateNSEBundleVersion(props.ios.buildNumber || DEFAULT_BUNDLE_VERSION, props.ios.groupIdentifier || ''); await this.updateNSEBundleShortVersion(props.ios.version || DEFAULT_BUNDLE_SHORT_VERSION); } async updateNSEEntitlements(groupIdentifier: string): Promise { const entitlementsFilePath = path.join(this.nsePath, NSE_TARGET_NAME, 'SmartechNSE.entitlements'); let entitlementsFile = await FileManager.readFile(entitlementsFilePath); entitlementsFile = entitlementsFile.replace(GROUP_IDENTIFIER_TEMPLATE_REGEX, groupIdentifier); await FileManager.writeFile(entitlementsFilePath, entitlementsFile); } async updateNSEBundleVersion(version: string, nseGroupName: string): Promise { const plistFilePath = path.join(this.nsePath, NSE_TARGET_NAME, 'SmartechNSE-Info.plist'); let plistFile = await FileManager.readFile(plistFilePath); plistFile = plistFile.replace(BUNDLE_VERSION_TEMPLATE_REGEX, version); plistFile = plistFile.replace(NSE_GROUP_REGEX, nseGroupName); await FileManager.writeFile(plistFilePath, plistFile); } async updateNSEBundleShortVersion(version: string): Promise { const plistFilePath = path.join(this.nsePath, NSE_TARGET_NAME, 'SmartechNSE-Info.plist'); let plistFile = await FileManager.readFile(plistFilePath); plistFile = plistFile.replace(BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, version); await FileManager.writeFile(plistFilePath, plistFile); } } export class NCEExtensionManager { private ncePath: string; constructor(iosPath: string) { this.ncePath = iosPath; } async updateNCEEntitlements(groupIdentifier: string): Promise { const entitlementsFilePath = `${this.ncePath}/${NCE_TARGET_NAME}/SmartechNCE.entitlements`; let entitlementsFile = await FileManager.readFile(entitlementsFilePath); entitlementsFile = entitlementsFile.replace( GROUP_IDENTIFIER_TEMPLATE_REGEX, groupIdentifier ); await FileManager.writeFile(entitlementsFilePath, entitlementsFile); } async updateNCEBundleVersion(version: string, nceGroupName: string): Promise { const plistFilePath = `${this.ncePath}/${NCE_TARGET_NAME}/SmartechNCE-Info.plist`; let plistFile = await FileManager.readFile(plistFilePath); plistFile = plistFile.replace(BUNDLE_VERSION_TEMPLATE_REGEX, version); plistFile = plistFile.replace(NCE_GROUP_REGEX, nceGroupName); await FileManager.writeFile(plistFilePath, plistFile); } async updateNCEBundleShortVersion(version: string): Promise { const plistFilePath = `${this.ncePath}/${NCE_TARGET_NAME}/SmartechNCE-Info.plist`; let plistFile = await FileManager.readFile(plistFilePath); plistFile = plistFile.replace(BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, version); await FileManager.writeFile(plistFilePath, plistFile); } async updateSmartechNotificationIcon(iconName: string): Promise { const plistFilePath = `${this.ncePath}/${NCE_TARGET_NAME}/SmartechNCE-Info.plist`; let plistFile = await FileManager.readFile(plistFilePath); // Find and replace the SmartechNotificationIcon value const regex = /(SmartechNotificationIcon<\/key>\s*)[^<]*(<\/string>)/; plistFile = plistFile.replace(regex, `$1${iconName}$2`); await FileManager.writeFile(plistFilePath, plistFile); } async setupCustomNotificationIcon(props: SmartechPushProps, projectRoot: string): Promise { // Only proceed if custom notification icon configuration is provided if (!props.ios.notificationIcon) { console.log('No custom notification icon configuration provided, skipping icon setup'); return; } let iconName = ""; // Default icon name // Use custom name if provided, otherwise derive from imageset or use default if (props.ios.notificationIcon.name) { iconName = props.ios.notificationIcon.name; } // Create Media.xcassets directory structure in user's app const nceMediaAssetsPath = `${this.ncePath}/${NCE_TARGET_NAME}/Media.xcassets`; const nceImagesetPath = `${nceMediaAssetsPath}/NotificationIcon.imageset`; // Ensure Media.xcassets directory exists fs.mkdirSync(nceMediaAssetsPath, { recursive: true }); // Create or update Media.xcassets Contents.json const mediaContentsJson = { "info": { "author": "xcode", "version": 1 } }; await FileManager.writeFile( `${nceMediaAssetsPath}/Contents.json`, JSON.stringify(mediaContentsJson, null, 2) ); if (props.ios.notificationIcon.imageset) { // User provided a complete imageset folder const userImagesetPath = path.resolve(projectRoot, props.ios.notificationIcon.imageset); if (!fs.existsSync(userImagesetPath)) { throw new Error(`Custom imageset not found at: ${userImagesetPath}`); } // If no custom name provided, extract icon name from the imageset folder name if (!props.ios.notificationIcon.name) { iconName = path.basename(userImagesetPath, '.imageset'); } // Copy the entire imageset folder await FileManager.copyFolderAsync(userImagesetPath, nceImagesetPath); console.log(`Custom notification icon imageset copied from: ${userImagesetPath}`); } else if (props.ios.notificationIcon.images) { // User provided individual image paths const images = props.ios.notificationIcon.images; // Create Contents.json const contentsJson = { images: [ { idiom: "universal", scale: "1x", ...(images["1x"] && { filename: path.basename(images["1x"]) }) }, { filename: path.basename(images["2x"]), idiom: "universal", scale: "2x" }, { filename: path.basename(images["3x"]), idiom: "universal", scale: "3x" } ].filter(img => img.filename), // Remove entries without filename info: { author: "xcode", version: 1 } }; // Ensure the imageset directory exists fs.mkdirSync(nceImagesetPath, { recursive: true }); // Write Contents.json await FileManager.writeFile( `${nceImagesetPath}/Contents.json`, JSON.stringify(contentsJson, null, 2) ); // Copy individual images if (images["1x"]) { const srcPath = path.resolve(projectRoot, images["1x"]); const destPath = `${nceImagesetPath}/${path.basename(images["1x"])}`; if (fs.existsSync(srcPath)) { await FileManager.copyFile(srcPath, destPath); } else { throw new Error(`Custom notification icon 1x not found at: ${srcPath}`); } } const src2xPath = path.resolve(projectRoot, images["2x"]); const dest2xPath = `${nceImagesetPath}/${path.basename(images["2x"])}`; if (fs.existsSync(src2xPath)) { await FileManager.copyFile(src2xPath, dest2xPath); } else { throw new Error(`Custom notification icon 2x not found at: ${src2xPath}`); } const src3xPath = path.resolve(projectRoot, images["3x"]); const dest3xPath = `${nceImagesetPath}/${path.basename(images["3x"])}`; if (fs.existsSync(src3xPath)) { await FileManager.copyFile(src3xPath, dest3xPath); } else { throw new Error(`Custom notification icon 3x not found at: ${src3xPath}`); } console.log('Custom notification icon images copied successfully'); } // Update the plist with the icon name await this.updateSmartechNotificationIcon(iconName); console.log(`Updated SmartechNotificationIcon in plist to: ${iconName}`); } }