refactor: get short commit

This commit is contained in:
三咲智子 Kevin Deng 2023-02-23 19:17:28 +08:00
parent f98c667613
commit bcf0965795
No known key found for this signature in database
GPG key ID: 69992F2250DFD93E
6 changed files with 11 additions and 8 deletions

View file

@ -14,7 +14,7 @@ const build = useBuildInfo()
<p> <p>
<i18n-t keypath="help.build_preview.desc1"> <i18n-t keypath="help.build_preview.desc1">
<NuxtLink :href="`https://github.com/elk-zone/elk/commit/${build.commit}`" target="_blank" text-rose hover:underline> <NuxtLink :href="`https://github.com/elk-zone/elk/commit/${build.commit}`" target="_blank" text-rose hover:underline>
<code>{{ build.commit.slice(0, 7) }}</code> <code>{{ build.shortCommit }}</code>
</NuxtLink> </NuxtLink>
</i18n-t> </i18n-t>
</p> </p>

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
const buildInfo = useAppConfig().buildInfo const buildInfo = useBuildInfo()
const timeAgoOptions = useTimeAgoOptions() const timeAgoOptions = useTimeAgoOptions()
const userSettings = useUserSettings() const userSettings = useUserSettings()
@ -65,7 +65,7 @@ function toggleDark() {
target="_blank" target="_blank"
font-mono font-mono
> >
{{ buildInfo.commit.slice(0, 7) }} {{ buildInfo.shortCommit }}
</NuxtLink> </NuxtLink>
</template> </template>
</div> </div>

View file

@ -31,11 +31,12 @@ const git = Git()
export const getGitInfo = async () => { export const getGitInfo = async () => {
const branch = gitBranch || await git.revparse(['--abbrev-ref', 'HEAD']) const branch = gitBranch || await git.revparse(['--abbrev-ref', 'HEAD'])
const commit = await git.revparse(['HEAD']) const commit = await git.revparse(['HEAD'])
return { branch, commit } const shortCommit = await git.revparse(['--short=7', 'HEAD'])
return { branch, commit, shortCommit }
} }
export const getEnv = async () => { export const getEnv = async () => {
const { commit, branch } = await getGitInfo() const { commit, shortCommit, branch } = await getGitInfo()
const env = isDevelopment const env = isDevelopment
? 'dev' ? 'dev'
: isPreview : isPreview
@ -43,5 +44,5 @@ export const getEnv = async () => {
: branch === 'main' : branch === 'main'
? 'canary' ? 'canary'
: 'release' : 'release'
return { commit, branch, env } as const return { commit, shortCommit, branch, env } as const
} }

View file

@ -10,11 +10,12 @@ export default defineNuxtModule({
name: 'elk:build-env', name: 'elk:build-env',
}, },
async setup(_options, nuxt) { async setup(_options, nuxt) {
const { env, commit, branch } = await getEnv() const { env, commit, shortCommit, branch } = await getEnv()
const buildInfo: BuildInfo = { const buildInfo: BuildInfo = {
version, version,
time: +Date.now(), time: +Date.now(),
commit, commit,
shortCommit,
branch, branch,
env, env,
} }

View file

@ -41,7 +41,7 @@ const handleShowCommit = () => {
<template #content> <template #content>
<div font-mono> <div font-mono>
<span>{{ buildInfo.env === 'release' ? `v${buildInfo.version}` : buildInfo.env }}</span> <span>{{ buildInfo.env === 'release' ? `v${buildInfo.version}` : buildInfo.env }}</span>
<span v-if="showCommit"> ({{ buildInfo.commit.slice(0, 7) }}@{{ buildInfo.branch }})</span> <span v-if="showCommit"> ({{ buildInfo.shortCommit }}@{{ buildInfo.branch }})</span>
</div> </div>
</template> </template>
</SettingsItem> </SettingsItem>

View file

@ -72,6 +72,7 @@ export interface ErrorDialogData {
export interface BuildInfo { export interface BuildInfo {
version: string version: string
commit: string commit: string
shortCommit: string
time: number time: number
branch: string branch: string
env: 'preview' | 'canary' | 'dev' | 'release' env: 'preview' | 'canary' | 'dev' | 'release'