From 2dd3260dba3d6b2776798306f45f11c27b47b580 Mon Sep 17 00:00:00 2001 From: yu-zhen Date: Tue, 17 Dec 2024 02:46:17 +0800 Subject: [PATCH] feat: add git commit version to organizer dashboard --- packages/interface/package.json | 9 +++--- packages/interface/scripts/genGitHash.js | 31 +++++++++++++++++++ packages/interface/src/components/Footer.tsx | 4 +++ packages/interface/src/config.ts | 1 + packages/interface/src/env.js | 4 +++ .../interface/src/pages/coordinator/index.tsx | 11 ++++++- 6 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 packages/interface/scripts/genGitHash.js diff --git a/packages/interface/package.json b/packages/interface/package.json index 9e2a9408..bd2ed46e 100644 --- a/packages/interface/package.json +++ b/packages/interface/package.json @@ -3,15 +3,16 @@ "version": "0.1.0", "private": true, "scripts": { - "build": "next build", - "dev": "next dev", + "build": "pnpm run git-hash && next build", + "dev": "pnpm run git-hash && next dev", "lint": "next lint", "lint:fix": "next lint --fix", - "start": "next start", + "start": "pnpm run git-hash && next start", "prettier": "prettier -c .", "prettier:fix": "prettier -w .", "types": "tsc -p tsconfig.json --noEmit", - "eas:registerSchemas": "npx tsx src/lib/eas/registerSchemas" + "eas:registerSchemas": "npx tsx src/lib/eas/registerSchemas", + "git-hash": "node ./scripts/genGitHash.js" }, "dependencies": { "@ethereum-attestation-service/eas-sdk": "^2.7.0", diff --git a/packages/interface/scripts/genGitHash.js b/packages/interface/scripts/genGitHash.js new file mode 100644 index 00000000..94c6f65a --- /dev/null +++ b/packages/interface/scripts/genGitHash.js @@ -0,0 +1,31 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const { execSync } = require("child_process"); +const fs = require("fs"); +const path = require("path"); + +const commitHash = execSync('git log --pretty=format:"%h" -n1').toString().trim(); + +const key = "NEXT_PUBLIC_COMMIT_HASH"; + +const envFile = path.resolve(__dirname, "../.env"); + +let data = ""; +if (fs.existsSync(envFile)) { + data = fs.readFileSync(envFile, "utf8"); +} + +const indexOfCommitHash = data.indexOf(key); + +if (indexOfCommitHash === -1) { + fs.writeFileSync(envFile, `${data}\n${key}=${commitHash}\n`, "utf8"); +} else { + const indexOfNextLine = data.indexOf("\n", indexOfCommitHash); + fs.writeFileSync( + envFile, + data + .slice(0, indexOfCommitHash) + .concat(data.slice(indexOfNextLine + 1)) + .concat(`${key}=${commitHash}\n`), + "utf8", + ); +} diff --git a/packages/interface/src/components/Footer.tsx b/packages/interface/src/components/Footer.tsx index 574d6708..340475c5 100644 --- a/packages/interface/src/components/Footer.tsx +++ b/packages/interface/src/components/Footer.tsx @@ -2,6 +2,8 @@ import Image from "next/image"; import { FaGithub, FaDiscord } from "react-icons/fa"; import { FaXTwitter } from "react-icons/fa6"; +import { config } from "~/config"; + import { Logo } from "./ui/Logo"; export const Footer = (): JSX.Element => ( @@ -26,6 +28,8 @@ export const Footer = (): JSX.Element => (
+

Git Version: {config.commitHash}

+ Docs diff --git a/packages/interface/src/config.ts b/packages/interface/src/config.ts index 2690bee0..ffee87a0 100644 --- a/packages/interface/src/config.ts +++ b/packages/interface/src/config.ts @@ -86,6 +86,7 @@ export const config = { roundLogo: process.env.NEXT_PUBLIC_ROUND_LOGO, semaphoreSubgraphUrl: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH, treeUrl: process.env.NEXT_PUBLIC_TREE_URL, + commitHash: process.env.NEXT_PUBLIC_COMMIT_HASH, }; export const theme = { diff --git a/packages/interface/src/env.js b/packages/interface/src/env.js index 7221d7c6..110f7fa8 100644 --- a/packages/interface/src/env.js +++ b/packages/interface/src/env.js @@ -52,6 +52,8 @@ module.exports = createEnv({ NEXT_PUBLIC_SEMAPHORE_SUBGRAPH: z.string().url().optional(), NEXT_PUBLIC_TREE_URL: z.string().url().optional(), + + NEXT_PUBLIC_COMMIT_HASH: z.string(), }, /** @@ -83,6 +85,8 @@ module.exports = createEnv({ NEXT_PUBLIC_SEMAPHORE_SUBGRAPH: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH, NEXT_PUBLIC_TREE_URL: process.env.NEXT_PUBLIC_TREE_URL, + + NEXT_PUBLIC_COMMIT_HASH: process.env.NEXT_PUBLIC_COMMIT_HASH, }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially diff --git a/packages/interface/src/pages/coordinator/index.tsx b/packages/interface/src/pages/coordinator/index.tsx index 0a3995d8..45f2f6fc 100644 --- a/packages/interface/src/pages/coordinator/index.tsx +++ b/packages/interface/src/pages/coordinator/index.tsx @@ -1,5 +1,14 @@ +import { config } from "~/config"; import { Layout } from "~/layouts/DefaultLayout"; -const CoordinatorPage = (): JSX.Element => This is the coordinator page.; +const CoordinatorPage = (): JSX.Element => ( + +
+

This is the coordinator page.

+ +

You are using version with commit: {config.commitHash}

+
+
+); export default CoordinatorPage;