MCPcopy Index your code
hub / github.com/simstudioai/sim / exportWorkflow

Function exportWorkflow

apps/sim/scripts/export-workflow.ts:52–110  ·  view source on GitHub ↗
(workflowId: string, outputFile?: string)

Source from the content-addressed store, hash-verified

50
51// ---------- Main export function ----------
52async 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 }

Callers 1

export-workflow.tsFile · 0.85

Calls 4

sanitizeForExportFunction · 0.90
writeMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected