import { spawnSync } from 'node:child_process' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const root = resolve(dirname(fileURLToPath(import.meta.url)), '..') const localOracle = resolve(root, '.cache/freecad/install-native/bin/FreeCADCmd') const candidates = process.env.FREECAD_CMD ? [process.env.FREECAD_CMD] : [localOracle, 'FreeCADCmd', 'freecadcmd'] let command = null for (const candidate of candidates) { const probe = spawnSync(candidate, ['--version'], { encoding: 'utf8', timeout: 15000 }) if (!probe.error || probe.error.code !== 'ENOENT') { command = candidate; break } } if (!command) throw new Error('FreeCADCmd 1.1.1 is unavailable. Set FREECAD_CMD to the locked oracle executable.') const execution = spawnSync(command, [resolve(root, 'scripts/freecad-reference-probe.py')], { cwd: root, encoding: 'utf8', timeout: 120000, maxBuffer: 20 * 1024 * 1024, env: { ...process.env }, }) const output = `${execution.stdout || ''}\n${execution.stderr || ''}` const resultLine = output.split(/\r?\n/).find((line) => line.startsWith('FREECAD_REFERENCE_RESULT=')) if (execution.error || execution.status !== 0 || !resultLine) { throw new Error(`FreeCAD reference probe failed with status ${execution.status}: ${execution.error?.message || output.trim()}`) } const result = JSON.parse(resultLine.slice('FREECAD_REFERENCE_RESULT='.length)) if (result.freecadVersion !== '1.1.1') throw new Error(`Expected FreeCAD 1.1.1, received ${result.freecadVersion}`) console.log(JSON.stringify({ command, result }, null, 2))