refactor(server): use Bun.Build.CompileTarget and derive host target instead of manual map

This commit is contained in:
2026-02-08 22:47:39 +08:00
parent 63906ec09b
commit 9f38636d76

View File

@@ -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<string, Target> = {
'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 () => {