Files
seastem-electronjs/apps/server/compile.ts

74 lines
1.7 KiB
TypeScript

import { rm } from 'node:fs/promises'
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
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 { values } = parseArgs({
options: { target: { type: 'string' } },
strict: true,
allowPositionals: false,
})
const resolveTarget = (): Target => {
if (values.target !== undefined) {
if (!isTarget(values.target)) {
throw new Error(
`Invalid target: ${values.target}\nAllowed: ${Object.keys(TARGETS).join(', ')}`,
)
}
return values.target
}
const key = `${process.platform}-${process.arch}`
const target = HOST_MAP[key]
if (!target) {
throw new Error(`Unsupported host: ${key}`)
}
return target
}
const main = async () => {
const target = resolveTarget()
const suffix = target.replace('bun-', '')
const outfile = `server-${suffix}`
await rm(OUTDIR, { recursive: true, force: true })
const result = await Bun.build({
entrypoints: [ENTRYPOINT],
outdir: OUTDIR,
compile: { outfile, target },
})
if (!result.success) {
throw new Error(result.logs.map(String).join('\n'))
}
console.log(`${target}${OUTDIR}/${outfile}`)
}
main().catch((err) => {
console.error('❌', err instanceof Error ? err.message : err)
process.exit(1)
})