(workflowId: string, outputFile?: string)
| 50 | |
| 51 | // ---------- Main export function ---------- |
| 52 | async function exportWorkflow(workflowId: string, outputFile?: string): Promise<void> { |
| 53 | try { |
| 54 | // Fetch workflow metadata |
| 55 | const [workflowData] = await db |
| 56 | .select() |
| 57 | .from(workflow) |
| 58 | .where(eq(workflow.id, workflowId)) |
| 59 | .limit(1) |
| 60 | |
| 61 | if (!workflowData) { |
| 62 | process.stderr.write(`Error: Workflow ${workflowId} not found\n`) |
| 63 | process.exit(1) |
| 64 | } |
| 65 | |
| 66 | // Load workflow from normalized tables |
| 67 | const normalizedData = await loadWorkflowFromNormalizedTables(workflowId) |
| 68 | |
| 69 | if (!normalizedData) { |
| 70 | process.stderr.write(`Error: Workflow ${workflowId} has no normalized data\n`) |
| 71 | process.exit(1) |
| 72 | } |
| 73 | |
| 74 | // Get variables in Record format (as stored in database) |
| 75 | type VariableType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'plain' |
| 76 | const workflowVariables = workflowData.variables as |
| 77 | | Record<string, { id: string; name: string; type: VariableType; value: unknown }> |
| 78 | | undefined |
| 79 | |
| 80 | // Prepare export state - match the exact format from the UI |
| 81 | const workflowState = { |
| 82 | blocks: normalizedData.blocks, |
| 83 | edges: normalizedData.edges, |
| 84 | loops: normalizedData.loops, |
| 85 | parallels: normalizedData.parallels, |
| 86 | metadata: { |
| 87 | name: workflowData.name, |
| 88 | description: workflowData.description ?? undefined, |
| 89 | exportedAt: new Date().toISOString(), |
| 90 | }, |
| 91 | variables: workflowVariables, |
| 92 | } |
| 93 | |
| 94 | // Sanitize and export - this returns { version, exportedAt, state } |
| 95 | const exportState = sanitizeForExport(workflowState) |
| 96 | const jsonString = JSON.stringify(exportState, null, 2) |
| 97 | |
| 98 | // Write to file or stdout |
| 99 | if (outputFile) { |
| 100 | writeFileSync(outputFile, jsonString, 'utf-8') |
| 101 | process.stderr.write(`Workflow exported to ${outputFile}\n`) |
| 102 | } else { |
| 103 | // Output the JSON to stdout only |
| 104 | process.stdout.write(`${jsonString}\n`) |
| 105 | } |
| 106 | } catch (error) { |
| 107 | process.stderr.write(`Error exporting workflow: ${error}\n`) |
| 108 | process.exit(1) |
| 109 | } |
no test coverage detected