#!/usr/bin/env node import { execFileSync } from 'node:child_process' import fs from 'node:fs' import path from 'node:path' import { cachePath, REPO_ROOT, resolveStorageRoot, runsPath } from './debug/paths.mjs' import { createTemporaryFiles } from './debug/temporary-files.mjs' if (process.platform !== 'darwin' || process.arch !== 'arm64') { console.error('This build requires an Apple silicon Mac and the Xcode toolchain.') process.exit(1) } const root = resolveStorageRoot() const temps = createTemporaryFiles(root) const output = runsPath(root, 'macos-client') const goCache = cachePath(root, 'go-build') const input = process.argv.slice(2) if (input.length !== 0 && (input.length !== 2 || input[0] !== '--runtime')) { console.error('Usage: node scripts/build-macos.mjs [--runtime ]') process.exit(1) } const external = input.length ? path.resolve(input[1]) : null const directory = temps.create('build-macos', process.pid) const env = { ...process.env, TMPDIR: directory, GOTMPDIR: directory, GOCACHE: goCache, CGO_ENABLED: '1' } const run = (command, args) => execFileSync(command, args, { cwd: REPO_ROOT, env, stdio: 'inherit' }) function stageRuntime(source) { const staged = path.join(directory, 'nivfold') fs.copyFileSync(source, staged, fs.constants.COPYFILE_EXCL) fs.accessSync(staged, fs.constants.X_OK) run('codesign', ['--verify', '--strict', staged]) const report = JSON.parse(execFileSync(staged, ['probe'], { cwd: REPO_ROOT, env, timeout: 5000, encoding: 'utf8' })) if (report.runtime !== 'nivfold' || report.protocol_version !== 1 || report.supported !== true) { throw new Error('expected a compatible Nivfold runtime (protocol 1)') } return staged } try { for (const dir of [output, goCache]) fs.mkdirSync(dir, { recursive: true }) const helper = external ? stageRuntime(external) : null run('pnpm', ['--dir', 'frontend', '--store-dir', cachePath(root, 'pnpm-store'), 'install', '--frozen-lockfile']) run('pnpm', ['--dir', 'frontend', 'run', 'build']) const client = path.join(directory, 'reims-go') run('go', ['build', '-trimpath', '-tags', 'desktop,production', '-o', client, '.']) if (helper) fs.renameSync(helper, path.join(output, 'nivfold')) fs.renameSync(client, path.join(output, 'reims-go')) temps.close(directory) console.log(`Native macOS client: ${path.join(output, 'reims-go')}`) if (!helper) console.log('The client build does not install a VM runtime. Pass --runtime with a trusted Nivfold build to install it beside the client.') } catch (error) { console.error(`macOS build failed: ${error.message}\nBuild directory retained: ${directory}`) process.exitCode = 1 }