import { Command } from 'commander'; import { RegenerateTaskCommand } from './regenerateTaskCommand'; import { LinkTaskCommand } from './linkTaskCommand'; import { setEntityColumnsKeys, setEntityColumnsPosition, setEntityColumnsSourceId, setEntityColumnsTrackForChanges, setEntityColumnsVisibility, } from '../../helpers/entityHelper'; import { showTaskFiles, TaskFilesCommand } from './filesCommand'; import chalk from 'chalk'; import inquirer from 'inquirer'; import { createPuller } from '../../helpers/pullerHelper'; import { createPusher } from '../../helpers/pusherHelper'; import { createTable } from '../../helpers/tableHelper'; import { createTask, associateTask, searchAndAssociateEntityToTask, } from '../../helpers/taskHelper'; import { readVariableValue } from '../../helpers/variablesHelper'; import { getProjectPath, useProfile } from '../../helpers/cluserHelper'; import { switchProfile } from '../profile/switchProfileCommand'; import { GenerateColumnsCommand, AddColumnsCommand, RemoveColumnsCommand, } from '../tables/tableCommand'; import { composeProfile } from '../compose/composeCommand'; import { ColumnsCommand } from './columnsCommand'; export function SetKeysCommand(): Command { const cmd = new Command('set-keys'); cmd .alias('sk') .description('Set KEYS for the related table of the report.') .option('-i, --id ', 'Search the task by id') .option('-n, --name ', 'Search the task by name') .action(async ({ id, name }: { id?: string; name?: string }) => { await setEntityColumnsKeys(id, name, 'task'); }); return cmd; } export function SetSourceIdCommand(): Command { const cmd = new Command('set-source-id'); cmd .alias('ss') .description('Set SOURCE_ID for the related table of the report.') .option('-i, --id ', 'Search the task by id') .option('-n, --name ', 'Search the task by name') .action(async ({ id, name }: { id?: string; name?: string }) => { await setEntityColumnsSourceId(id, name, 'task'); }); return cmd; } export function TrackChangesCommand(): Command { const cmd = new Command('track-changes'); cmd .alias('tc') .description('Track changes for table columns.') .option('-i, --id ', 'Search the task by id') .option('-n, --name ', 'Search the task by name') .action(async ({ id, name }: { id?: string; name?: string }) => { await setEntityColumnsTrackForChanges(id, name, 'task'); }); return cmd; } export function SetVisibilityCommand(): Command { const cmd = new Command('set-visibility'); cmd .alias('sv') .description("Set Visibility of columns in a Task's Table.") .option('-i, --id ', 'Search the task by id') .option('-n, --name ', 'Search the task by name') .action(async ({ id, name }: { id?: string; name?: string }) => { await setEntityColumnsVisibility(id, name, 'task'); }); return cmd; } export function SetPositionCommand(): Command { const cmd = new Command('set-position'); cmd .alias('sp') .description("Set Position of columns in a Task's Table.") .option('-i, --id ', 'Search the task by id') .option('-n, --name ', 'Search the task by name') .action(async ({ id, name }: { id?: string; name?: string }) => { await setEntityColumnsPosition(id, name, 'task'); }); return cmd; } export function GenerateTaskCommand(): Command { const cmd = new Command('generate') .alias('g') .description('Generates a HexaSync Task') .option('-n, --task-name ', 'Specify the name for the task') .option( '-t, --type ', 'Specify the task type (SYNC, REFERENCE, REPORT, MAPPING)', 'SYNC', ) .action(async ({ taskName, type }) => { const validTypes = ['SYNC', 'REFERENCE', 'REPORT', 'MAPPING'] as const; const normalizedType = type?.toUpperCase(); if (!validTypes.includes(normalizedType)) { console.error( chalk.red( `✗ Invalid task type '${type}'. Valid types: ${validTypes.join(', ')}`, ), ); return; } var createdTask = await createTask(taskName, '', normalizedType); if (!createdTask?.filePath) { return; } var createdTable = await createTable( taskName, createdTask.taskNamePrefix, ); if (!createdTable?.filePath) { return; } associateTask(createdTask.content.id, 'table', createdTable.content.id); // 5. Ask if user would like to associate with a puller or create a new one while (true) { const { associatePullerOption } = await inquirer.prompt([ { type: 'list', name: 'associatePullerOption', message: 'ⓘ What would you like to do regarding the puller?', choices: [ { name: 'Associate with an existing puller', value: 'associate' }, { name: 'Create a new puller', value: 'create' }, { name: 'Skip', value: 'skip' }, ], }, ]); if (associatePullerOption === 'associate') { var searchedPuller = await searchAndAssociateEntityToTask( createdTask.content.id, 'puller', '', { resultKey: 'items' }, ); if (!!searchedPuller?.entityId) { break; } } else if (associatePullerOption === 'create') { var createdPuller = await createPuller( taskName, createdTask.pullConnection?.entityId, ); if (!!createdPuller?.id) { associateTask(createdTask.content.id, 'puller', createdPuller.id); } break; } else { console.log( chalk.yellow('⚠︎ You chose to skip associating with a puller.'), ); break; } } while (true) { const { associatePusherOption } = await inquirer.prompt([ { type: 'list', name: 'associatePusherOption', message: 'ⓘ What would you like to do regarding the pusher?', choices: [ { name: 'Associate with an existing pusher', value: 'associate' }, { name: 'Create a new pusher', value: 'create' }, { name: 'Skip', value: 'skip' }, ], }, ]); if (associatePusherOption === 'associate') { var searchedPusher = await searchAndAssociateEntityToTask( createdTask.content.id, 'pusher', '', ); if (!!searchedPusher?.entityId) { break; } } else if (associatePusherOption === 'create') { var createdPusher = await createPusher( taskName, createdTask.pushConnection?.entityId, ); if (!!createdPusher?.id) { associateTask(createdTask.content.id, 'pusher', createdPusher.id); break; } } else { console.log( chalk.yellow('⚠︎ You chose to skip associating with a puller.'), ); break; } } // Shows the related Task's files await switchProfile(getProjectPath(), {}); await composeProfile(); await showTaskFiles(createdTask.content.id); console.log( chalk.gray( `\n⚠︎ Please ${chalk.yellow('update')} the ${chalk.green('task')} ${chalk.yellow('name')}, ${chalk.yellow('valueTableName')}, and ${chalk.yellow('priority')} according to your needs.`, ), ); console.log( ` - ${chalk.white('name')}: ${chalk.yellow(readVariableValue(createdTask?.content?.name!))}`, ); console.log( ` - ${chalk.white('valueTableName')}: ${chalk.yellow(readVariableValue(createdTask?.content?.valueTableName!))}`, ); console.log( ` - ${chalk.white('priority')}: ${chalk.yellow(readVariableValue(createdTask?.content?.priority?.toString()!))}`, ); console.log( chalk.gray( `\r⚠︎ Please ${chalk.yellow('update')} the ${chalk.green('table')} ${chalk.yellow('columns')}, and ${chalk.yellow('indexGroups')} according to your needs.`, ), ); console.log( chalk.gray( `\r⚠︎ Please ${chalk.yellow('update')} the ${chalk.green('puller')} and ${chalk.green('pusher')} according to your needs.`, ), ); // Ask if user wants to manage table columns const { columnsOption } = await inquirer.prompt([ { type: 'list', name: 'columnsOption', message: 'ⓘ Would you like to manage table columns?', choices: [ { name: 'Generate columns from puller', value: 'generate' }, { name: 'Add columns manually', value: 'add' }, { name: 'Skip', value: 'skip' }, ], }, ]); if (columnsOption === 'generate') { const generateCmd = GenerateColumnsCommand('task'); await generateCmd.parseAsync( ['--name', String(readVariableValue(createdTask.content.name))], { from: 'user' }, ); } else if (columnsOption === 'add') { const addCmd = AddColumnsCommand('task'); await addCmd.parseAsync( ['--name', String(readVariableValue(createdTask.content.name))], { from: 'user' }, ); } else { console.log( chalk.yellow('⚠︎ You chose to skip managing table columns.'), ); } }); return cmd; } export function TaskCommand(): Command { const cmd = new Command('task'); cmd.alias('ta').description('Task related commands.'); cmd.addCommand(GenerateTaskCommand()); cmd.addCommand(RegenerateTaskCommand()); cmd.addCommand(LinkTaskCommand()); cmd.addCommand(SetSourceIdCommand()); cmd.addCommand(SetKeysCommand()); cmd.addCommand(TrackChangesCommand()); cmd.addCommand(SetVisibilityCommand()); cmd.addCommand(SetPositionCommand()); cmd.addCommand(TaskFilesCommand()); cmd.addCommand(ColumnsCommand()); return cmd; }