UNPKG

deprecopilot

Version:

Automated dependency management with AI-powered codemods

44 lines (43 loc) 1.5 kB
import { readFileSync } from 'fs'; import { resolve, isAbsolute } from 'path'; import { pathToFileURL } from 'url'; import { logger } from '../lib/logger.js'; export async function loadPlugins({ cwd = process.cwd(), verbose = false } = {}) { let pkg; try { pkg = JSON.parse(readFileSync(resolve(cwd, 'package.json'), 'utf-8')); } catch (e) { logger.debug('Could not read package.json for plugin loading'); return []; } const pluginIds = pkg.deprecopilot?.plugins || []; const plugins = []; for (const id of pluginIds) { let mod; try { let path = id; if (!isAbsolute(id) && !id.startsWith('.') && !id.startsWith('/')) { // Try to require as installed module path = require.resolve(id, { paths: [cwd] }); } else { path = resolve(cwd, id); } // Patch: convert absolute paths to file:// URLs for ESM import if (isAbsolute(path)) { path = pathToFileURL(path).href; } mod = await import(path); const plugin = mod.default || mod; if (!plugin.name) throw new Error('Plugin missing name'); plugins.push(plugin); logger.debug(`Loaded plugin: ${plugin.name}`); } catch (e) { logger.error(`Failed to load plugin ${id}: ${e.message}`); } } return plugins; }