UNPKG

native-update

Version:

Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.

203 lines 6.63 kB
import { Filesystem } from '@capacitor/filesystem'; import { Preferences } from '@capacitor/preferences'; import { ConfigManager } from './config'; import { Logger } from './logger'; import { SecurityValidator } from './security'; import { BundleManager } from '../live-update/bundle-manager'; import { DownloadManager } from '../live-update/download-manager'; import { VersionManager } from '../live-update/version-manager'; import { AppUpdateManager } from '../app-update/app-update-manager'; import { EventEmitter } from './event-emitter'; import { NativeUpdateError, ErrorCode } from './errors'; /** * Central manager for all plugin components */ export class PluginManager { constructor() { this.bundleManager = null; this.downloadManager = null; this.versionManager = null; this.appUpdateManager = null; this.initialized = false; this.configManager = ConfigManager.getInstance(); this.logger = Logger.getInstance(); this.securityValidator = SecurityValidator.getInstance(); this.eventEmitter = EventEmitter.getInstance(); } static getInstance() { if (!PluginManager.instance) { PluginManager.instance = new PluginManager(); } return PluginManager.instance; } /** * Initialize the plugin with configuration */ async initialize(config) { var _a, _b; if (this.initialized) { this.logger.warn('Plugin already initialized'); return; } try { // Configure the plugin this.configManager.configure(config); // Use Capacitor plugins directly if not provided if (!config.filesystem) { config.filesystem = Filesystem; } if (!config.preferences) { config.preferences = Preferences; } // Initialize managers this.bundleManager = new BundleManager(); await this.bundleManager.initialize(); this.downloadManager = new DownloadManager(); await this.downloadManager.initialize(); this.versionManager = new VersionManager(); await this.versionManager.initialize(); this.appUpdateManager = new AppUpdateManager({ serverUrl: config.serverUrl || config.baseUrl || '', channel: config.channel || 'production', autoCheck: (_a = config.autoCheck) !== null && _a !== void 0 ? _a : true, autoUpdate: (_b = config.autoUpdate) !== null && _b !== void 0 ? _b : false, updateStrategy: config.updateStrategy, publicKey: config.publicKey, requireSignature: config.requireSignature, checksumAlgorithm: config.checksumAlgorithm, checkInterval: config.checkInterval, security: config.security, }); // Wire up app update manager events to central event emitter this.setupAppUpdateEventBridge(); this.initialized = true; this.logger.info('Plugin initialized successfully'); } catch (error) { this.logger.error('Failed to initialize plugin', error); throw error; } } /** * Check if plugin is initialized */ isInitialized() { return this.initialized && this.configManager.isConfigured(); } /** * Ensure plugin is initialized */ ensureInitialized() { if (!this.isInitialized()) { throw new NativeUpdateError(ErrorCode.NOT_CONFIGURED, 'Plugin not initialized. Please call initialize() first.'); } } /** * Get bundle manager */ getBundleManager() { this.ensureInitialized(); return this.bundleManager; } /** * Get download manager */ getDownloadManager() { this.ensureInitialized(); return this.downloadManager; } /** * Get version manager */ getVersionManager() { this.ensureInitialized(); return this.versionManager; } /** * Get configuration manager */ getConfigManager() { return this.configManager; } /** * Get logger */ getLogger() { return this.logger; } /** * Get security validator */ getSecurityValidator() { return this.securityValidator; } /** * Get app update manager */ getAppUpdateManager() { this.ensureInitialized(); return this.appUpdateManager; } /** * Get event emitter */ getEventEmitter() { return this.eventEmitter; } /** * Reset plugin state */ async reset() { this.logger.info('Resetting plugin state'); // Clear all data if (this.bundleManager) { await this.bundleManager.clearAllBundles(); } if (this.versionManager) { await this.versionManager.clearVersionCache(); } if (this.downloadManager) { this.downloadManager.cancelAllDownloads(); } // Reset initialization state this.bundleManager = null; this.downloadManager = null; this.versionManager = null; this.appUpdateManager = null; this.initialized = false; // Clear all event listeners this.eventEmitter.removeAllListeners(); this.logger.info('Plugin reset complete'); } /** * Clean up resources */ async cleanup() { this.logger.info('Cleaning up plugin resources'); // Cancel any active downloads if (this.downloadManager) { this.downloadManager.cancelAllDownloads(); } // Clean expired bundles if (this.bundleManager) { await this.bundleManager.cleanExpiredBundles(); } this.logger.info('Cleanup complete'); } /** * Setup event bridge between AppUpdateManager and central EventEmitter */ setupAppUpdateEventBridge() { if (!this.appUpdateManager) return; // Bridge appUpdateStateChanged events this.appUpdateManager.addListener('appUpdateStateChanged', (event) => { this.eventEmitter.emit('appUpdateStateChanged', event); }); // Bridge appUpdateProgress events this.appUpdateManager.addListener('appUpdateProgress', (event) => { this.eventEmitter.emit('appUpdateProgress', event); }); } } //# sourceMappingURL=plugin-manager.js.map