refactor(server): use type guard to eliminate as Target casts in compile.ts
This commit is contained in:
@@ -4,14 +4,17 @@ import process from 'node:process'
|
||||
const ENTRYPOINT = '.output/server/index.mjs'
|
||||
const OUTDIR = 'out'
|
||||
|
||||
const VALID_TARGETS = [
|
||||
'bun-windows-x64',
|
||||
'bun-darwin-arm64',
|
||||
'bun-linux-x64',
|
||||
'bun-linux-arm64',
|
||||
] as const
|
||||
const TARGETS = {
|
||||
'bun-windows-x64': true,
|
||||
'bun-darwin-arm64': true,
|
||||
'bun-linux-x64': true,
|
||||
'bun-linux-arm64': true,
|
||||
} 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> = {
|
||||
'win32-x64': 'bun-windows-x64',
|
||||
@@ -24,12 +27,12 @@ const resolveTarget = (): Target => {
|
||||
const idx = process.argv.indexOf('--target')
|
||||
if (idx !== -1) {
|
||||
const value = process.argv[idx + 1]
|
||||
if (!value || !VALID_TARGETS.includes(value as Target)) {
|
||||
if (!isTarget(value)) {
|
||||
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}`
|
||||
|
||||
Reference in New Issue
Block a user