| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import { cancel, isCancel, multiselect, select, spinner, text } from '@clack/prompts';
- import { pascalCase } from 'change-case';
- import { ClassDeclaration, Project } from 'ts-morph';
- import { getPluginClasses, getTsMorphProject } from '../utilities/ast-utils';
- import { VendurePluginDeclaration } from '../utilities/vendure-plugin-declaration';
- export async function getCustomEntityName(cancelledMessage: string) {
- const entityName = await text({
- message: 'What is the name of the custom entity?',
- initialValue: '',
- validate: input => {
- if (!input) {
- return 'The custom entity name cannot be empty';
- }
- const pascalCaseRegex = /^[A-Z][a-zA-Z0-9]*$/;
- if (!pascalCaseRegex.test(input)) {
- return 'The custom entity name must be in PascalCase, e.g. "ProductReview"';
- }
- },
- });
- if (isCancel(entityName)) {
- cancel(cancelledMessage);
- process.exit(0);
- }
- return pascalCase(entityName);
- }
- export async function analyzeProject(options: {
- providedVendurePlugin?: VendurePluginDeclaration;
- cancelledMessage?: string;
- }) {
- const providedVendurePlugin = options.providedVendurePlugin;
- let project = providedVendurePlugin?.classDeclaration.getProject();
- if (!providedVendurePlugin) {
- const projectSpinner = spinner();
- projectSpinner.start('Analyzing project...');
- await new Promise(resolve => setTimeout(resolve, 100));
- project = getTsMorphProject();
- projectSpinner.stop('Project analyzed');
- }
- return project as Project;
- }
- export async function selectPlugin(
- project: Project,
- cancelledMessage: string,
- ): Promise<VendurePluginDeclaration> {
- const pluginClasses = getPluginClasses(project);
- const targetPlugin = await select({
- message: 'To which plugin would you like to add the feature?',
- options: pluginClasses.map(c => ({
- value: c,
- label: c.getName() as string,
- })),
- maxItems: 10,
- });
- if (isCancel(targetPlugin)) {
- cancel(cancelledMessage);
- process.exit(0);
- }
- return new VendurePluginDeclaration(targetPlugin as ClassDeclaration);
- }
- export async function selectMultiplePluginClasses(
- project: Project,
- cancelledMessage: string,
- ): Promise<VendurePluginDeclaration[]> {
- const pluginClasses = getPluginClasses(project);
- const selectAll = await select({
- message: 'To which plugin would you like to add the feature?',
- options: [
- {
- value: 'all',
- label: 'All plugins',
- },
- {
- value: 'specific',
- label: 'Specific plugins (you will be prompted to select the plugins)',
- },
- ],
- });
- if (isCancel(selectAll)) {
- cancel(cancelledMessage);
- process.exit(0);
- }
- if (selectAll === 'all') {
- return pluginClasses.map(pc => new VendurePluginDeclaration(pc));
- }
- const targetPlugins = await multiselect({
- message: 'Select one or more plugins (use ↑, ↓, space to select)',
- options: pluginClasses.map(c => ({
- value: c,
- label: c.getName() as string,
- })),
- });
- if (isCancel(targetPlugins)) {
- cancel(cancelledMessage);
- process.exit(0);
- }
- return (targetPlugins as ClassDeclaration[]).map(pc => new VendurePluginDeclaration(pc));
- }
|