Skip to content

Commit

Permalink
build(web): get git meta info
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Dec 23, 2024
1 parent f0b97e9 commit 761f491
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'
import pkg from './package.json'

const commitHash = execSync('git rev-parse --short HEAD').toString().trimEnd()
const commitUrl = `https://github.com/Chilfish/Weibo-archiver/commit/${commitHash}`

const commitDate = execSync('git log -1 --format=%cI').toString().trimEnd()
const lastCommitMessage = execSync('git show -s --format=%s').toString().trimEnd()

const root = fileURLToPath(new URL('../../', import.meta.url))
const core = path.join(root, 'packages/core/src')
const shared = path.join(root, 'packages/shared/src')
const ui = path.join(root, 'packages/ui/src')

const { commitHash, commitDate, commitUrl, lastCommitMessage } = getGitInfo()

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
Expand Down Expand Up @@ -69,3 +65,33 @@ export default defineConfig({
},
},
})

function getGitInfo() {
const repoUrl = 'https://github.com/Chilfish/Weibo-archiver/commit'
try {
const commitHash = execSync('git rev-parse --short HEAD').toString().trimEnd()
const commitUrl = `${repoUrl}/${commitHash}`

const commitDate = execSync('git log -1 --format=%cI').toString().trimEnd()
const lastCommitMessage = execSync('git show -s --format=%s').toString().trimEnd()

return { commitHash, commitDate, commitUrl, lastCommitMessage }
}
catch {
// https://vercel.com/docs/projects/environment-variables/system-environment-variables#VERCEL_GIT_COMMIT_SHA
const {
VERCEL_GIT_COMMIT_SHA: commitHash,
VERCEL_GIT_COMMIT_DATE: commitDate,
VERCEL_GIT_COMMIT_MESSAGE: lastCommitMessage,
} = process.env

const shortCommitHash = commitHash?.slice(0, 7)

return {
commitHash: shortCommitHash,
commitDate: commitDate || new Date().toISOString(),
commitUrl: `${repoUrl}/${shortCommitHash}`,
lastCommitMessage,
}
}
}

0 comments on commit 761f491

Please sign in to comment.