UNPKG

@api-buddy/plugin-utils

Version:

Shared utilities for API Buddy plugins

94 lines 2.45 kB
// src/plugin-manager.js var SimplePluginManager = class { constructor(options = {}) { this.plugins = /* @__PURE__ */ 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: /* @__PURE__ */ new Map(), utils: { registerHook: () => { }, unregisterHook: () => { }, callHook: async () => void 0 } }; } /** * Register a plugin */ async registerPlugin(plugin) { if (this.plugins.has(plugin.name)) { throw new Error(`Plugin with name '${plugin.name}' is already registered`); } try { 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 { 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() { this.context.logger.info("Loading plugins..."); } }; export { SimplePluginManager }; //# sourceMappingURL=index.js.map