#!/usr/bin/env node // R2 — EXTRACT : workflows JSON → src/ (resynchronisation après édition UI n8n — à éviter). // Usage : node tools/n8n-extract.mjs [fichierWorkflow] import fs from 'fs'; import path from 'path'; import { WORKFLOWS, getCode, findWorkflowByFile } from './lib-node-map.mjs'; const fileArg = process.argv.slice(2).find(a => !a.startsWith('--')); const targets = fileArg ? [findWorkflowByFile(fileArg)].filter(Boolean) : WORKFLOWS; for (const w of targets) { if (!fs.existsSync(w.workflowFile)) { console.log(`(skip ${w.id}: ${w.workflowFile} absent)`); continue; } const wf = JSON.parse(fs.readFileSync(w.workflowFile, 'utf8')); fs.mkdirSync(w.srcDir, { recursive: true }); for (const { file, node, kind } of w.nodes) { const n = wf.nodes.find(x => x.name === node); if (!n) throw new Error(`Node introuvable: ${node} (${w.workflowFile})`); const code = getCode(n, kind); fs.writeFileSync(path.join(w.srcDir, file), code); console.log(`${w.srcDir}/${file} ← "${node}" (${code.length} chars)`); } } console.log('\nExtraction OK');