From 9f38636d76980bc3e04f55fbb7e0aa85c44bac15 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Sun, 8 Feb 2026 22:47:39 +0800 Subject: [PATCH] refactor(server): use Bun.Build.CompileTarget and derive host target instead of manual map --- apps/server/compile.ts | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/apps/server/compile.ts b/apps/server/compile.ts index b5bdb93..f81d48a 100644 --- a/apps/server/compile.ts +++ b/apps/server/compile.ts @@ -4,24 +4,15 @@ import { parseArgs } from 'node:util' 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 +const SUPPORTED_TARGETS: readonly Bun.Build.CompileTarget[] = [ + 'bun-windows-x64', + 'bun-darwin-arm64', + 'bun-linux-x64', + 'bun-linux-arm64', +] -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 isSupportedTarget = (value: string): value is Bun.Build.CompileTarget => + (SUPPORTED_TARGETS as readonly string[]).includes(value) const { values } = parseArgs({ options: { target: { type: 'string' } }, @@ -29,22 +20,22 @@ const { values } = parseArgs({ allowPositionals: false, }) -const resolveTarget = (): Target => { +const resolveTarget = (): Bun.Build.CompileTarget => { if (values.target !== undefined) { - if (!isTarget(values.target)) { + if (!isSupportedTarget(values.target)) { throw new Error( - `Invalid target: ${values.target}\nAllowed: ${Object.keys(TARGETS).join(', ')}`, + `Invalid target: ${values.target}\nAllowed: ${SUPPORTED_TARGETS.join(', ')}`, ) } return values.target } - const key = `${process.platform}-${process.arch}` - const target = HOST_MAP[key] - if (!target) { - throw new Error(`Unsupported host: ${key}`) + 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 target + return candidate } const main = async () => {