import { getProjectPath } from '../../../helpers/cluserHelper'; import { getColumnsFromTasks, getTasks, INormalizedName, normalizeName, } from '../../../helpers/profileGeneratorHelper'; import inquirer from 'inquirer'; import { readVariableValue } from '../../../helpers/variablesHelper'; export async function getTaskDataFromMerge( normalizedTaskName: INormalizedName, ): Promise<{ connection: { name: string }; puller: null; // Merge doesn't involve pullers pusher: null; // Merge doesn't involve pushers table: { columns: string[] }; }> { const projectComponentPath = getProjectPath(); // Step 1: Fetch all tasks using getTasks const tasks = getTasks(projectComponentPath); if (!tasks || tasks.length === 0) { throw new Error('No tasks found to merge.'); } // Prompt user to select multiple tasks const { selectedTasks } = await inquirer.prompt([ { type: 'checkbox', name: 'selectedTasks', message: 'Select tasks to merge:', choices: tasks.map(({ taskConfig, __file_path }) => ({ name: `${readVariableValue(taskConfig.objects[0]?.name)}`, value: { taskConfig, __file_path }, })), }, ]); if (selectedTasks.length < 2) { throw new Error('You must select at least two tasks to merge.'); } // Step 2: Use the helper function to extract unique columns from the selected tasks const allColumns = getColumnsFromTasks(selectedTasks); if (allColumns.length === 0) { throw new Error('No columns found in the selected tasks.'); } // Prompt user to select columns from the combined set const { selectedColumns } = await inquirer.prompt([ { type: 'checkbox', name: 'selectedColumns', message: 'Select columns for the merged table:', choices: allColumns, }, ]); if (selectedColumns.length === 0) { throw new Error( 'You must select at least one column for the merged table.', ); } // Step 3: Prompt user for connection name const { connectionName } = await inquirer.prompt([ { type: 'input', name: 'connectionName', message: 'Enter the connection name for the merged task:', validate: (input) => input.trim() !== '' || 'Connection name cannot be empty.', }, ]); if (!connectionName.trim()) { throw new Error('Connection name is required for the merged task.'); } // Step 4: Return merged task data return { connection: { name: normalizeName(connectionName).camelCaseName }, puller: null, pusher: null, table: { columns: selectedColumns }, }; }