From 995e0bbdacc1e982e270b987bea57aa36a0495b4 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Mon, 10 Jun 2024 17:07:11 +0800 Subject: [PATCH] build: allow build ui without git info --- ui/common.ts | 29 +++++++++++++++++------------ ui/src/tool/utils.ts | 2 +- ui/vite.config-explorer.ts | 7 ++++++- ui/vite.config.ts | 34 +++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/ui/common.ts b/ui/common.ts index fea8de6442..808852acf3 100644 --- a/ui/common.ts +++ b/ui/common.ts @@ -20,19 +20,24 @@ export function getSha(): string | undefined { return envs.GITHUB_SHA; } -export function getVersionTag(): HtmlTagDescriptor { - const commitDate = execSync('git log -1 --format=%cI').toString().trimEnd(); - const branchName = execSync('git rev-parse --abbrev-ref HEAD').toString().trimEnd(); - const commitHash = execSync('git rev-parse HEAD').toString().trimEnd(); - const lastCommitMessage = execSync('git show -s --format=%s').toString().trimEnd(); +export function getVersionTag(): HtmlTagDescriptor | null { + try { + const commitDate = execSync('git log -1 --format=%cI').toString().trimEnd(); + const branchName = execSync('git rev-parse --abbrev-ref HEAD').toString().trimEnd(); + const commitHash = execSync('git rev-parse HEAD').toString().trimEnd(); + const lastCommitMessage = execSync('git show -s --format=%s').toString().trimEnd(); - return { - tag: "script", - children: - `console.info("Branch: ${branchName}");` + - `console.info("Commit: ${commitHash}");` + - `console.info("Date: ${commitDate}");` + - `console.info("Message: ${lastCommitMessage}");`, + return { + tag: "script", + children: + `console.info("Branch: ${branchName}");` + + `console.info("Commit: ${commitHash}");` + + `console.info("Date: ${commitDate}");` + + `console.info("Message: ${lastCommitMessage}");`, + } + } catch (e) { + console.warn("Failed to get git info", e) + return null; } } diff --git a/ui/src/tool/utils.ts b/ui/src/tool/utils.ts index 5d27643bae..ee9917d5bb 100644 --- a/ui/src/tool/utils.ts +++ b/ui/src/tool/utils.ts @@ -1,4 +1,4 @@ -import {parseResult, Result} from "../generated/schema.ts"; +import {Result, parseResult} from "../generated/schema.ts"; export function loadDataFromEmbed(): Result { const doc = document.querySelector("#data")!; diff --git a/ui/vite.config-explorer.ts b/ui/vite.config-explorer.ts index 11ed1b812d..4873b2cf39 100644 --- a/ui/vite.config-explorer.ts +++ b/ui/vite.config-explorer.ts @@ -2,7 +2,12 @@ import {defineConfig} from 'vite'; import {build, codecov, commonPlugin, getVersionTag} from "./common"; import {createHtmlPlugin} from "vite-plugin-html"; -const tags = [getVersionTag()]; +const tags = []; +const versionTag = getVersionTag(); +if (versionTag) { + tags.push(versionTag); +} + if (process.env.GSA_TELEMETRY) { tags.push({ tag: "script", diff --git a/ui/vite.config.ts b/ui/vite.config.ts index ee6f88fecb..a248b7d97c 100644 --- a/ui/vite.config.ts +++ b/ui/vite.config.ts @@ -1,4 +1,4 @@ -import {defineConfig} from 'vite'; +import {HtmlTagDescriptor, defineConfig} from 'vite'; import {viteSingleFile} from "vite-plugin-singlefile" import * as fs from "node:fs" import {build, codecov, commonPlugin, getVersionTag} from "./common"; @@ -23,26 +23,30 @@ const getPlaceHolder = (): string => { } } +const tags: HtmlTagDescriptor[] = [ + { + injectTo: "head", + tag: "script", + attrs: { + type: "application/json", + id: "data" + }, + children: getPlaceHolder() + }, + +] +const versionTag = getVersionTag(); +if (versionTag) { + tags.push(versionTag); +} + export default defineConfig({ plugins: [ ...commonPlugin(), createHtmlPlugin({ minify: true, entry: './src/main.tsx', - inject: { - tags: [ - { - injectTo: "head", - tag: "script", - attrs: { - type: "application/json", - id: "data" - }, - children: getPlaceHolder() - }, - getVersionTag(), - ] - } + inject: {tags} }), viteSingleFile( {