This commit is contained in:
NotII
2025-04-06 15:48:11 +01:00
parent a651bd3ec4
commit 59a9063bfc
5 changed files with 130 additions and 2 deletions

View File

@@ -2,8 +2,9 @@ import Dashboard from "@/components/dashboard/dashboard";
import Content from "@/components/dashboard/content"; import Content from "@/components/dashboard/content";
import { fetchServer } from '@/lib/server-service'; import { fetchServer } from '@/lib/server-service';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
import { Info } from 'lucide-react'; import { Info, GitCommit } from 'lucide-react';
import packageJson from '../../package.json'; import packageJson from '../../package.json';
import { getGitCommitInfo } from '@/lib/git-utils';
// ✅ Corrected Vendor Type // ✅ Corrected Vendor Type
interface Vendor { interface Vendor {
@@ -28,14 +29,16 @@ interface OrderStats {
export default async function DashboardPage() { export default async function DashboardPage() {
const startTime = performance.now(); const startTime = performance.now();
const [userResponse, orderStats] = await Promise.all([ const [userResponse, orderStats, gitInfo] = await Promise.all([
fetchServer<User>("/auth/me"), fetchServer<User>("/auth/me"),
fetchServer<OrderStats>("/orders/stats"), fetchServer<OrderStats>("/orders/stats"),
getGitCommitInfo()
]); ]);
const endTime = performance.now(); const endTime = performance.now();
const generationTime = (endTime - startTime).toFixed(2); const generationTime = (endTime - startTime).toFixed(2);
const panelVersion = packageJson.version; const panelVersion = packageJson.version;
const commitHash = gitInfo.commitHash;
const vendor = userResponse.vendor; const vendor = userResponse.vendor;
@@ -46,6 +49,9 @@ export default async function DashboardPage() {
<Info size={12} className="text-muted-foreground/80" /> <Info size={12} className="text-muted-foreground/80" />
<span>v{panelVersion}</span> <span>v{panelVersion}</span>
<span className="text-muted-foreground/50">|</span> <span className="text-muted-foreground/50">|</span>
<GitCommit size={12} className="text-muted-foreground/80" />
<span>{commitHash}</span>
<span className="text-muted-foreground/50">|</span>
<span>Generated in {generationTime}ms</span> <span>Generated in {generationTime}ms</span>
</div> </div>
</Dashboard> </Dashboard>

71
lib/git-utils.ts Normal file
View File

@@ -0,0 +1,71 @@
/**
* Git utility to load commit hash in both development and production environments
*/
interface GitInfo {
commitHash: string;
buildTime: string;
}
let cachedGitInfo: GitInfo | null = null;
/**
* Gets the current git commit hash from various sources
* - First tries environment variable (from Docker)
* - Then tries the JSON file created by our build script
* - Returns "dev" if nothing is available
*/
export async function getGitCommitInfo(): Promise<GitInfo> {
// Return cached value if available
if (cachedGitInfo) {
return cachedGitInfo;
}
// First, check if we have the env variable (from Docker)
if (process.env.GIT_COMMIT_SHA) {
const gitInfo: GitInfo = {
commitHash: process.env.GIT_COMMIT_SHA,
buildTime: new Date().toISOString(),
};
cachedGitInfo = gitInfo;
return gitInfo;
}
// In the browser, fetch from the public directory
if (typeof window !== 'undefined') {
try {
const response = await fetch('/git-info.json');
if (response.ok) {
const gitInfo: GitInfo = await response.json();
cachedGitInfo = gitInfo;
return gitInfo;
}
} catch (error) {
console.warn('Could not fetch git info from public directory', error);
}
} else {
// On the server (during local development), try to load from the static directory
try {
// We use dynamic import to avoid bundling this in the client build
const fs = require('fs');
const path = require('path');
const gitInfoPath = path.join(process.cwd(), 'public', 'git-info.json');
if (fs.existsSync(gitInfoPath)) {
const gitInfo: GitInfo = JSON.parse(fs.readFileSync(gitInfoPath, 'utf8'));
cachedGitInfo = gitInfo;
return gitInfo;
}
} catch (error) {
console.warn('Could not read git info from file system', error);
}
}
// Default if nothing else works
const defaultGitInfo: GitInfo = {
commitHash: 'development',
buildTime: new Date().toISOString(),
};
return defaultGitInfo;
}

View File

@@ -4,7 +4,9 @@
"gitCommit": "1.1.7", "gitCommit": "1.1.7",
"private": true, "private": true,
"scripts": { "scripts": {
"predev": "node scripts/get-git-hash.js",
"dev": "next dev", "dev": "next dev",
"prebuild": "node scripts/get-git-hash.js",
"build": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 next build", "build": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 next build",
"build:fast": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 NEXT_SKIP_LINT=1 NEXT_SKIP_TS_CHECK=1 next build", "build:fast": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 NEXT_SKIP_LINT=1 NEXT_SKIP_TS_CHECK=1 next build",
"build:optimized": "node scripts/optimize-build.js", "build:optimized": "node scripts/optimize-build.js",

4
public/git-info.json Normal file
View File

@@ -0,0 +1,4 @@
{
"commitHash": "a651bd3",
"buildTime": "2025-04-06T14:47:46.087Z"
}

45
scripts/get-git-hash.js Executable file
View File

@@ -0,0 +1,45 @@
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Ensure the directory exists
const gitDataDir = path.resolve(__dirname, '../.next/static/git');
if (!fs.existsSync(gitDataDir)) {
fs.mkdirSync(gitDataDir, { recursive: true });
}
let gitHash = 'unknown';
try {
// Try to get the git hash
gitHash = execSync('git rev-parse --short HEAD').toString().trim();
console.log(`✅ Git commit hash: ${gitHash}`);
} catch (error) {
console.error('⚠️ Could not get git commit hash:', error.message);
}
// Write the git hash to a file that can be imported
const gitInfoFilePath = path.resolve(gitDataDir, 'git-info.json');
fs.writeFileSync(
gitInfoFilePath,
JSON.stringify({
commitHash: gitHash,
buildTime: new Date().toISOString()
}, null, 2)
);
console.log(`📝 Git info written to: ${gitInfoFilePath}`);
// Also write to a location that can be imported during development
const gitInfoDevPath = path.resolve(__dirname, '../public/git-info.json');
fs.writeFileSync(
gitInfoDevPath,
JSON.stringify({
commitHash: gitHash,
buildTime: new Date().toISOString()
}, null, 2)
);
console.log(`📝 Git info written to: ${gitInfoDevPath}`);