UNPKG

@vendure/cli

Version:

A modern, headless ecommerce framework

179 lines 8.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.performAddOperation = performAddOperation; const prompts_1 = require("@clack/prompts"); const picocolors_1 = __importDefault(require("picocolors")); const add_api_extension_1 = require("./api-extension/add-api-extension"); const add_codegen_1 = require("./codegen/add-codegen"); const add_entity_1 = require("./entity/add-entity"); const add_job_queue_1 = require("./job-queue/add-job-queue"); const create_new_plugin_1 = require("./plugin/create-new-plugin"); const add_service_1 = require("./service/add-service"); const add_ui_extensions_1 = require("./ui-extensions/add-ui-extensions"); async function performAddOperation(options) { try { if (options.plugin) { if (typeof options.plugin !== 'string' || !options.plugin.trim()) { throw new Error('Plugin name is required. Usage: vendure add -p <plugin-name>'); } await create_new_plugin_1.createNewPluginCommand.run({ name: options.plugin, config: options.config }); return { success: true, message: `Plugin "${options.plugin}" created successfully`, }; } if (options.entity) { if (typeof options.entity !== 'string' || !options.entity.trim()) { throw new Error('Entity name is required. Usage: vendure add -e <entity-name> --selected-plugin <plugin-name>'); } if (!options.selectedPlugin || typeof options.selectedPlugin !== 'string' || !options.selectedPlugin.trim()) { throw new Error('Plugin name is required when running in non-interactive mode. Usage: vendure add -e <entity-name> --selected-plugin <plugin-name>'); } await add_entity_1.addEntityCommand.run({ className: options.entity, isNonInteractive: true, config: options.config, pluginName: options.selectedPlugin, customFields: options.customFields, translatable: options.translatable, }); return { success: true, message: `Entity "${options.entity}" added successfully to plugin "${options.selectedPlugin}"`, }; } if (options.service) { if (typeof options.service !== 'string' || !options.service.trim()) { throw new Error('Service name is required. Usage: vendure add -s <service-name> --selected-plugin <plugin-name>'); } if (!options.selectedPlugin || typeof options.selectedPlugin !== 'string' || !options.selectedPlugin.trim()) { throw new Error('Plugin name is required when running in non-interactive mode. Usage: vendure add -s <service-name> --selected-plugin <plugin-name>'); } await add_service_1.addServiceCommand.run({ serviceName: options.service, isNonInteractive: true, config: options.config, pluginName: options.selectedPlugin, serviceType: options.selectedEntity ? 'entity' : options.type || 'basic', selectedEntityName: options.selectedEntity, }); return { success: true, message: `Service "${options.service}" added successfully to plugin "${options.selectedPlugin}"`, }; } if (options.jobQueue) { const pluginName = typeof options.jobQueue === 'string' ? options.jobQueue : undefined; if (!options.name || typeof options.name !== 'string' || !options.name.trim()) { throw new Error('Job queue name is required. Usage: vendure add -j [plugin-name] --name <job-name>'); } if (!options.selectedService || typeof options.selectedService !== 'string' || !options.selectedService.trim()) { throw new Error('Service name is required for job queue. Usage: vendure add -j [plugin-name] --name <job-name> --selected-service <service-name>'); } await add_job_queue_1.addJobQueueCommand.run({ isNonInteractive: true, config: options.config, pluginName, name: options.name, selectedService: options.selectedService, }); return { success: true, message: 'Job-queue feature added successfully', }; } if (options.codegen) { const pluginName = typeof options.codegen === 'string' ? options.codegen : undefined; if (typeof options.codegen === 'string' && !options.codegen.trim()) { throw new Error('Plugin name cannot be empty when specified. Usage: vendure add --codegen [plugin-name]'); } await add_codegen_1.addCodegenCommand.run({ isNonInteractive: true, config: options.config, pluginName, }); return { success: true, message: 'Codegen configuration added successfully', }; } if (options.apiExtension) { const pluginName = typeof options.apiExtension === 'string' ? options.apiExtension : undefined; const hasValidQueryName = options.queryName && typeof options.queryName === 'string' && options.queryName.trim() !== ''; const hasValidMutationName = options.mutationName && typeof options.mutationName === 'string' && options.mutationName.trim() !== ''; if (!hasValidQueryName && !hasValidMutationName) { throw new Error('At least one of query-name or mutation-name must be specified as a non-empty string. ' + 'Usage: vendure add -a [plugin-name] --query-name <name> --mutation-name <name>'); } if (typeof options.apiExtension === 'string' && !options.apiExtension.trim()) { throw new Error('Plugin name cannot be empty when specified. ' + 'Usage: vendure add -a [plugin-name] --query-name <name> --mutation-name <name>'); } await add_api_extension_1.addApiExtensionCommand.run({ isNonInteractive: true, config: options.config, pluginName, queryName: options.queryName, mutationName: options.mutationName, selectedService: options.selectedService, }); return { success: true, message: 'API extension scaffold added successfully', }; } if (options.uiExtensions) { const pluginName = typeof options.uiExtensions === 'string' ? options.uiExtensions : undefined; if (typeof options.uiExtensions === 'string' && !options.uiExtensions.trim()) { throw new Error('Plugin name cannot be empty when specified. Usage: vendure add --ui-extensions [plugin-name]'); } await add_ui_extensions_1.addUiExtensionsCommand.run({ isNonInteractive: true, config: options.config, pluginName, }); return { success: true, message: 'UI extensions added successfully', }; } return { success: false, message: 'No valid add operation specified', }; } catch (error) { if (error.message.includes('is required') || error.message.includes('cannot be empty') || error.message.includes('must be specified')) { throw error; } if (error.message.includes('Plugin name is required')) { const errorMessage = error.message; const stackLines = error.stack.split('\n'); const stackTrace = stackLines.slice(1).join('\n'); prompts_1.log.error(stackTrace); prompts_1.log.error(''); prompts_1.log.error(picocolors_1.default.red('Error:') + ' ' + String(errorMessage)); } else { prompts_1.log.error(error.message); if (error.stack) { prompts_1.log.error(error.stack); } } process.exit(1); } } //# sourceMappingURL=add-operations.js.map