#!/usr/bin/env node // R2 — BUILD : src/ → workflows JSON (src = SOURCE DE VÉRITÉ, src gagne). // Usage : node tools/n8n-build.mjs [--check] [fichierWorkflow] // --check : vérifie la synchro sans écrire (exit 1 si désynchronisé) // fichierWorkflow : limite à un workflow (sinon tous ceux du node-map) import fs from 'fs'; import path from 'path'; import { WORKFLOWS, getCode, setCode, findWorkflowByFile } from './lib-node-map.mjs'; const CHECK = process.argv.includes('--check'); const fileArg = process.argv.slice(2).find(a => !a.startsWith('--')); const targets = fileArg ? [findWorkflowByFile(fileArg)].filter(Boolean) : WORKFLOWS; if (fileArg && !targets.length) { console.log(`(${fileArg} non enregistré dans le node-map : rien à builder)`); process.exit(0); } let totalChanges = 0; 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')); let changes = 0; for (const { file, node, kind } of w.nodes) { const srcPath = path.join(w.srcDir, file); if (!fs.existsSync(srcPath)) throw new Error(`Fichier source manquant: ${srcPath}`); const srcCode = fs.readFileSync(srcPath, 'utf8'); const n = wf.nodes.find(x => x.name === node); if (!n) throw new Error(`Node introuvable dans ${w.workflowFile}: ${node}`); if (getCode(n, kind) !== srcCode) { changes++; console.log(`${CHECK ? 'DÉSYNCHRONISÉ' : 'inject'}: ${w.srcDir}/${file} → "${node}"`); if (!CHECK) setCode(n, kind, srcCode); } } if (!CHECK && changes) { fs.writeFileSync(w.workflowFile, JSON.stringify(wf, null, 2) + '\n'); } totalChanges += changes; console.log(`${w.id}: ${changes ? changes + ' node(s) ' + (CHECK ? 'désynchronisé(s)' : 'injecté(s)') : 'synchronisé'}`); } if (CHECK) process.exit(totalChanges ? 1 : 0);