import chalk from 'chalk'; import { Command } from 'commander'; import inquirer from 'inquirer'; import { createStep, STEP_STATES, STEP_TYPES } from '../helpers/stepHelper'; import { findEntityByIdOrName, getEntityContentsProperty, saveEntity, searchEntitiesByName, searchEntityByName, } from '../helpers/entityHelper'; export function getStepStateName( state: 'before' | 'current' | 'after', entityType: 'puller' | 'pusher', ): string { var actionName = entityType === 'puller' ? 'Pull' : 'Push'; var result = `${state !== 'current' ? state : ''}${actionName}Steps`; // convert the CamelCase to camelCase result = result.charAt(0).toLowerCase() + result.slice(1); return result; } export async function askForEntityName( entityType: 'puller' | 'pusher' | 'webhook', ): Promise { var entityName = ''; while (!entityName) { const { entityNameInput } = await inquirer.prompt([ { type: 'input', name: 'entityNameInput', message: `Please type the ${chalk.yellow('name')} of the ${chalk.yellow(entityType)}. HexaSync will search for it:`, }, ]); entityName = entityNameInput; } return entityName; } export async function askForStepName(name: string): Promise { var stepName = name; while (!stepName) { const { stepNameInput } = await inquirer.prompt([ { type: 'input', name: 'stepNameInput', message: `Please name the step:`, default: name, }, ]); stepName = stepNameInput; } return stepName; } export async function askForStepType(): Promise { var stepType = ''; while (!stepType) { const { stepTypeInput } = await inquirer.prompt([ { type: 'list', name: 'stepTypeInput', message: `Please select the ${chalk.yellow('type')} of ${chalk.yellow('step')}:`, choices: STEP_TYPES.map((type, i) => ({ name: ` ${i + 1}. ${type}`, value: type, })), }, ]); stepType = stepTypeInput; } return stepType; } export async function askForStepState( entityType: 'puller' | 'pusher' | 'webhook', ): Promise { var stepState = ''; if (['puller', 'pusher'].includes(entityType)) { while (!stepState) { const { stepStateInput } = await inquirer.prompt([ { type: 'list', name: 'stepStateInput', message: `Please select the ${chalk.yellow('state')} of ${chalk.yellow('step')}:`, choices: STEP_STATES.map((state, i) => ({ name: ` ${i + 1}. ${state} ${entityType === 'puller' ? 'Pull' : 'Push'} Steps`, value: getStepStateName( state.toLowerCase() as 'before' | 'current' | 'after', entityType as 'puller' | 'pusher', ), })), }, ]); stepState = stepStateInput; } } else { stepState = 'pullSteps'; } return stepState; } export function AddStepCommand( entityType: 'puller' | 'pusher' | 'webhook', ): Command { const cmd = new Command('generate') .alias('g') .description('Generates a Step') .option('-n, --name ', 'Name of the step') .option( '-x, --sample ', 'The sample of CURL request or path to a file containing CURL request', ); cmd.action(async ({ name, sample }) => { console.log(`${chalk.green('✔')} Generating a ${entityType} step.`); var entityName = await askForEntityName(entityType); var searchedEntity = await searchEntityByName( entityName, entityType as any, ); if (!searchedEntity?.entityId) { return; } var stepName = await askForStepName(name); var stepType = await askForStepType(); var stepState = await askForStepState(entityType); console.log( `${chalk.green('✔')} Generating step named ${chalk.yellow(stepName)}, type ${chalk.yellow(stepType)}, state ${chalk.yellow(stepState)} for ${chalk.yellow(entityName)} ${chalk.yellow(entityType)}.`, ); var createdStep: any = null; var steps = searchedEntity.content[stepState] || []; while (!createdStep) { createdStep = await createStep(stepName, stepType, { sample }); var exists = steps.find((step: any) => step.key === createdStep.key); if (exists) { console.log( `${chalk.red('✘')} Step ${chalk.yellow(stepName)} already exists. Please choose another name.`, ); stepName = ''; stepName = await askForStepName(stepName); createdStep = null; } } console.log(`${chalk.green('✔')} Step ${chalk.yellow(stepName)} created.`); console.log( `${chalk.green('✔')} Adding step ${chalk.yellow(stepName)} to ${chalk.yellow(entityName)} ${chalk.yellow(entityType)}.`, ); createdStep.rootStep = steps.length === 0; searchedEntity.content[stepState] = [...steps, createdStep]; console.log( `${chalk.green('✔')} Step ${chalk.yellow(stepName)} added to ${chalk.yellow(entityName)} ${chalk.yellow(entityType)}.`, ); saveEntity( searchedEntity.content, entityType as any, searchedEntity.filePath, ); }); return cmd; } export function StepCommands( entityType: 'puller' | 'pusher' | 'webhook', ): Command { const cmd = new Command('steps') .alias('s') .description('Steps related commands.'); cmd.addCommand(AddStepCommand(entityType)); return cmd; }