import chalk from 'chalk'; import { getGlobalConfig, getProjectPath, loadVariables } from './cluserHelper'; import { parse, stringify } from 'yaml'; import fs from 'fs'; import fg from 'fast-glob'; import path from 'path'; import { createVariable, readVariableKey, readVariableValue, } from './variablesHelper'; import { Dictionary } from 'tsyringe/dist/typings/types'; import { IEntityInfo } from './@types/IEntityInfo'; import { IEntitySearchResult } from './@types/IEntitySearchResult'; import { findTaskAssociation } from './taskAssociationHelper'; import inquirer from 'inquirer'; import { normalizeName } from './profileGeneratorHelper'; import { loadCached } from './entityHelper'; import { v4 as uuid } from 'uuid'; export interface IPusherTemplate { id: string; name: string; description?: string; options: string[]; beforePushSteps: string[]; pushSteps: string[]; afterPushSteps: string[]; responseFilters: Dictionary>; } export const DefaultPusherScribanResponseFilter = { id: { expression: `{{ if (pusher.current.CREATE?.id | string.size) > 0 pusher.current.CREATE.id else if (pusher.current.UPDATE?.id | string.size) > 0 pusher.current.UPDATE.id else if (pusher.current.REMOVE?.id | string.size) > 0 pusher.current.REMOVE.id else item.__destination_id end }}`, }, isFailed: { expression: `{{ if (pusher.current.CREATE?.isFailed | string.downcase) == "true" || (pusher.current.CREATE?.isFailed | string.downcase) == "1" true else (pusher.current.UPDATE?.isFailed | string.downcase) == "true" || (pusher.current.UPDATE?.isFailed | string.downcase) == "1" true else (pusher.current.REMOVE?.isFailed | string.downcase) == "true" || (pusher.current.REMOVE?.isFailed | string.downcase) == "1" true else false end }}`, }, isSuccess: { expression: `{{ if (pusher.current.CREATE?.id | string.size) > 0 true else if (pusher.current.UPDATE?.id | string.size) > 0 true else if (pusher.current.REMOVE?.id | string.size) > 0 true else if (item.__destination_id | string.size) > 0 true else false end }}`, }, message: { expression: `{{ [pusher.current.CREATE?.message, pusher.current.UPDATE?.message, pusher.current.REMOVE?.message] | array.compact | array.join "\r\n" }}`, }, }; export async function createPusher( entityName: string, connectionId: string | null | undefined, ): Promise { const entityType = 'pusher'; const entityTypeDisplayName = normalizeName(entityType); if (!entityName) { // 1. Use inquirer to prompt the user for the task name let { name } = await inquirer.prompt([ { type: 'input', name: 'name', message: `ⓘ Enter the ${entityTypeDisplayName.displayName} name:`, validate: (input) => input.trim() !== '' || chalk.red(`✗ ${entityTypeDisplayName.displayName} name is required.`), }, ]); if (!name) { console.error( chalk.red(`✗ ${entityTypeDisplayName.displayName} name is required.`), ); return null; } entityName = name; } if (!connectionId) { } var connections = await loadCached('connection'); var connection = connections.find( (connection) => connection.entityId === connectionId, ); let namePrefix: string = 'HexaSync'; if (!!connection?.entityName) { namePrefix = `${connection.entityName}`; } var normalizedNamePrefix = normalizeName(namePrefix); var normalizedEntityName = normalizeName(entityName); var normalizedConnectionName = normalizeName(connection?.entityName || ''); var baseNamePrefix = normalizedNamePrefix.underscoreDisplayName ? `${normalizedNamePrefix.underscoreDisplayName}__` : ''; var fileName = `${baseNamePrefix}${normalizedEntityName.underscoreDisplayName}_${entityTypeDisplayName.underscoreDisplayName}.yaml`; var targetPath = path.join( getProjectPath(), 'pushers', normalizedConnectionName.dashedSlug, ); if (!fs.existsSync(targetPath)) { fs.mkdirSync(targetPath, { recursive: true }); } var filePath = path.join(targetPath, fileName); if (fs.existsSync(filePath)) { console.error( chalk.red( `✗ ${entityTypeDisplayName.displayName} file already exists: ${chalk.white(filePath)}`, ), ); return null; } var templateIdKey = `${baseNamePrefix}${normalizedEntityName.underscoreDisplayName}_${entityTypeDisplayName.underscoreDisplayName}_Id`; var templateNameKey = `${baseNamePrefix}${normalizedEntityName.underscoreDisplayName}_${entityTypeDisplayName.underscoreDisplayName}_Name`; var templateName = `[${normalizedNamePrefix.displayName}] ${normalizedEntityName.displayName} ${entityTypeDisplayName.displayName}`; var template: IPusherTemplate = { id: createVariable(templateIdKey, uuid()), name: createVariable(templateNameKey, templateName), description: ``, options: [], beforePushSteps: [], pushSteps: [], afterPushSteps: [], responseFilters: { scriban: { ...DefaultPusherScribanResponseFilter }, }, }; fs.writeFileSync( filePath, stringify( { pushers: [template] }, { keepSourceTokens: true, lineWidth: 0 }, ), ); console.log( chalk.green( `✓ Empty ${entityTypeDisplayName.displayName} file created: ${chalk.white(filePath)}`, ), ); return template; }