import { stringify, parse } from 'yaml'; import { generateAssociation, getAssociation, searchComponents, searchTasks, } from '../../../helpers/profileGeneratorHelper'; import fs from 'fs'; import inquirer from 'inquirer'; export async function includeTask( componentPath: string, entityName: string, ): Promise<{ filePaths: string[]; associations: | { taskId: string; pullerId: string; tableId: string; resultKey: string; }[] | null; } | null> { console.log(`Include a task in ${componentPath}`); const tasks = await searchTasks(entityName, componentPath); if (!tasks.length) { console.error(`There is no such task ${entityName} in ${componentPath}`); return null; } const filePaths: string[] = []; const associations: { taskId: string; pullerId: string; tableId: string; resultKey: string; }[] = []; for (const taskYml of tasks) { console.log( `Found task ${taskYml.__real_name || entityName} in ${componentPath} with id ${taskYml?.id}`, ); const taskFilePath = taskYml.__file_path; const tableFilePath = taskYml.__file_path.replace( /\_Task\.yaml$/g, '_Table.yaml', ); if (!fs.existsSync(tableFilePath)) { console.error( `Could not find related table at ${tableFilePath} in ${componentPath}`, ); continue; } const tableYmlStr = fs.readFileSync(tableFilePath, 'utf8') as string; console.log(`Found related table at ${tableFilePath} in ${componentPath}`); let tableYml: any; try { tableYml = parse(tableYmlStr); } catch (e: any) { console.error( `Could not parse related table YAML at ${tableFilePath} in ${componentPath}: ${e?.message || e}`, ); continue; } if (!Array.isArray(tableYml?.tables) || !tableYml.tables[0]?.id) { console.error( `Invalid related table YAML at ${tableFilePath} in ${componentPath}: missing tables[0].id`, ); continue; } let pullerAssociation: any = null; let pullerFilePath = ''; const association = getAssociation(componentPath); const taskAssociation = association?.objectAssociations[taskYml.id]; if (taskAssociation) { const pullerYml = searchComponents('Puller', componentPath).find( (p) => p.pullers && p.pullers.length && p.pullers[0].id === taskAssociation?.puller?.id, ); if (pullerYml) { pullerAssociation = { id: taskAssociation?.puller?.id, resultKey: taskAssociation?.puller?.resultKey, }; pullerFilePath = pullerYml.__file_path; } } filePaths.push(taskFilePath, tableFilePath); if (pullerFilePath) { console.log(`Would you like to clone the task only?`); while (true) { const answer = await inquirer.prompt<{ value: string }>([ { type: 'input', name: 'value', message: `Answer (y/n):`, }, ]); if (['y', 'n'].indexOf(answer.value || '') === -1) { console.log(`Please input a valid value y/n`); continue; } if (answer.value === 'n') { filePaths.push(pullerFilePath); } break; } } associations.push({ taskId: taskYml.id, tableId: tableYml.tables[0]?.id, pullerId: pullerAssociation?.id, resultKey: pullerAssociation?.resultKey, }); } const distinctFilePaths = filePaths.filter((v, i, a) => a.indexOf(v) === i); return { filePaths: distinctFilePaths, associations: associations.length ? associations : null, }; }