refactor(server): use util.parseArgs for declarative CLI arg parsing in compile.ts

This commit is contained in:
2026-02-08 22:38:49 +08:00
parent 8c4e4ad150
commit 63906ec09b

View File

@@ -1,5 +1,5 @@
import { rm } from 'node:fs/promises' import { rm } from 'node:fs/promises'
import process from 'node:process' import { parseArgs } from 'node:util'
const ENTRYPOINT = '.output/server/index.mjs' const ENTRYPOINT = '.output/server/index.mjs'
const OUTDIR = 'out' const OUTDIR = 'out'
@@ -23,16 +23,20 @@ const HOST_MAP: Record<string, Target> = {
'linux-arm64': 'bun-linux-arm64', 'linux-arm64': 'bun-linux-arm64',
} }
const { values } = parseArgs({
options: { target: { type: 'string' } },
strict: true,
allowPositionals: false,
})
const resolveTarget = (): Target => { const resolveTarget = (): Target => {
const idx = process.argv.indexOf('--target') if (values.target !== undefined) {
if (idx !== -1) { if (!isTarget(values.target)) {
const value = process.argv[idx + 1]
if (!isTarget(value)) {
throw new Error( throw new Error(
`Invalid target: ${value}\nAllowed: ${Object.keys(TARGETS).join(', ')}`, `Invalid target: ${values.target}\nAllowed: ${Object.keys(TARGETS).join(', ')}`,
) )
} }
return value return values.target
} }
const key = `${process.platform}-${process.arch}` const key = `${process.platform}-${process.arch}`