forked from imbytecat/fullstack-starter
ed257fe4e6
- migrate: 校验已应用 migration 的 SHA-256,拒绝 schema drift; split 后 trim + skip empty,避免空 statement 触发 SQL 错误 - todo.contract: update 拒绝空 patch - env: DATABASE_URL 限定 postgres(ql):// scheme,配置错误更早失败 - compile: autoloadDotenv: false,二进制部署不再吞 cwd 的 .env - Error.tsx: 生产环境隐藏 error.message,避免内部错误泄露 - AGENTS: 同步 generatedFieldKeys / migrator 行为新描述
73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { mkdir, rm } from 'node:fs/promises'
|
|
import { parseArgs } from 'node:util'
|
|
|
|
const ENTRYPOINT = 'bin.ts'
|
|
const OUTDIR = 'out'
|
|
|
|
const SUPPORTED_TARGETS: readonly Bun.Build.CompileTarget[] = [
|
|
'bun-windows-x64',
|
|
'bun-darwin-arm64',
|
|
'bun-darwin-x64',
|
|
'bun-linux-x64',
|
|
'bun-linux-arm64',
|
|
]
|
|
|
|
const SUPPORTED_TARGET_SET: ReadonlySet<string> = new Set(SUPPORTED_TARGETS)
|
|
|
|
const isSupportedTarget = (value: string): value is Bun.Build.CompileTarget => SUPPORTED_TARGET_SET.has(value)
|
|
|
|
const { values } = parseArgs({
|
|
options: { target: { type: 'string' } },
|
|
strict: true,
|
|
allowPositionals: false,
|
|
})
|
|
|
|
const resolveTarget = (): Bun.Build.CompileTarget => {
|
|
if (values.target !== undefined) {
|
|
if (!isSupportedTarget(values.target)) {
|
|
throw new Error(`Invalid target: ${values.target}\nAllowed: ${SUPPORTED_TARGETS.join(', ')}`)
|
|
}
|
|
return values.target
|
|
}
|
|
|
|
const os = process.platform === 'win32' ? 'windows' : process.platform
|
|
const candidate = `bun-${os}-${process.arch}`
|
|
if (!isSupportedTarget(candidate)) {
|
|
throw new Error(`Unsupported host: ${process.platform}-${process.arch}`)
|
|
}
|
|
return candidate
|
|
}
|
|
|
|
const main = async () => {
|
|
const target = resolveTarget()
|
|
const suffix = target.replace('bun-', '')
|
|
const outfile = `server-${suffix}`
|
|
|
|
await mkdir(OUTDIR, { recursive: true })
|
|
await Promise.all([rm(`${OUTDIR}/${outfile}`, { force: true }), rm(`${OUTDIR}/${outfile}.exe`, { force: true })])
|
|
|
|
const result = await Bun.build({
|
|
entrypoints: [ENTRYPOINT],
|
|
outdir: OUTDIR,
|
|
// autoloadDotenv: false — produce a deterministic binary; it must not silently consume a .env from cwd.
|
|
compile: { outfile, target, autoloadDotenv: false },
|
|
minify: true,
|
|
bytecode: true,
|
|
sourcemap: 'inline',
|
|
})
|
|
|
|
if (!result.success) {
|
|
throw new Error(result.logs.map(String).join('\n'))
|
|
}
|
|
|
|
// Bun bundler still writes *.js.map next to the binary even with inline sourcemap.
|
|
await rm(`${OUTDIR}/${ENTRYPOINT.replace(/\.ts$/, '')}.js.map`, { force: true })
|
|
|
|
console.log(`✓ ${target} → ${OUTDIR}/${outfile}`)
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('❌', err instanceof Error ? err.message : err)
|
|
process.exit(1)
|
|
})
|