plugin-resolution.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Project } from 'ts-morph';
  2. import { getPluginClasses } from '../utilities/ast-utils';
  3. import { VendurePluginRef } from './vendure-plugin-ref';
  4. export interface PluginResolutionOptions {
  5. providedPlugin?: VendurePluginRef;
  6. pluginName?: string;
  7. isNonInteractive?: boolean;
  8. }
  9. export interface PluginResolutionResult {
  10. plugin: VendurePluginRef | undefined;
  11. shouldPromptForSelection: boolean;
  12. }
  13. /**
  14. * Resolves a plugin reference from provided options, handling both interactive and non-interactive modes.
  15. * This function centralizes the common plugin resolution logic used across multiple CLI commands.
  16. */
  17. export function resolvePluginFromOptions(
  18. project: Project,
  19. options: PluginResolutionOptions,
  20. ): PluginResolutionResult {
  21. let plugin: VendurePluginRef | undefined = options.providedPlugin;
  22. if (!plugin && options.pluginName) {
  23. const pluginClasses = getPluginClasses(project);
  24. const foundPlugin = pluginClasses.find(p => p.getName() === options.pluginName);
  25. if (!foundPlugin) {
  26. const availablePlugins = pluginClasses.map(p => p.getName()).filter(Boolean);
  27. throw new Error(
  28. `Plugin "${options.pluginName}" not found. Available plugins:\n` +
  29. availablePlugins.map(name => ` - ${name as string}`).join('\n'),
  30. );
  31. }
  32. plugin = new VendurePluginRef(foundPlugin);
  33. }
  34. if (options.isNonInteractive && !plugin) {
  35. throw new Error('Plugin must be specified when running in non-interactive mode');
  36. }
  37. return {
  38. plugin,
  39. shouldPromptForSelection: !plugin && !options.isNonInteractive,
  40. };
  41. }