import path from 'path'; import { getGlobalConfig, getProjectPath } from './cluserHelper'; import { readVariableKey, readVariableValue } from './variablesHelper'; import { parse, stringify } from 'yaml'; import chalk from 'chalk'; import fg from 'fast-glob'; import fs from 'fs'; import { Dictionary } from 'tsyringe/dist/typings/types'; import { getAssociation } from './profileGeneratorHelper'; export interface ITaskAssociation { puller: { id: string; resultKey: string }; table: { id: string }; pusher: { id: string }; } export function findTaskAssociation(taskId: string): ITaskAssociation { var cfg = getGlobalConfig(); var taskAssociations = parse(fs.readFileSync(cfg.taskAssociationsFilePath, 'utf-8')) ?.objectAssociations || {}; var result = taskAssociations[taskId] || taskAssociations[readVariableKey(taskId)]; if (!result) { console.error( `${chalk.red('✗')} Task association not found for ${chalk.red(taskId)}`, ); } return result; } /** * This function is to fix the task associations in the project if anyone setup incorrectly */ export async function fixTaskAssociations() { const projectPath = getProjectPath(); const stream = fg.stream(`${projectPath.replace(/\\/g, '/')}/**/*.yaml`, { onlyFiles: true, }); var associations: Dictionary = {}; var filesToDelete: string[] = []; for await (const s of stream) { var filePath = s as string; const fileContent = fs.readFileSync(filePath, 'utf8'); const jsonObject = parse(fileContent); if ( jsonObject.objectAssociations && typeof jsonObject.objectAssociations === 'object' && Object.keys(jsonObject.objectAssociations).length > 0 ) { if (filePath.replace(projectPath, '') !== '/ObjectAssociations.yaml') { console.log( `${chalk.green('✓')} Found Task Associations in ${chalk.gray(filePath.replace(projectPath, ''))}`, ); filesToDelete.push(filePath); } associations = { ...associations, ...jsonObject.objectAssociations }; } } for (const file of filesToDelete) { const fileContent = fs.readFileSync(file, 'utf8'); const jsonObject = parse(fileContent); if (Object.keys(jsonObject).length === 1 && jsonObject.objectAssociations) { // delete the file console.log( `${chalk.red('✗')} Deleting Task Associations in ${chalk.gray(file.replace(projectPath, ''))}`, ); fs.unlinkSync(file); } else { // remove the objectAssociations key and write back to file delete jsonObject.objectAssociations; fs.writeFileSync( file, stringify(jsonObject, { keepSourceTokens: true, lineWidth: 0 }), ); console.log( `${chalk.yellow('!')} Removed objectAssociations key from ${chalk.gray(file.replace(projectPath, ''))}`, ); } } if (filesToDelete.length === 0) { console.log( `${chalk.green('✓')} No incorrect Task Associations found in this project`, ); return; } console.log( `${chalk.green('✓')} Writting Task Associations to ${chalk.green(path.join(projectPath, 'ObjectAssociations.yaml'))}`, ); fs.writeFileSync( path.join(projectPath, 'ObjectAssociations.yaml'), stringify( { objectAssociations: associations }, { keepSourceTokens: true, lineWidth: 0 }, ), ); } export function saveTaskAssociations( taskId: string, association: ITaskAssociation, ) { const projectPath = getProjectPath(); const filePath = path.join(projectPath, 'ObjectAssociations.yaml'); var associations = getAssociation(); associations.objectAssociations = associations.objectAssociations || {}; associations.objectAssociations[readVariableKey(taskId)] = association; fs.writeFileSync( filePath, stringify( { objectAssociations: associations.objectAssociations }, { keepSourceTokens: true, lineWidth: 0 }, ), ); }