import fs from 'fs'; import { getAssociation, getTasks, normalizeName, } from '../../../helpers/profileGeneratorHelper'; import inquirer from 'inquirer'; import { parse } from 'yaml'; import { readVariableValue } from '../../../helpers/variablesHelper'; export async function getTaskDataFromClone(normalizedTaskName: any): Promise<{ connection: { name: string }; puller: { id: string; resultKey: string } | null; pusher: { id: string } | null; table: { columns: string[] }; }> { const tasks = getTasks(); if (!tasks || tasks.length === 0) { throw new Error('No tasks found to clone.'); } const { selectedTask } = await inquirer.prompt([ { type: 'list', name: 'selectedTask', message: 'Select a task to clone:', choices: tasks.map(({ taskConfig, __file_path }) => ({ name: `${readVariableValue(taskConfig.objects[0]?.name)}`, value: { taskConfig, __file_path }, })), }, ]); const { taskConfig, __file_path } = selectedTask; const { connectionName } = await inquirer.prompt([ { type: 'input', name: 'connectionName', message: 'Enter the connection name for the cloned task:', validate: (input) => input.trim() !== '' || 'Connection name cannot be empty.', }, ]); const normalizedConnectionName = normalizeName(connectionName).camelCaseName; const tableFilePath = __file_path.replace('Task', 'Table'); if (!fs.existsSync(tableFilePath)) { throw new Error( `Table file not found for the selected task: ${tableFilePath}`, ); } const tableConfig = parse(fs.readFileSync(tableFilePath, 'utf-8')); if ( !tableConfig.tables || tableConfig.tables.length === 0 || !tableConfig.tables[0].columns ) { throw new Error("The selected task's table has no columns defined."); } const clonedColumns = Object.keys(tableConfig.tables[0].columns); const objectAssociations = getAssociation(); const associatedObjects = objectAssociations.objectAssociations[taskConfig.objects[0]?.id]; if (!associatedObjects) { throw new Error('No object associations found for the selected task.'); } return { connection: { name: normalizedConnectionName }, puller: associatedObjects.puller ? { id: associatedObjects.puller.id, resultKey: associatedObjects.puller.resultKey, } : null, pusher: associatedObjects.pusher ? { id: associatedObjects.pusher.id } : null, table: { columns: clonedColumns }, }; }