import { rm } from 'node:fs/promises' import process from 'node:process' const ENTRYPOINT = '.output/server/index.mjs' const OUTDIR = 'out' const TARGETS = { 'bun-windows-x64': true, 'bun-darwin-arm64': true, 'bun-linux-x64': true, 'bun-linux-arm64': true, } as const type Target = keyof typeof TARGETS const isTarget = (value: unknown): value is Target => typeof value === 'string' && value in TARGETS 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 (!isTarget(value)) { throw new Error( `Invalid target: ${value}\nAllowed: ${Object.keys(TARGETS).join(', ')}`, ) } return value } 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) })