|
| 1 | +import { $ } from 'bun' |
| 2 | +import { mkdir, writeFile } from 'node:fs/promises' |
| 3 | +import { createHash } from 'node:crypto' |
| 4 | +import { readFileSync } from 'node:fs' |
| 5 | +import { resolve } from 'node:path' |
| 6 | + |
| 7 | +const packageJson = JSON.parse(readFileSync(resolve(import.meta.dir, '../package.json'), 'utf-8')) |
| 8 | +const version = packageJson.version |
| 9 | + |
| 10 | +async function buildForPlatform(triple: string, outfile: string) { |
| 11 | + console.log(`Building for ${triple}...`) |
| 12 | + |
| 13 | + for (let i = 0; i < 5; ++i) { |
| 14 | + try { |
| 15 | + let cmd = $`bun build --compile --target=${triple} ./src/index.ts --outfile=${outfile} --env inline --define __VERSION__=${JSON.stringify(version)}` |
| 16 | + |
| 17 | + cmd = cmd.env({ |
| 18 | + PLATFORM_LIBC: triple.includes('-musl') ? 'musl' : 'glibc', |
| 19 | + }) |
| 20 | + |
| 21 | + await cmd |
| 22 | + console.log(`✓ Built ${outfile}`) |
| 23 | + return |
| 24 | + } catch (err) { |
| 25 | + if (i < 4) { |
| 26 | + console.log(` Retry ${i + 1}/5 for ${triple}`) |
| 27 | + continue |
| 28 | + } |
| 29 | + throw new Error(`Failed to build for platform ${triple}`, { cause: err }) |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function sha256(filePath: string): string { |
| 35 | + const content = readFileSync(filePath) |
| 36 | + return createHash('sha256').update(content).digest('hex') |
| 37 | +} |
| 38 | + |
| 39 | +async function main() { |
| 40 | + await mkdir('dist', { recursive: true }) |
| 41 | + |
| 42 | + const builds = [ |
| 43 | + ['bun-linux-arm64', './dist/herb-linux-arm64'], |
| 44 | + ['bun-linux-arm64-musl', './dist/herb-linux-arm64-musl'], |
| 45 | + ['bun-linux-x64-baseline', './dist/herb-linux-x64'], |
| 46 | + ['bun-linux-x64-musl-baseline', './dist/herb-linux-x64-musl'], |
| 47 | + ['bun-darwin-arm64', './dist/herb-macos-arm64'], |
| 48 | + ['bun-darwin-x64-baseline', './dist/herb-macos-x64'], |
| 49 | + ['bun-windows-x64-baseline', './dist/herb-windows-x64.exe'], |
| 50 | + ] as const |
| 51 | + |
| 52 | + await Promise.all( |
| 53 | + builds.map(([triple, outfile]) => buildForPlatform(triple, outfile)) |
| 54 | + ) |
| 55 | + |
| 56 | + console.log('\nGenerating checksums...') |
| 57 | + const sums: string[] = [] |
| 58 | + |
| 59 | + for (const [, outfile] of builds) { |
| 60 | + const hash = sha256(outfile) |
| 61 | + const filename = outfile.split('/').pop()! |
| 62 | + sums.push(`${hash} ${filename}`) |
| 63 | + console.log(` ${filename}: ${hash}`) |
| 64 | + } |
| 65 | + |
| 66 | + const sumsFile = resolve('dist', 'SHA256SUMS') |
| 67 | + await writeFile(sumsFile, sums.join('\n') + '\n') |
| 68 | + console.log(`\n✓ Checksums written to ${sumsFile}`) |
| 69 | + |
| 70 | + console.log('\n✓ All builds completed successfully') |
| 71 | +} |
| 72 | + |
| 73 | +main().catch((err) => { |
| 74 | + console.error('Build failed:', err) |
| 75 | + process.exit(1) |
| 76 | +}) |
0 commit comments