diff --git a/components/layout/NavLinks.tsx b/components/layout/NavLinks.tsx index 4364eda..a11295c 100644 --- a/components/layout/NavLinks.tsx +++ b/components/layout/NavLinks.tsx @@ -6,11 +6,7 @@ import { usePathname } from 'next/navigation' import { ExternalLink } from '@/components/ui/ExternalLink' import { navItems } from '@/components/layout/navItems' -type NavLinksProps = { - gitOriginUrl: string | null -} - -export function NavLinks({ gitOriginUrl }: NavLinksProps) { +export function NavLinks() { const pathname = usePathname() const isActive = (href: string) => { @@ -30,11 +26,7 @@ export function NavLinks({ gitOriginUrl }: NavLinksProps) { > {item.isExternal ? ( {item.icon} diff --git a/components/layout/Sidebar.tsx b/components/layout/Sidebar.tsx index e522693..f86f8a3 100644 --- a/components/layout/Sidebar.tsx +++ b/components/layout/Sidebar.tsx @@ -42,7 +42,7 @@ export async function Sidebar() { {metadata.title!.toString()}
diff --git a/components/layout/navItems.tsx b/components/layout/navItems.tsx index 5f3d512..aa99c81 100644 --- a/components/layout/navItems.tsx +++ b/components/layout/navItems.tsx @@ -20,10 +20,14 @@ export const navItems: NavItem[] = [ icon: , isExternal: true, }, - { - href: '{gitOriginUrl}', - label: 'Source Code', - icon: , - isExternal: true, - }, + ...(process.env.GIT_ORIGIN_URL + ? [ + { + href: process.env.GIT_ORIGIN_URL, + label: 'Source Code', + icon: , + isExternal: true, + }, + ] + : []), ] diff --git a/next.config.ts b/next.config.ts index f885e1d..985069c 100644 --- a/next.config.ts +++ b/next.config.ts @@ -4,6 +4,10 @@ const nextConfig: NextConfig = { images: { qualities: [75, 100], }, + env: { + GIT_COMMIT_HASH: process.env.GIT_COMMIT_HASH, + GIT_ORIGIN_URL: process.env.GIT_ORIGIN_URL, + }, } export default nextConfig diff --git a/nixpacks.toml b/nixpacks.toml index e6ec59c..8ad5b72 100644 --- a/nixpacks.toml +++ b/nixpacks.toml @@ -1,2 +1,2 @@ [phases.setup] -aptPkgs = ["libnss3", "libatk1.0-0", "libatk-bridge2.0-0", "libcups2", "libgbm1", "libasound2t64", "libpangocairo-1.0-0", "libxss1", "libgtk-3-0", "libxshmfence1", "libglu1", "chromium", "unzip", "curl", "wget", "git"] \ No newline at end of file +aptPkgs = ["libnss3", "libatk1.0-0", "libatk-bridge2.0-0", "libcups2", "libgbm1", "libasound2t64", "libpangocairo-1.0-0", "libxss1", "libgtk-3-0", "libxshmfence1", "libglu1", "chromium", "unzip", "curl", "wget"] \ No newline at end of file diff --git a/utils/git.ts b/utils/git.ts index d011ebc..dd20d69 100644 --- a/utils/git.ts +++ b/utils/git.ts @@ -1,53 +1,55 @@ -import { exec } from 'child_process' - /** - * Get the current Git commit hash. - * @returns The current Git commit hash as a string, or null if an error occurs. + * Get the current git commit hash. + * @returns The current git commit hash, or null if it cannot be determined. */ export async function getGitCommitHash(): Promise { - try { - const commitHash = (await new Promise((resolve, reject) => { - exec('git rev-parse HEAD', (error, stdout) => { - if (error) { - reject(error) - } else { - resolve(stdout.toString().trim()) - } - }) - })) as string + if (process.env.GIT_COMMIT_HASH) { + return process.env.GIT_COMMIT_HASH + } - return commitHash - } catch (error) { - console.error('Error getting Git commit hash:', error) + try { + const { exec } = await import('child_process') + const hash = await new Promise((res, rej) => { + exec('git rev-parse HEAD', (err, stdout) => (err ? rej(err) : res(stdout.trim()))) + }) + return hash + } catch { return null } } /** - * Get the Git origin URL. - * @returns The Git origin URL as a string, or null if an error occurs. + * Get the git origin URL. + * @returns The git origin URL, or null if it cannot be determined. */ export async function getGitOriginUrl(): Promise { + const envUrl = process.env.GIT_ORIGIN_URL + if (envUrl) return normalizeGitUrl(envUrl) + try { - const originUrl = (await new Promise((resolve, reject) => { - exec('git config --get remote.origin.url', (error, stdout) => { - if (error) { - reject(error) - } else { - resolve(stdout.toString().trim()) - } - }) - })) as string - - if (originUrl.startsWith('ssh://')) { - const httpsUrl = originUrl.replace(/^ssh:\/\/git@/, 'https://').replace(/\.git$/, '') - const urlWithoutPort = httpsUrl.replace(/:\d+/, '') - return urlWithoutPort - } - - return originUrl - } catch (error) { - console.error('Error getting Git origin URL:', error) + const { exec } = await import('child_process') + const url = await new Promise((res, rej) => { + exec('git config --get remote.origin.url', (err, stdout) => + err ? rej(err) : res(stdout.trim()), + ) + }) + return normalizeGitUrl(url) + } catch { return null } } + +/** + * Normalize a git URL to a standard format. + * @param url The git URL to normalize. + * @returns The normalized git URL. + */ +function normalizeGitUrl(url: string): string { + if (url.startsWith('ssh://')) { + return url + .replace(/^ssh:\/\/git@/, 'https://') + .replace(/\.git$/, '') + .replace(/:\d+/, '') + } + return url +}