UNPKG

angular-translation-checker

Version:

A comprehensive tool for analyzing translation keys in Angular projects using ngx-translate

147 lines 5.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginManager = void 0; const types_1 = require("../types"); class PluginManager { constructor(logger, eventBus) { this.logger = logger; this.eventBus = eventBus; this.plugins = new Map(); this.analyzers = new Map(); this.extractors = new Map(); this.formatters = new Map(); this.validators = new Map(); this.reporters = new Map(); } async registerPlugin(plugin, config) { try { this.logger.debug(`Registering plugin: ${plugin.name} v${plugin.version}`); // Check for name conflicts if (this.plugins.has(plugin.name)) { throw new types_1.PluginError(`Plugin with name '${plugin.name}' is already registered`, plugin.name); } // Create plugin context const context = { config, logger: this.logger, eventBus: this.eventBus }; // Initialize plugin await plugin.initialize(context); // Register plugin by type this.plugins.set(plugin.name, plugin); if (this.isAnalyzerPlugin(plugin)) { this.analyzers.set(plugin.name, plugin); } if (this.isExtractorPlugin(plugin)) { this.extractors.set(plugin.name, plugin); } if (this.isFormatterPlugin(plugin)) { this.formatters.set(plugin.outputFormat, plugin); } if (this.isValidatorPlugin(plugin)) { this.validators.set(plugin.name, plugin); } if (this.isReporterPlugin(plugin)) { this.reporters.set(plugin.name, plugin); } this.logger.info(`Successfully registered plugin: ${plugin.name}`); this.eventBus.emit('plugin:registered', { plugin: plugin.name }); } catch (error) { const pluginError = error instanceof types_1.PluginError ? error : new types_1.PluginError(`Failed to register plugin '${plugin.name}': ${error}`, plugin.name); this.logger.error(pluginError.message); throw pluginError; } } async unregisterPlugin(pluginName) { const plugin = this.plugins.get(pluginName); if (!plugin) { throw new types_1.PluginError(`Plugin '${pluginName}' is not registered`, pluginName); } try { // Cleanup plugin if (plugin.cleanup) { await plugin.cleanup(); } // Remove from all registries this.plugins.delete(pluginName); this.analyzers.delete(pluginName); this.extractors.delete(pluginName); this.validators.delete(pluginName); this.reporters.delete(pluginName); // Remove formatters by format for (const [format, formatter] of this.formatters.entries()) { if (formatter.name === pluginName) { this.formatters.delete(format); break; } } this.logger.info(`Successfully unregistered plugin: ${pluginName}`); this.eventBus.emit('plugin:unregistered', { plugin: pluginName }); } catch (error) { const pluginError = new types_1.PluginError(`Failed to unregister plugin '${pluginName}': ${error}`, pluginName); this.logger.error(pluginError.message); throw pluginError; } } getPlugin(name) { return this.plugins.get(name); } getAnalyzers() { return Array.from(this.analyzers.values()); } getExtractors() { return Array.from(this.extractors.values()); } getExtractorByExtension(extension) { return Array.from(this.extractors.values()).filter(extractor => extractor.supportedExtensions.includes(extension)); } getFormatter(format) { return this.formatters.get(format); } getValidators() { return Array.from(this.validators.values()); } getReporters() { return Array.from(this.reporters.values()); } getAllPlugins() { return Array.from(this.plugins.values()); } getPluginCount() { return this.plugins.size; } async cleanup() { const pluginNames = Array.from(this.plugins.keys()); for (const pluginName of pluginNames) { try { await this.unregisterPlugin(pluginName); } catch (error) { this.logger.error(`Error cleaning up plugin '${pluginName}': ${error}`); } } } // Type guards isAnalyzerPlugin(plugin) { return 'analyze' in plugin && typeof plugin.analyze === 'function'; } isExtractorPlugin(plugin) { return 'supportedExtensions' in plugin && 'extractKeys' in plugin; } isFormatterPlugin(plugin) { return 'outputFormat' in plugin && 'format' in plugin; } isValidatorPlugin(plugin) { return 'validate' in plugin && typeof plugin.validate === 'function'; } isReporterPlugin(plugin) { return 'report' in plugin && typeof plugin.report === 'function'; } } exports.PluginManager = PluginManager; //# sourceMappingURL=plugin-manager.js.map