Show the build's version and commit in Settings

vite.config.ts stamps the package version, git commit and build time
into the bundle as __BUILD__, and the Settings sidebar shows
"v0.1.0 · <short sha>" (full sha and build time on hover). The Docker
build has no git binary, so the commit is read from .git/HEAD and the
refs directly; .dockerignore now lets just those files through.
GIT_COMMIT in the environment overrides detection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GaoZtJQZwLXkEgWs8twCia
This commit is contained in:
agent 2026-09-27 20:08:19 +00:00
parent 2e52f1be2c
commit 3ac278835b
4 changed files with 52 additions and 1 deletions

View file

@ -1,8 +1,40 @@
import { execSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
/** Current commit. Uses git when available, else reads .git directly (the Docker build has no git binary). */
function gitCommit(): { commit: string; dirty: boolean } {
if (process.env.GIT_COMMIT) return { commit: process.env.GIT_COMMIT, dirty: false };
try {
const commit = execSync('git rev-parse HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim();
const dirty = execSync('git status --porcelain --untracked-files=no', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim() !== '';
return { commit, dirty };
} catch {
/* fall through */
}
try {
const head = readFileSync('.git/HEAD', 'utf8').trim();
if (!head.startsWith('ref: ')) return { commit: head, dirty: false };
const ref = head.slice(5);
if (existsSync(`.git/${ref}`)) return { commit: readFileSync(`.git/${ref}`, 'utf8').trim(), dirty: false };
const packed = readFileSync('.git/packed-refs', 'utf8').split('\n').find((l) => l.endsWith(` ${ref}`));
if (packed) return { commit: packed.split(' ')[0], dirty: false };
} catch {
/* no repository */
}
return { commit: 'unknown', dirty: false };
}
const build = {
version: JSON.parse(readFileSync('package.json', 'utf8')).version as string,
...gitCommit(),
time: new Date().toISOString()
};
export default defineConfig({
plugins: [sveltekit()],
define: { __BUILD__: JSON.stringify(build) },
test: {
include: ['src/**/*.test.ts'],
environment: 'node'