refactor(server): use type guard to eliminate as Target casts in compile.ts

This commit is contained in:
2026-02-08 22:33:02 +08:00
parent e171db8196
commit 8c4e4ad150

View File

@@ -4,14 +4,17 @@ import process from 'node:process'
const ENTRYPOINT = '.output/server/index.mjs' const ENTRYPOINT = '.output/server/index.mjs'
const OUTDIR = 'out' const OUTDIR = 'out'
const VALID_TARGETS = [ const TARGETS = {
'bun-windows-x64', 'bun-windows-x64': true,
'bun-darwin-arm64', 'bun-darwin-arm64': true,
'bun-linux-x64', 'bun-linux-x64': true,
'bun-linux-arm64', 'bun-linux-arm64': true,
] as const } as const
type Target = (typeof VALID_TARGETS)[number] type Target = keyof typeof TARGETS
const isTarget = (value: unknown): value is Target =>
typeof value === 'string' && value in TARGETS
const HOST_MAP: Record<string, Target> = { const HOST_MAP: Record<string, Target> = {
'win32-x64': 'bun-windows-x64', 'win32-x64': 'bun-windows-x64',
@@ -24,12 +27,12 @@ const resolveTarget = (): Target => {
const idx = process.argv.indexOf('--target') const idx = process.argv.indexOf('--target')
if (idx !== -1) { if (idx !== -1) {
const value = process.argv[idx + 1] const value = process.argv[idx + 1]
if (!value || !VALID_TARGETS.includes(value as Target)) { if (!isTarget(value)) {
throw new Error( throw new Error(
`Invalid target: ${value}\nAllowed: ${VALID_TARGETS.join(', ')}`, `Invalid target: ${value}\nAllowed: ${Object.keys(TARGETS).join(', ')}`,
) )
} }
return value as Target return value
} }
const key = `${process.platform}-${process.arch}` const key = `${process.platform}-${process.arch}`