import { rm } from 'node:fs/promises' import { parseArgs } from 'node:util' const ENTRYPOINT = '.output/server/index.mjs' const OUTDIR = 'out' const SUPPORTED_TARGETS: readonly Bun.Build.CompileTarget[] = [ 'bun-windows-x64', 'bun-darwin-arm64', 'bun-linux-x64', 'bun-linux-arm64', ] const isSupportedTarget = (value: string): value is Bun.Build.CompileTarget => (SUPPORTED_TARGETS as readonly string[]).includes(value) const { values } = parseArgs({ options: { target: { type: 'string' } }, strict: true, allowPositionals: false, }) const resolveTarget = (): Bun.Build.CompileTarget => { if (values.target !== undefined) { if (!isSupportedTarget(values.target)) { throw new Error( `Invalid target: ${values.target}\nAllowed: ${SUPPORTED_TARGETS.join(', ')}`, ) } return values.target } const os = process.platform === 'win32' ? 'windows' : process.platform const candidate = `bun-${os}-${process.arch}` if (!isSupportedTarget(candidate)) { throw new Error(`Unsupported host: ${process.platform}-${process.arch}`) } return candidate } 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) })