From 42e2b4a8393610ef77ed288248dea3c8aca86dd8 Mon Sep 17 00:00:00 2001 From: Philipp Mandler Date: Sat, 16 Dec 2023 15:50:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Simplify=20version=20and=20allow?= =?UTF-8?q?=20to=20pass=20infos=20via=20env=20variables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/common/footer.tsx | 67 ++++++-------------- frontend/src/common/virtual:git-version.d.ts | 12 ++-- frontend/vite.config.js | 21 +++--- 3 files changed, 36 insertions(+), 64 deletions(-) diff --git a/frontend/src/common/footer.tsx b/frontend/src/common/footer.tsx index 2d419a35..66e3ba76 100644 --- a/frontend/src/common/footer.tsx +++ b/frontend/src/common/footer.tsx @@ -41,23 +41,22 @@ export function CommitPopup({ commit, text }: { commit: Commit; text: string }) } export function ShortVersion() { - const lastCommit = version?.lastCommit; + const commit = version?.commit; return ( <> Version:{' '} - {lastCommit?.hash && ( + {commit?.hash && ( - {lastCommit.hash.substring(0, 10)} + {commit?.hash.substring(0, 7)} - )}{' '} - ({lastCommit?.countSinceStart || 'unknown'}) + )} ); } @@ -65,50 +64,24 @@ export function ShortVersion() { export function LongVersion() { return ( <> - Frontend built on {formatDate(version?.date)}. - {version?.lastCommitOnMain?.countSinceStart ? ( + Frontend built on {formatDate(version?.buildDate)}. + {version?.commit?.hash && ( <> {' '} - - {version?.branch != 'main' && - version.lastCommit && - version.lastCommit?.countSinceStart && - version.lastCommitOnMain?.countSinceStart && ( - <> - {' '} - +{' '} - - - )} - {version.diffShort && <> + {version.diffShort}} + Last commit{' '} + {version.commit?.hash && ( + + {version.commit.hash.substring(0, 7)} + + )}{' '} + on {formatDate(version.commit?.date)}. - ) : ( - version?.lastCommit?.hash && ( - <> - {' '} - Last commit{' '} - {version.lastCommit?.hash && ( - - {version.lastCommit.hash.substring(0, 10)} - - )}{' '} - on {formatDate(version.lastCommit?.date)}. - - ) )} ); diff --git a/frontend/src/common/virtual:git-version.d.ts b/frontend/src/common/virtual:git-version.d.ts index 11333011..4a77e8d5 100644 --- a/frontend/src/common/virtual:git-version.d.ts +++ b/frontend/src/common/virtual:git-version.d.ts @@ -1,16 +1,14 @@ declare module 'virtual:git-version' { export interface Commit { - countSinceStart?: number; - date?: string; hash?: string; + date?: string; + url?: string; } export interface Version { - branch?: string; - diffShort?: string; - lastCommit?: Commit; - lastCommitOnMain?: Commit; - date?: string; + name?: string; + buildDate?: string; + commit?: Commit; } let version: Version | undefined; diff --git a/frontend/vite.config.js b/frontend/vite.config.js index ee12ec4d..ad1cdc50 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -6,6 +6,8 @@ import { execSync } from 'child_process'; import fs from 'fs'; import * as mime from 'mime-types'; +import pkg from './package.json'; + function gitVersionPlugin() { const virtualModuleId = 'virtual:git-version'; @@ -27,22 +29,21 @@ function gitVersionPlugin() { return stdout; } - function commitInfo(ref) { + function commitInfo() { + const hash = process.env.COMMIT_HASH || git('rev-parse HEAD'); + return { - hash: git(`rev-parse ${ref}`), - date: git(`show -s --format=%cI ${ref}`), - countSinceStart: parseInt(git(`rev-list --count ${ref}`)), + hash: hash, + date: process.env.COMMIT_DATE || git('show -s --format=%cI HEAD'), + url: process.env.COMMIT_URL || `https://github.com/bugbakery/transcribee/commit/${hash}`, }; } if (id === virtualModuleId) { - const lastCommitOnMain = git('merge-base origin/main HEAD'); const json = JSON.stringify({ - diffShort: git('diff --shortstat HEAD'), - lastCommit: commitInfo('HEAD'), - lastCommitOnMain: commitInfo(lastCommitOnMain), - branch: git('rev-parse --abbrev-ref HEAD'), - date: new Date().toISOString(), + name: process.env.VERSION_NAME || pkg.version, + commit: commitInfo('HEAD'), + buildDate: new Date().toISOString(), }); return ` const version = ${json};