import { Command } from 'commander'; import { extractStepResponseFields, findPuller, findTable, getAssociation, searchComponentByName, searchComponents, } from '../../helpers/profileGeneratorHelper'; import Fuse from 'fuse.js'; import inquirer from 'inquirer'; import { stringify } from 'yaml'; import fs from 'fs'; export async function findTask(name: string) { return searchComponentByName( name, 'Task', 'task', [ { name: 'name', weight: 2 }, { name: 'description', weight: 3 }, ], (f: { objects: any }) => f.objects, ); } export function RegenerateTaskCommand(): Command { const cmd = new Command('regenerate'); cmd .alias('rg') .description('Regenerate task columns.') .option('-n, --name ', 'Search task by name') .action(async ({ name }) => { const task = await findTask(name); const associations = getAssociation(); const taskAssociation = associations.objectAssociations[task.id]; const resultKey = taskAssociation?.puller?.resultKey; const puller = findPuller(taskAssociation?.puller?.id); let primaryStep = puller.pullSteps.find( (x: any) => x.key === 'PULL_DATA', ); if (!primaryStep) { throw new Error('The PULL_DATA step is empty'); } if (!primaryStep.connectorId) { throw new Error('The PULL_DATA step has no connection defined.'); } const responseFields = extractStepResponseFields( resultKey, primaryStep?.displayType, primaryStep?.data, ); const table = findTable(taskAssociation?.table?.id); let columns = {}; for (var i = 0; i < responseFields.length; i++) { columns[responseFields[i]] = (table?.columns && table?.columns[responseFields[i]]) || { indexGroups: [] }; } table.columns = { ...columns }; const filePath = table.__file_path; if (!filePath) { throw new Error(`Could not find table path.`); } delete table.__file_path; delete table.__real_name; fs.writeFileSync( filePath, stringify( { tables: [{ ...table }] }, { keepSourceTokens: true, lineWidth: 0 }, ), ); console.log( `Done. Please don't forget to double check again and fill the indexGroups.`, ); }); return cmd; }