Maybe made the commit info work in prod?
This commit is contained in:
parent
3941d77d10
commit
2c660bae02
6 changed files with 58 additions and 56 deletions
|
|
@ -6,11 +6,7 @@ import { usePathname } from 'next/navigation'
|
||||||
import { ExternalLink } from '@/components/ui/ExternalLink'
|
import { ExternalLink } from '@/components/ui/ExternalLink'
|
||||||
import { navItems } from '@/components/layout/navItems'
|
import { navItems } from '@/components/layout/navItems'
|
||||||
|
|
||||||
type NavLinksProps = {
|
export function NavLinks() {
|
||||||
gitOriginUrl: string | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function NavLinks({ gitOriginUrl }: NavLinksProps) {
|
|
||||||
const pathname = usePathname()
|
const pathname = usePathname()
|
||||||
|
|
||||||
const isActive = (href: string) => {
|
const isActive = (href: string) => {
|
||||||
|
|
@ -30,11 +26,7 @@ export function NavLinks({ gitOriginUrl }: NavLinksProps) {
|
||||||
>
|
>
|
||||||
{item.isExternal ? (
|
{item.isExternal ? (
|
||||||
<ExternalLink
|
<ExternalLink
|
||||||
href={
|
href={item.href}
|
||||||
item.href.includes('{gitOriginUrl}')
|
|
||||||
? item.href.replace('{gitOriginUrl}', gitOriginUrl ?? '')
|
|
||||||
: item.href
|
|
||||||
}
|
|
||||||
className="flex items-center gap-2 px-2 py-1 hover:no-underline focus:no-underline"
|
className="flex items-center gap-2 px-2 py-1 hover:no-underline focus:no-underline"
|
||||||
>
|
>
|
||||||
{item.icon}
|
{item.icon}
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ export async function Sidebar() {
|
||||||
<span>{metadata.title!.toString()}</span>
|
<span>{metadata.title!.toString()}</span>
|
||||||
</h2>
|
</h2>
|
||||||
<ul className="space-y-2">
|
<ul className="space-y-2">
|
||||||
<NavLinks gitOriginUrl={gitOriginUrl} />
|
<NavLinks />
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-auto flex gap-4 mx-auto">
|
<div className="mt-auto flex gap-4 mx-auto">
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,14 @@ export const navItems: NavItem[] = [
|
||||||
icon: <Rss className={navIconsClassName} />,
|
icon: <Rss className={navIconsClassName} />,
|
||||||
isExternal: true,
|
isExternal: true,
|
||||||
},
|
},
|
||||||
|
...(process.env.GIT_ORIGIN_URL
|
||||||
|
? [
|
||||||
{
|
{
|
||||||
href: '{gitOriginUrl}',
|
href: process.env.GIT_ORIGIN_URL,
|
||||||
label: 'Source Code',
|
label: 'Source Code',
|
||||||
icon: <GitBranch className={navIconsClassName} />,
|
icon: <GitBranch className={navIconsClassName} />,
|
||||||
isExternal: true,
|
isExternal: true,
|
||||||
},
|
},
|
||||||
|
]
|
||||||
|
: []),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@ const nextConfig: NextConfig = {
|
||||||
images: {
|
images: {
|
||||||
qualities: [75, 100],
|
qualities: [75, 100],
|
||||||
},
|
},
|
||||||
|
env: {
|
||||||
|
GIT_COMMIT_HASH: process.env.GIT_COMMIT_HASH,
|
||||||
|
GIT_ORIGIN_URL: process.env.GIT_ORIGIN_URL,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export default nextConfig
|
export default nextConfig
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
[phases.setup]
|
[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"]
|
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"]
|
||||||
74
utils/git.ts
74
utils/git.ts
|
|
@ -1,53 +1,55 @@
|
||||||
import { exec } from 'child_process'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current Git commit hash.
|
* Get the current git commit hash.
|
||||||
* @returns The current Git commit hash as a string, or null if an error occurs.
|
* @returns The current git commit hash, or null if it cannot be determined.
|
||||||
*/
|
*/
|
||||||
export async function getGitCommitHash(): Promise<string | null> {
|
export async function getGitCommitHash(): Promise<string | null> {
|
||||||
try {
|
if (process.env.GIT_COMMIT_HASH) {
|
||||||
const commitHash = (await new Promise((resolve, reject) => {
|
return process.env.GIT_COMMIT_HASH
|
||||||
exec('git rev-parse HEAD', (error, stdout) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error)
|
|
||||||
} else {
|
|
||||||
resolve(stdout.toString().trim())
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
})) as string
|
|
||||||
|
|
||||||
return commitHash
|
try {
|
||||||
} catch (error) {
|
const { exec } = await import('child_process')
|
||||||
console.error('Error getting Git commit hash:', error)
|
const hash = await new Promise<string>((res, rej) => {
|
||||||
|
exec('git rev-parse HEAD', (err, stdout) => (err ? rej(err) : res(stdout.trim())))
|
||||||
|
})
|
||||||
|
return hash
|
||||||
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Git origin URL.
|
* Get the git origin URL.
|
||||||
* @returns The Git origin URL as a string, or null if an error occurs.
|
* @returns The git origin URL, or null if it cannot be determined.
|
||||||
*/
|
*/
|
||||||
export async function getGitOriginUrl(): Promise<string | null> {
|
export async function getGitOriginUrl(): Promise<string | null> {
|
||||||
|
const envUrl = process.env.GIT_ORIGIN_URL
|
||||||
|
if (envUrl) return normalizeGitUrl(envUrl)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const originUrl = (await new Promise((resolve, reject) => {
|
const { exec } = await import('child_process')
|
||||||
exec('git config --get remote.origin.url', (error, stdout) => {
|
const url = await new Promise<string>((res, rej) => {
|
||||||
if (error) {
|
exec('git config --get remote.origin.url', (err, stdout) =>
|
||||||
reject(error)
|
err ? rej(err) : res(stdout.trim()),
|
||||||
} else {
|
)
|
||||||
resolve(stdout.toString().trim())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})) as string
|
return normalizeGitUrl(url)
|
||||||
|
} catch {
|
||||||
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)
|
|
||||||
return null
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue