Skip to content

Commit

Permalink
🧹 Simplify version and allow to pass infos via env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
phlmn committed Dec 17, 2023
1 parent 88976d4 commit 42e2b4a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 64 deletions.
67 changes: 20 additions & 47 deletions frontend/src/common/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,74 +41,47 @@ 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 && (
<a
href={`https://github.com/bugbakery/transcribee/commit/${lastCommit.hash}`}
href={commit?.url}
target="_blank"
rel="noreferrer"
className="underline decoration-dashed"
title="View commit on GitHub"
>
{lastCommit.hash.substring(0, 10)} <IoMdOpen className="inline" />
{commit?.hash.substring(0, 7)} <IoMdOpen className="inline" />
</a>
)}{' '}
({lastCommit?.countSinceStart || 'unknown'})
)}
</>
);
}

export function LongVersion() {
return (
<>
Frontend built on {formatDate(version?.date)}.
{version?.lastCommitOnMain?.countSinceStart ? (
Frontend built on {formatDate(version?.buildDate)}.
{version?.commit?.hash && (
<>
{' '}
<CommitPopup
commit={version.lastCommitOnMain}
text={`${version.lastCommitOnMain.countSinceStart} commits on main`}
/>
{version?.branch != 'main' &&
version.lastCommit &&
version.lastCommit?.countSinceStart &&
version.lastCommitOnMain?.countSinceStart && (
<>
{' '}
+{' '}
<CommitPopup
commit={version.lastCommit}
text={`${
version.lastCommit?.countSinceStart - version.lastCommitOnMain?.countSinceStart
} commits on ${version.branch}`}
/>
</>
)}
{version.diffShort && <> + {version.diffShort}</>}
Last commit{' '}
{version.commit?.hash && (
<a
href={version.commit?.url}
target="_blank"
rel="noreferrer"
className="underline decoration-dashed"
title="View commit on GitHub"
>
{version.commit.hash.substring(0, 7)} <IoMdOpen className="inline" />
</a>
)}{' '}
on {formatDate(version.commit?.date)}.
</>
) : (
version?.lastCommit?.hash && (
<>
{' '}
Last commit{' '}
{version.lastCommit?.hash && (
<a
href={`https://github.com/bugbakery/transcribee/commit/${version.lastCommit.hash}`}
target="_blank"
rel="noreferrer"
className="underline decoration-dashed"
title="View commit on GitHub"
>
{version.lastCommit.hash.substring(0, 10)} <IoMdOpen className="inline" />
</a>
)}{' '}
on {formatDate(version.lastCommit?.date)}.
</>
)
)}
</>
);
Expand Down
12 changes: 5 additions & 7 deletions frontend/src/common/virtual:git-version.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
21 changes: 11 additions & 10 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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};
Expand Down

0 comments on commit 42e2b4a

Please sign in to comment.