feat(web): show the build's commit and date in settings
Lets you tell whether an installed PWA picked up a new release. The commit is read from .git at build time (the Docker build stage has no git binary), so .dockerignore now lets HEAD and the refs through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
.git
|
.git
|
||||||
|
# Just enough of the repository for the web build to read the commit hash.
|
||||||
|
!.git/HEAD
|
||||||
|
!.git/refs
|
||||||
|
!.git/packed-refs
|
||||||
.github
|
.github
|
||||||
**/node_modules
|
**/node_modules
|
||||||
**/.turbo
|
**/.turbo
|
||||||
|
|||||||
9
apps/web/src/lib/app-version.ts
Normal file
9
apps/web/src/lib/app-version.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// Replaced at build time by `define` in `vite.config.ts`.
|
||||||
|
declare const __APP_COMMIT__: string
|
||||||
|
declare const __APP_BUILT_AT__: string
|
||||||
|
|
||||||
|
/** The release this bundle was built from, to tell whether an installed PWA updated. */
|
||||||
|
export const appVersion = {
|
||||||
|
commit: __APP_COMMIT__,
|
||||||
|
builtAt: new Date(__APP_BUILT_AT__),
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import { Button } from '@/components/ui/button'
|
|||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||||
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
|
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
|
import { appVersion } from '@/lib/app-version'
|
||||||
import { authClient, signOut, useSession } from '@/lib/auth-client'
|
import { authClient, signOut, useSession } from '@/lib/auth-client'
|
||||||
|
|
||||||
export const Route = createFileRoute('/_authenticated/settings')({
|
export const Route = createFileRoute('/_authenticated/settings')({
|
||||||
@@ -40,10 +41,23 @@ function SettingsPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
<AppVersion />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const builtAtFormat = new Intl.DateTimeFormat('es', { dateStyle: 'short', timeStyle: 'short' })
|
||||||
|
|
||||||
|
function AppVersion() {
|
||||||
|
return (
|
||||||
|
<p className="text-center text-muted-foreground text-xs">
|
||||||
|
Versión <span className="font-mono">{appVersion.commit}</span> ·{' '}
|
||||||
|
{builtAtFormat.format(appVersion.builtAt)}
|
||||||
|
</p>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const profileSchema = z.object({
|
const profileSchema = z.object({
|
||||||
name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'),
|
name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { readFileSync } from 'node:fs'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
import tailwindcss from '@tailwindcss/vite'
|
import tailwindcss from '@tailwindcss/vite'
|
||||||
@@ -7,8 +8,38 @@ import { defineConfig } from 'vite'
|
|||||||
import { VitePWA } from 'vite-plugin-pwa'
|
import { VitePWA } from 'vite-plugin-pwa'
|
||||||
|
|
||||||
const srcDir = path.resolve(import.meta.dirname, './src')
|
const srcDir = path.resolve(import.meta.dirname, './src')
|
||||||
|
const gitDir = path.resolve(import.meta.dirname, '../../.git')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short hash of the commit being built, read straight from `.git` because the
|
||||||
|
* Docker build stage has no `git` binary (`.dockerignore` lets `HEAD` and the
|
||||||
|
* refs through for this). Falls back to `dev` when there is no repository.
|
||||||
|
*/
|
||||||
|
function readCommit(): string {
|
||||||
|
try {
|
||||||
|
const head = readFileSync(path.join(gitDir, 'HEAD'), 'utf8').trim()
|
||||||
|
if (!head.startsWith('ref: ')) return head.slice(0, 7)
|
||||||
|
|
||||||
|
const ref = head.slice('ref: '.length)
|
||||||
|
try {
|
||||||
|
return readFileSync(path.join(gitDir, ref), 'utf8').trim().slice(0, 7)
|
||||||
|
} catch {
|
||||||
|
// The ref may only exist in `packed-refs` (fresh clones, after `git gc`).
|
||||||
|
const packed = readFileSync(path.join(gitDir, 'packed-refs'), 'utf8')
|
||||||
|
const line = packed.split('\n').find((entry) => entry.endsWith(` ${ref}`))
|
||||||
|
if (line) return line.slice(0, 7)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// No repository (or a worktree, where `.git` is a file): not worth failing the build.
|
||||||
|
}
|
||||||
|
return 'dev'
|
||||||
|
}
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
define: {
|
||||||
|
__APP_COMMIT__: JSON.stringify(readCommit()),
|
||||||
|
__APP_BUILT_AT__: JSON.stringify(new Date().toISOString()),
|
||||||
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// Must run before react(): it watches `src/routes` and generates `routeTree.gen.ts`.
|
// Must run before react(): it watches `src/routes` and generates `routeTree.gen.ts`.
|
||||||
tanstackRouter({ target: 'react', autoCodeSplitting: true }),
|
tanstackRouter({ target: 'react', autoCodeSplitting: true }),
|
||||||
|
|||||||
Reference in New Issue
Block a user