import { Command } from 'commander'; import { getAssociation, searchPuller, searchPusher, searchTable, searchTask, } from '../../helpers/profileGeneratorHelper'; import fs from 'fs'; import { stringify } from 'yaml'; import inquirer from 'inquirer'; async function chooseOneString(d: string[]): Promise { if (!d || !d.length) { throw new Error(`Looks like the puller does not have the responseFilters`); } if (d.length === 1) { return d[0]; } const answer = await inquirer.prompt<{ value: string }>([ { type: 'list', name: 'value', message: 'Please select one of the response filters below:', choices: d, }, ]); return answer.value; } export function LinkTaskCommand(): Command { const cmd = new Command('link'); cmd .alias('l') .description('Link a task to puller, pusher, or table.') .option( '-n, --name ', "Search task by name, e.g.: product, 'sales order', 'image'", ) .option('-t, --type ', 'Search puller, pusher, or table') .action(async ({ name, type }) => { const task = await searchTask(name); const associations = getAssociation(); let taskAssociation = associations.objectAssociations[task.id] || {}; switch (type) { case 'puller': const puller = await searchPuller(name); taskAssociation = { ...taskAssociation, puller: { id: puller.id, resultKey: await chooseOneString( Object.keys(puller.responseFilters || []).filter( (t) => t !== 'hasNextPage', ), ), }, }; break; case 'pusher': const pusher = await searchPusher(name); taskAssociation = { ...taskAssociation, pusher: { id: pusher.id, }, }; break; case 'table': const table = await searchTable(name); taskAssociation = { ...taskAssociation, table: { id: table.id, }, }; break; default: throw new Error(`The type ${type} is not supported`); } associations.objectAssociations[task.id] = taskAssociation; const filePath = associations.__file_path; delete associations.__file_path; fs.writeFileSync( filePath, stringify(associations, { keepSourceTokens: true, lineWidth: 0 }), ); }); return cmd; }