shared-prompts.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { cancel, isCancel, multiselect, select, spinner } from '@clack/prompts';
  2. import { ClassDeclaration, Project } from 'ts-morph';
  3. import { Messages } from '../constants';
  4. import { getPluginClasses, getTsMorphProject } from '../utilities/ast-utils';
  5. import { VendurePluginRef } from './vendure-plugin-ref';
  6. export async function analyzeProject(options: {
  7. providedVendurePlugin?: VendurePluginRef;
  8. cancelledMessage?: string;
  9. }) {
  10. const providedVendurePlugin = options.providedVendurePlugin;
  11. let project = providedVendurePlugin?.classDeclaration.getProject();
  12. if (!providedVendurePlugin) {
  13. const projectSpinner = spinner();
  14. projectSpinner.start('Analyzing project...');
  15. await new Promise(resolve => setTimeout(resolve, 100));
  16. project = getTsMorphProject();
  17. projectSpinner.stop('Project analyzed');
  18. }
  19. return project as Project;
  20. }
  21. export async function selectPlugin(project: Project, cancelledMessage: string): Promise<VendurePluginRef> {
  22. const pluginClasses = getPluginClasses(project);
  23. if (pluginClasses.length === 0) {
  24. cancel(Messages.NoPluginsFound);
  25. process.exit(0);
  26. }
  27. const targetPlugin = await select({
  28. message: 'To which plugin would you like to add the feature?',
  29. options: pluginClasses.map(c => ({
  30. value: c,
  31. label: c.getName() as string,
  32. })),
  33. maxItems: 10,
  34. });
  35. if (isCancel(targetPlugin)) {
  36. cancel(cancelledMessage);
  37. process.exit(0);
  38. }
  39. return new VendurePluginRef(targetPlugin as ClassDeclaration);
  40. }
  41. export async function selectMultiplePluginClasses(
  42. project: Project,
  43. cancelledMessage: string,
  44. ): Promise<VendurePluginRef[]> {
  45. const pluginClasses = getPluginClasses(project);
  46. if (pluginClasses.length === 0) {
  47. cancel(Messages.NoPluginsFound);
  48. process.exit(0);
  49. }
  50. const selectAll = await select({
  51. message: 'To which plugin would you like to add the feature?',
  52. options: [
  53. {
  54. value: 'all',
  55. label: 'All plugins',
  56. },
  57. {
  58. value: 'specific',
  59. label: 'Specific plugins (you will be prompted to select the plugins)',
  60. },
  61. ],
  62. });
  63. if (isCancel(selectAll)) {
  64. cancel(cancelledMessage);
  65. process.exit(0);
  66. }
  67. if (selectAll === 'all') {
  68. return pluginClasses.map(pc => new VendurePluginRef(pc));
  69. }
  70. const targetPlugins = await multiselect({
  71. message: 'Select one or more plugins (use ↑, ↓, space to select)',
  72. options: pluginClasses.map(c => ({
  73. value: c,
  74. label: c.getName() as string,
  75. })),
  76. });
  77. if (isCancel(targetPlugins)) {
  78. cancel(cancelledMessage);
  79. process.exit(0);
  80. }
  81. return (targetPlugins as ClassDeclaration[]).map(pc => new VendurePluginRef(pc));
  82. }