Embed git SHA at build time via define flag

This commit is contained in:
Chris Wanstrath 2026-03-04 19:09:46 -08:00
parent dfb865e433
commit 0ae4e6e9b2
2 changed files with 5 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import { join } from 'path'
const DIST_DIR = join(import.meta.dir, '..', 'dist') const DIST_DIR = join(import.meta.dir, '..', 'dist')
const ENTRY_POINT = join(import.meta.dir, '..', 'src', 'cli', 'index.ts') const ENTRY_POINT = join(import.meta.dir, '..', 'src', 'cli', 'index.ts')
const GIT_SHA = Bun.spawnSync(['git', 'rev-parse', '--short', 'HEAD']).stdout.toString().trim() || 'unknown'
interface BuildTarget { interface BuildTarget {
arch: string arch: string
@ -40,6 +41,7 @@ async function buildCurrent() {
'bun', 'bun',
'--minify', '--minify',
'--sourcemap=external', '--sourcemap=external',
`--define=__GIT_SHA__="${GIT_SHA}"`,
'--outfile', '--outfile',
output, output,
], { ], {
@ -73,6 +75,7 @@ async function buildTarget(target: BuildTarget) {
`bun-${target.os}-${target.arch}`, `bun-${target.os}-${target.arch}`,
'--minify', '--minify',
'--sourcemap=external', '--sourcemap=external',
`--define=__GIT_SHA__="${GIT_SHA}"`,
'--outfile', '--outfile',
output, output,
], { ], {

View File

@ -1,3 +1,3 @@
const result = Bun.spawnSync(['git', 'rev-parse', '--short', 'HEAD']) declare var __GIT_SHA__: string
export const SHA = result.stdout.toString().trim() export const SHA: string = typeof __GIT_SHA__ !== 'undefined' ? __GIT_SHA__ : 'dev'