import { rm } from 'node:fs/promises' import process from 'node:process' const ENTRYPOINT = '.output/server/index.mjs' const OUTDIR = 'out' const VALID_TARGETS = [ 'bun-windows-x64', 'bun-darwin-arm64', 'bun-linux-x64', 'bun-linux-arm64', ] as const type Target = (typeof VALID_TARGETS)[number] const HOST_MAP: Record = { 'win32-x64': 'bun-windows-x64', 'darwin-arm64': 'bun-darwin-arm64', 'linux-x64': 'bun-linux-x64', 'linux-arm64': 'bun-linux-arm64', } const resolveTarget = (): Target => { const idx = process.argv.indexOf('--target') if (idx !== -1) { const value = process.argv[idx + 1] if (!value || !VALID_TARGETS.includes(value as Target)) { throw new Error( `Invalid target: ${value}\nAllowed: ${VALID_TARGETS.join(', ')}`, ) } return value as Target } const key = `${process.platform}-${process.arch}` const target = HOST_MAP[key] if (!target) { throw new Error(`Unsupported host: ${key}`) } return target } const main = async () => { const target = resolveTarget() const suffix = target.replace('bun-', '') const outfile = `server-${suffix}` await rm(OUTDIR, { recursive: true, force: true }) const result = await Bun.build({ entrypoints: [ENTRYPOINT], outdir: OUTDIR, compile: { outfile, target }, }) if (!result.success) { throw new Error(result.logs.map(String).join('\n')) } console.log(`✓ ${target} → ${OUTDIR}/${outfile}`) } main().catch((err) => { console.error('❌', err instanceof Error ? err.message : err) process.exit(1) })