forked from imbytecat/fullstack-starter
refactor(server): use Bun.Build.CompileTarget and derive host target instead of manual map
This commit is contained in:
@@ -4,24 +4,15 @@ import { parseArgs } from 'node:util'
|
|||||||
const ENTRYPOINT = '.output/server/index.mjs'
|
const ENTRYPOINT = '.output/server/index.mjs'
|
||||||
const OUTDIR = 'out'
|
const OUTDIR = 'out'
|
||||||
|
|
||||||
const TARGETS = {
|
const SUPPORTED_TARGETS: readonly Bun.Build.CompileTarget[] = [
|
||||||
'bun-windows-x64': true,
|
'bun-windows-x64',
|
||||||
'bun-darwin-arm64': true,
|
'bun-darwin-arm64',
|
||||||
'bun-linux-x64': true,
|
'bun-linux-x64',
|
||||||
'bun-linux-arm64': true,
|
'bun-linux-arm64',
|
||||||
} as const
|
]
|
||||||
|
|
||||||
type Target = keyof typeof TARGETS
|
const isSupportedTarget = (value: string): value is Bun.Build.CompileTarget =>
|
||||||
|
(SUPPORTED_TARGETS as readonly string[]).includes(value)
|
||||||
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 { values } = parseArgs({
|
const { values } = parseArgs({
|
||||||
options: { target: { type: 'string' } },
|
options: { target: { type: 'string' } },
|
||||||
@@ -29,22 +20,22 @@ const { values } = parseArgs({
|
|||||||
allowPositionals: false,
|
allowPositionals: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
const resolveTarget = (): Target => {
|
const resolveTarget = (): Bun.Build.CompileTarget => {
|
||||||
if (values.target !== undefined) {
|
if (values.target !== undefined) {
|
||||||
if (!isTarget(values.target)) {
|
if (!isSupportedTarget(values.target)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Invalid target: ${values.target}\nAllowed: ${Object.keys(TARGETS).join(', ')}`,
|
`Invalid target: ${values.target}\nAllowed: ${SUPPORTED_TARGETS.join(', ')}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return values.target
|
return values.target
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = `${process.platform}-${process.arch}`
|
const os = process.platform === 'win32' ? 'windows' : process.platform
|
||||||
const target = HOST_MAP[key]
|
const candidate = `bun-${os}-${process.arch}`
|
||||||
if (!target) {
|
if (!isSupportedTarget(candidate)) {
|
||||||
throw new Error(`Unsupported host: ${key}`)
|
throw new Error(`Unsupported host: ${process.platform}-${process.arch}`)
|
||||||
}
|
}
|
||||||
return target
|
return candidate
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user