|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { Command } from 'commander'; |
| 4 | +import chalk from 'chalk'; |
| 5 | +import { loadConfig } from './config.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * CLI Command Contribution resolved from a plugin manifest. |
| 9 | + */ |
| 10 | +interface ResolvedCommandContribution { |
| 11 | + /** CLI command name */ |
| 12 | + name: string; |
| 13 | + /** Brief description */ |
| 14 | + description?: string; |
| 15 | + /** Module path to import */ |
| 16 | + module?: string; |
| 17 | + /** Source plugin package name */ |
| 18 | + pluginName: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Discover CLI command contributions from installed plugins. |
| 23 | + * |
| 24 | + * Scans the project's `objectstack.config.ts` for plugins that declare |
| 25 | + * `contributes.commands` in their manifest, then dynamically imports |
| 26 | + * those plugin modules to register Commander.js commands. |
| 27 | + * |
| 28 | + * @param program - The root Commander.js program to register commands on |
| 29 | + */ |
| 30 | +export async function loadPluginCommands(program: Command): Promise<void> { |
| 31 | + let config: any; |
| 32 | + |
| 33 | + try { |
| 34 | + const loaded = await loadConfig(); |
| 35 | + config = loaded.config; |
| 36 | + } catch { |
| 37 | + // No config file found — nothing to load |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const plugins: unknown[] = [ |
| 42 | + ...(config.plugins || []), |
| 43 | + ...(config.devPlugins || []), |
| 44 | + ]; |
| 45 | + |
| 46 | + // Collect command contributions from plugin manifests |
| 47 | + const contributions: ResolvedCommandContribution[] = []; |
| 48 | + |
| 49 | + for (const plugin of plugins) { |
| 50 | + if (!plugin || typeof plugin !== 'object') continue; |
| 51 | + const p = plugin as Record<string, unknown>; |
| 52 | + |
| 53 | + const manifest = p.manifest as Record<string, unknown> | undefined; |
| 54 | + const contributes = (manifest?.contributes ?? p.contributes) as Record<string, unknown> | undefined; |
| 55 | + if (!contributes) continue; |
| 56 | + |
| 57 | + const commands = contributes.commands as Array<Record<string, unknown>> | undefined; |
| 58 | + if (!Array.isArray(commands)) continue; |
| 59 | + |
| 60 | + const pluginName = resolvePluginName(p); |
| 61 | + |
| 62 | + for (const cmd of commands) { |
| 63 | + if (!cmd || typeof cmd.name !== 'string') continue; |
| 64 | + contributions.push({ |
| 65 | + name: cmd.name, |
| 66 | + description: typeof cmd.description === 'string' ? cmd.description : undefined, |
| 67 | + module: typeof cmd.module === 'string' ? cmd.module : undefined, |
| 68 | + pluginName, |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (contributions.length === 0) return; |
| 74 | + |
| 75 | + // Load and register each contributed command |
| 76 | + for (const contribution of contributions) { |
| 77 | + try { |
| 78 | + const commands = await importPluginCommands(contribution); |
| 79 | + for (const cmd of commands) { |
| 80 | + program.addCommand(cmd); |
| 81 | + } |
| 82 | + } catch (error: any) { |
| 83 | + // Log warning but don't crash — plugin commands are optional |
| 84 | + if (process.env.DEBUG) { |
| 85 | + console.error( |
| 86 | + chalk.yellow(` ⚠ Failed to load CLI command '${contribution.name}' from plugin '${contribution.pluginName}': ${error.message}`) |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Import Commander.js commands from a plugin module. |
| 95 | + * |
| 96 | + * The module must export commands in one of these forms: |
| 97 | + * - `export const commands: Command[]` |
| 98 | + * - `export default Command` |
| 99 | + * - `export default Command[]` |
| 100 | + */ |
| 101 | +async function importPluginCommands( |
| 102 | + contribution: ResolvedCommandContribution |
| 103 | +): Promise<Command[]> { |
| 104 | + // Resolve the module specifier |
| 105 | + const moduleId = contribution.module |
| 106 | + ? `${contribution.pluginName}/${contribution.module.replace(/^\.\//, '')}` |
| 107 | + : contribution.pluginName; |
| 108 | + |
| 109 | + const mod = await import(moduleId); |
| 110 | + |
| 111 | + // Form 1: Named export `commands` |
| 112 | + if (Array.isArray(mod.commands)) { |
| 113 | + return mod.commands.filter(isCommandInstance); |
| 114 | + } |
| 115 | + |
| 116 | + // Form 2: Default export (single or array) |
| 117 | + const defaultExport = mod.default; |
| 118 | + if (defaultExport) { |
| 119 | + if (Array.isArray(defaultExport)) { |
| 120 | + return defaultExport.filter(isCommandInstance); |
| 121 | + } |
| 122 | + if (isCommandInstance(defaultExport)) { |
| 123 | + return [defaultExport]; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Fallback: search for any Command instances in module exports |
| 128 | + const commands: Command[] = []; |
| 129 | + for (const key of Object.keys(mod)) { |
| 130 | + if (isCommandInstance(mod[key])) { |
| 131 | + commands.push(mod[key]); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return commands; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Check if a value is a Commander.js Command instance. |
| 140 | + * Uses duck-typing to avoid import dependency issues. |
| 141 | + */ |
| 142 | +function isCommandInstance(value: unknown): value is Command { |
| 143 | + return ( |
| 144 | + value !== null && |
| 145 | + typeof value === 'object' && |
| 146 | + typeof (value as any).name === 'function' && |
| 147 | + typeof (value as any).description === 'function' && |
| 148 | + typeof (value as any).action === 'function' && |
| 149 | + typeof (value as any).parse === 'function' |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Resolve a human-readable name from a plugin object. |
| 155 | + */ |
| 156 | +function resolvePluginName(plugin: Record<string, unknown>): string { |
| 157 | + if (typeof plugin.name === 'string') return plugin.name; |
| 158 | + const manifest = plugin.manifest as Record<string, unknown> | undefined; |
| 159 | + if (manifest && typeof manifest.name === 'string') return manifest.name; |
| 160 | + if (plugin.constructor && plugin.constructor.name !== 'Object') return plugin.constructor.name; |
| 161 | + return 'unknown'; |
| 162 | +} |
0 commit comments