From 1b44ae098cbf0e056c7c51b80d75c8f3c3c32e48 Mon Sep 17 00:00:00 2001 From: GyDi Date: Sat, 14 Jan 2023 12:07:31 +0800 Subject: [PATCH] fix: add os platform value --- src/pages/_layout.tsx | 4 ---- src/services/types.d.ts | 19 +++++++++++++++++++ src/utils/get-system.ts | 5 +++-- vite.config.ts | 1 + 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/pages/_layout.tsx b/src/pages/_layout.tsx index 7756d80..4b52ace 100644 --- a/src/pages/_layout.tsx +++ b/src/pages/_layout.tsx @@ -24,10 +24,6 @@ import useCustomTheme from "@/components/layout/use-custom-theme"; import getSystem from "@/utils/get-system"; import "dayjs/locale/zh-cn"; -declare global { - const WIN_PORTABLE: boolean; -} - dayjs.extend(relativeTime); const OS = getSystem(); diff --git a/src/services/types.d.ts b/src/services/types.d.ts index de3541d..749cffd 100644 --- a/src/services/types.d.ts +++ b/src/services/types.d.ts @@ -1,3 +1,22 @@ +type Platform = + | "aix" + | "android" + | "darwin" + | "freebsd" + | "haiku" + | "linux" + | "openbsd" + | "sunos" + | "win32" + | "cygwin" + | "netbsd"; + +/** + * defines in `vite.config.ts` + */ +declare const WIN_PORTABLE: boolean; +declare const OS_PLATFORM: Platform; + /** * Some interface for clash api */ diff --git a/src/utils/get-system.ts b/src/utils/get-system.ts index 8801617..8917703 100644 --- a/src/utils/get-system.ts +++ b/src/utils/get-system.ts @@ -2,10 +2,11 @@ // according to UA export default function getSystem() { const ua = navigator.userAgent; + const platform = OS_PLATFORM; - if (ua.includes("Mac OS X")) return "macos"; + if (ua.includes("Mac OS X") || platform === "darwin") return "macos"; - if (/win64|win32/i.test(ua)) return "windows"; + if (/win64|win32/i.test(ua) || platform === "win32") return "windows"; if (/linux/i.test(ua)) return "linux"; diff --git a/vite.config.ts b/vite.config.ts index 4a51b76..0315d76 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -24,6 +24,7 @@ export default defineConfig({ }, }, define: { + OS_PLATFORM: `"${process.platform}"`, WIN_PORTABLE: !!process.env.VITE_WIN_PORTABLE, }, });