UNPKG

@api-buddy/plugin-utils

Version:

Shared utilities for API Buddy plugins

106 lines 3.41 kB
/** * A simple plugin manager that handles loading and managing plugins. * Can be extended to add custom plugin functionality. */ export class SimplePluginManager { constructor(options = {}) { this.plugins = new Map(); this.defaultLogger = { info: console.log, warn: console.warn, error: console.error, debug: console.debug, }; this.options = { pluginsDir: Array.isArray(options.pluginsDir) ? options.pluginsDir : options.pluginsDir ? [options.pluginsDir] : [], nodeModulesDirs: options.nodeModulesDirs || [], }; this.context = { cwd: options.cwd || process.cwd(), logger: { ...this.defaultLogger, ...(options.logger || {}) }, config: {}, data: new Map(), utils: { registerHook: () => { }, unregisterHook: () => { }, callHook: async () => undefined, }, }; } /** * Register a plugin */ async registerPlugin(plugin) { if (this.plugins.has(plugin.name)) { throw new Error(`Plugin with name '${plugin.name}' is already registered`); } try { // Initialize the plugin if it has an initialize method if (typeof plugin.initialize === 'function') { await plugin.initialize(this.context); } this.plugins.set(plugin.name, plugin); this.context.logger.info(`Registered plugin: ${plugin.name}@${plugin.version}`); } catch (error) { this.context.logger.error(`Failed to initialize plugin ${plugin.name}:`, error); throw error; } } /** * Get a registered plugin by name */ getPlugin(name) { return this.plugins.get(name); } /** * Get all registered plugins */ getPlugins() { return Array.from(this.plugins.values()); } /** * Check if a plugin is registered */ hasPlugin(name) { return this.plugins.has(name); } /** * Unregister a plugin */ async unregisterPlugin(name) { const plugin = this.plugins.get(name); if (!plugin) return false; try { // Clean up the plugin if it has a cleanup method if (typeof plugin.cleanup === 'function') { await plugin.cleanup(this.context); } this.plugins.delete(name); this.context.logger.info(`Unregistered plugin: ${name}`); return true; } catch (error) { this.context.logger.error(`Failed to cleanup plugin ${name}:`, error); return false; } } /** * Load plugins from the specified directories */ async loadPlugins() { // Implementation for loading plugins from directories // This is a simplified version - in a real implementation, you would: // 1. Scan the plugin directories for plugin modules // 2. Import and initialize each plugin // 3. Register them with the plugin manager this.context.logger.info('Loading plugins...'); // Actual implementation would go here } } //# sourceMappingURL=plugin-manager.js.map