// turning a recorded flow draft into a maestro-compatible YAML file. // // this is separate from flow-session.ts, the draft store, because the two have // different reach. every `do` verb writes a candidate into the store, so the // store has to load wherever the CLI's runtime commands load, including inside // a box shell running in workerd, where the `yaml` package does not load at // all. YAML formatting and writing the file are the export step, and only // `rnx flow` and `rnx maestro` ever reach it. import { mkdirSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import yaml from 'yaml' import { validateFlowSteps } from './flow-file' import { finalizeFlowSession, readFlowSession } from './flow-session' export function exportFlowSession(outputPath?: string) { const state = readFlowSession() if (!state) return { active: false as const } const yamlText = `${yaml.stringify(state.steps).trimEnd()}\n` const issues = validateFlowSteps(state.steps) if (issues.length > 0) { return { active: true as const, valid: false as const, issues, yaml: yamlText, stepCount: state.steps.length, outputPath: null, } } let resolvedOutputPath: string | null = null if (outputPath) { resolvedOutputPath = resolve(outputPath) mkdirSync(dirname(resolvedOutputPath), { recursive: true }) writeFileSync(resolvedOutputPath, yamlText) } return { active: true as const, valid: true as const, issues: [], yaml: yamlText, stepCount: state.steps.length, outputPath: resolvedOutputPath, } } export function endFlowSession(outputPath?: string) { const result = exportFlowSession(outputPath) if (!result.active) return result finalizeFlowSession() return result }