import { Command } from 'commander'; import fs from 'fs'; import path from 'path'; import { INormalizedName, normalizeName, searchTask, } from '../../../../helpers/profileGeneratorHelper'; import { getProjectPath } from '../../../../helpers/cluserHelper'; import { dependencyYaml } from '../../../../components/dependencyTemplate'; import inquirer from 'inquirer'; import { v4 } from 'uuid'; import { mergeVariables, readVariableValue, } from '../../../../helpers/variablesHelper'; import { findEntitiesByIdOrName, findEntityByIdOrName, } from '../../../../helpers/entityHelper'; export function extractTaskName(fullName: string): INormalizedName { const trimmedName = fullName .replace(/\s*\[([^\]]+)\]\s*/g, '') .replace(/\s*\(([^\)]+)\)\s*/g, '') .replaceAll(' Task', ''); return normalizeName(trimmedName); } export function GenerateDependencyCommand(): Command { const cmd = new Command('generate') .alias('g') .description('Generates a HexaSync Dependency') .option('-f, --from-task ', 'Search for dependee') .option('-t, --to-task ', 'Search for dependant') .option( '-c, --category ', 'The folder path to store the dependencies', ) .action(async ({ fromTask, toTask, category }) => { const fromTaskYml = await findEntityByIdOrName('', fromTask, 'task'); if (!fromTaskYml) { console.error( `There is no such task ${fromTask}, generting dummy id for "From Task"`, ); } const fromTaskNormalizedName = fromTaskYml?.entityName ? extractTaskName( readVariableValue(fromTaskYml?.entityName || '')?.toString() || '', ) : null; const toTaskYml = await findEntityByIdOrName('', toTask, 'task'); if (!toTaskYml) { console.error( `There is no such task ${toTask}, generting dummy id for "To Task"`, ); } const toTaskNormalizedName = toTaskYml?.entityName ? extractTaskName( readVariableValue(toTaskYml?.entityName || '')?.toString() || '', ) : null; let fileName = fromTaskNormalizedName && toTaskNormalizedName ? `${fromTaskNormalizedName.camelCaseName}_${toTaskNormalizedName.camelCaseName}_Dependency` : null; // should enter the name let dependencyName = fromTaskYml && toTaskYml ? `${readVariableValue( fromTaskYml.entityName || '', )} depends on ${readVariableValue(toTaskYml.entityName || '')}` : null; let idKey = fromTaskYml && toTaskYml ? `${readVariableValue(fromTaskYml.entityName || '')} ${readVariableValue( toTaskYml.entityName || '', )} Dependency` .replace(/[^a-zA-Z\s]+/g, '') .replace(/\s+/g, '_') : null; let dependencyKey = toTaskNormalizedName ? `${toTaskNormalizedName.underscoreSlug.toUpperCase()}` : null; const answer = await inquirer.prompt<{ value: string }>([ { type: 'input', name: 'value', message: `Please enter the name of the dependency:`, default: dependencyName, }, ]); const newName = answer.value; if (!newName) { console.error('Please enter a valid input'); return; } if (newName !== dependencyName || !dependencyName) { const newNormalizedName = normalizeName(newName); fileName = `${newNormalizedName.camelCaseName}_Dependency`; dependencyName = newNormalizedName.displayName; idKey = `${newNormalizedName.camelCaseName} Dependency` .replace(/[^a-zA-Z\s]+/g, '') .replace(/\s+/g, '_'); dependencyKey = newNormalizedName.underscoreSlug.toUpperCase(); } let variables = [ { field: 'id', key: `**${idKey}_Id**`, value: v4(), shouldMerge: true, }, { field: 'name', key: `**${idKey}_Name**`, value: dependencyName, shouldMerge: true, }, { field: 'dependencyKey', key: `**${idKey}_Key**`, value: dependencyKey, shouldMerge: true, }, { field: 'objectId', key: fromTaskYml?.content?.id || fromTaskYml?.entityId || '', value: '', shouldMerge: false, }, { field: 'dependedOn', key: toTaskYml?.content?.id || toTaskYml?.entityId || '', value: '', shouldMerge: false, }, { field: 'fromTable', key: fromTaskYml?.content?.valueTableName || '', value: '', shouldMerge: false, }, { field: 'toTable', key: toTaskYml?.content?.valueTableName || '', value: '', shouldMerge: false, }, ]; let dependencyYml = dependencyYaml(); for (var i = 0; i < variables.length; i++) { dependencyYml = dependencyYml.replaceAll( `||VARIABLE_KEY_${variables[i].field}||`, variables[i].key, ); } var mergableVariables = variables .filter((v) => v.shouldMerge) .map((v) => { return { field: v.field, key: v.key, value: v.value }; }); const componentsPath = getProjectPath(); const categoryPath = path.join(componentsPath, 'dependencies', category); if (!fs.existsSync(categoryPath)) { fs.mkdirSync(categoryPath, { recursive: true }); } const dependencyFilePath = path.join(categoryPath, `${fileName}.yaml`); if (fs.existsSync(dependencyFilePath)) { console.error(`There is a file exists in ${dependencyFilePath}`); return; } mergeVariables(mergableVariables); fs.writeFileSync(dependencyFilePath, dependencyYml); console.log(`Done. Remember to edit your script to match the business requirement.`); }); return cmd; }