import { readFileSync, writeFileSync, existsSync, readdirSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; const here = path.dirname(fileURLToPath(import.meta.url)); const ffiPath = path.resolve(here, '../../fluxa-core/src/ffi.rs'); const outPath = path.resolve(here, '../src/core/coreMethods.ts'); const routesPath = path.join(path.dirname(ffiPath), 'ffi'); if (!existsSync(ffiPath)) { console.error(`gen-core-methods: ${ffiPath} not found (fluxa-core must be checked out next to fluxa-desktop, same as the Cargo path dependency)`); process.exit(1); } const routeFiles = existsSync(routesPath) ? readdirSync(routesPath) .filter((name) => name.endsWith('_routes.rs')) .map((name) => path.join(routesPath, name)) : []; const methods = [ffiPath, ...routeFiles].flatMap((sourcePath) => [...readFileSync(sourcePath, 'utf8').matchAll(/^ {8}"([A-Za-z.]+)" =>/gm)].map((m) => m[1]), ); const unique = [...new Set(methods)].sort(); if (unique.length !== methods.length) { const dupes = methods.filter((m, i) => methods.indexOf(m) !== i); console.error(`gen-core-methods: duplicate method arms in ffi.rs: ${[...new Set(dupes)].join(', ')}`); process.exit(1); } if (unique.length === 0) { console.error('gen-core-methods: extracted zero methods from ffi.rs, extraction pattern is broken'); process.exit(1); } const body = `export const CORE_METHODS = [ ${unique.map((m) => ` '${m}',`).join('\n')} ] as const; export type CoreMethod = (typeof CORE_METHODS)[number]; `; const header = `// Generated by scripts/gen-core-methods.mjs from fluxa-core/src/ffi.rs — do not edit.\n`; const next = header + body; const current = existsSync(outPath) ? readFileSync(outPath, 'utf8') : ''; if (current !== next) { writeFileSync(outPath, next); console.log(`gen-core-methods: wrote ${unique.length} methods to src/core/coreMethods.ts`); }