From be726bc246470513eb7368489fc202b787dc6e90 Mon Sep 17 00:00:00 2001 From: SegaraRai Date: Sun, 30 Jun 2024 19:26:16 +0900 Subject: [PATCH] feat: add link to GitHub with current app version text in account menu --- components/AppHeader.vue | 11 +++++++++++ nuxt.config.ts | 35 ++++++++++++++++++++++++++++++----- package.json | 1 + 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/components/AppHeader.vue b/components/AppHeader.vue index 1e5bc90..15e502f 100644 --- a/components/AppHeader.vue +++ b/components/AppHeader.vue @@ -60,6 +60,9 @@ import type { DropdownItem } from "#ui/types"; const { signOut, user } = useAuth(); +const { + public: { appVersion }, +} = useRuntimeConfig(); const toast = useToast(); @@ -89,6 +92,14 @@ const items = computed((): DropdownItem[][] => [ label: user.value?.name ?? "", }, ], + [ + { + label: `Luonto ${appVersion}`, + icon: "i-ph-github-logo-bold", + href: "https://github.com/SegaraRai/luonto", + target: "_blank", + }, + ], [ { label: "サインアウト", diff --git a/nuxt.config.ts b/nuxt.config.ts index e0c8b18..8deaec9 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -1,13 +1,37 @@ -import path from "node:path"; +import { spawnSync } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { env } from "node:process"; +import { fileURLToPath } from "node:url"; import { createNitroSWPreset } from "./nitroSWPreset"; +function fromProjectDir(path: string): string { + return fileURLToPath(new URL(path, import.meta.url)); +} + +function getAppVersion(): string { + if (env.NODE_ENV === "development" || env.NODE_ENV === "test") { + return "[dev]"; + } + + const pkg = JSON.parse(readFileSync(fromProjectDir("package.json"), "utf-8")); + + const { stdout } = spawnSync("git", ["rev-parse", "--short", "HEAD"], { + encoding: "utf-8", + }); + + const pkgVersion = pkg.version; + const commitHash = stdout.trim(); + + return `v${pkgVersion} (${commitHash})`; +} + const nitro = - process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test" + env.NODE_ENV === "development" || env.NODE_ENV === "test" ? {} : { ...createNitroSWPreset({ - swEntry: path.resolve(__dirname, "swEntry.ts"), - prerenderEntry: path.resolve(__dirname, "prerenderEntry.ts"), + swEntry: fromProjectDir("swEntry.ts"), + prerenderEntry: fromProjectDir("prerenderEntry.ts"), fallbackBase: "loading.html", fallbackFiles: ["index.html", "200.html", "404.html"], }), @@ -36,10 +60,11 @@ export default defineNuxtConfig({ authJs: { baseUrl: "https://service-worker", }, + appVersion: getAppVersion(), }, }, alias: { - cookie: path.resolve(__dirname, "node_modules/cookie"), + cookie: fromProjectDir("node_modules/cookie"), }, ui: { safelistColors: [ diff --git a/package.json b/package.json index 70be8ac..4c5506f 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "luonto", + "version": "1.0.0", "private": true, "type": "module", "scripts": {