import chalk from 'chalk'; import { Command } from 'commander'; import { getGlobalConfig, HEXASYNC_CONNECTOR_CONFIG, } from '../helpers/cluserHelper'; import { parse, stringify } from 'yaml'; import fs from 'fs'; import inquirer from 'inquirer'; export function CreateConnectorCommand(): Command { const cmd = new Command('create') .alias('c') .description('Create a connector') .action(async () => { var connectorConfigPath = HEXASYNC_CONNECTOR_CONFIG; var connectorConfigs = parse(fs.readFileSync(connectorConfigPath, 'utf8')) || {}; var connectors = connectorConfigs.connectors || []; // Prompt user for connector details const answers = await inquirer.prompt([ { type: 'input', name: 'name', message: 'Enter the connector name:', validate: (input) => input.trim() !== '' || 'Name is required.', }, { type: 'input', name: 'connectorId', message: 'Enter the connector ID (UUID):', validate: (input) => /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test( input, ) || 'Please enter a valid UUID.', }, { type: 'input', name: 'fullName', message: 'Enter the full name (defaults to name):', default: (answers) => answers.name, }, { type: 'input', name: 'description', message: 'Enter the description (defaults to name):', default: (answers) => answers.name, }, { type: 'input', name: 'slug', message: 'Enter the slug (defaults to initials of name in lowercase):', default: (answers) => answers.name .split(' ') .map((word) => word[0].toLowerCase()) .join(''), }, ]); // Append the new connector to the connectors array connectors.push({ name: answers.name, fullName: answers.fullName, description: answers.description, slug: answers.slug, connectorId: answers.connectorId, }); // Save the updated connectors to the configuration file connectorConfigs.connectors = connectors; fs.writeFileSync( connectorConfigPath, stringify(connectorConfigs, { keepSourceTokens: true, lineWidth: 0 }), ); console.log( `${chalk.green('✓')} Connector ${chalk.blue(answers.name)} has been successfully created.`, ); }); return cmd; } export function ConnectorCommand(): Command { const cmd = new Command('connector') .alias('cn') .description( 'Provides a list of commands that are helpful for building connectors', ); cmd.addCommand(CreateConnectorCommand()); return cmd; }