UNPKG

@api-buddy/plugin-utils

Version:

Shared utilities for API Buddy plugins

121 lines (119 loc) 3.5 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { SimplePluginManager: () => SimplePluginManager }); module.exports = __toCommonJS(index_exports); // 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..."); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SimplePluginManager }); //# sourceMappingURL=index.cjs.map