From a53c2ce5da29db88ba1556f862207e381b41b3b8 Mon Sep 17 00:00:00 2001 From: Kirk Lin Date: Sun, 7 Jan 2024 21:10:33 +0800 Subject: [PATCH] feat(utils): add platform utils --- packages/web/constants/src/index.ts | 1 + .../web/constants/src/platformConstants.ts | 9 +++ packages/web/utils/src/index.ts | 1 + packages/web/utils/src/platformUtils.ts | 71 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 packages/web/constants/src/platformConstants.ts create mode 100644 packages/web/utils/src/platformUtils.ts diff --git a/packages/web/constants/src/index.ts b/packages/web/constants/src/index.ts index 92f0c9e7..1596abc7 100644 --- a/packages/web/constants/src/index.ts +++ b/packages/web/constants/src/index.ts @@ -1,6 +1,7 @@ export * from "./celerisWebConstants"; export * from "./commonConstants"; export * from "./pageConstants"; +export * from "./platformConstants"; export * from "./storageConstants"; export * from "./requestConstants"; export * from "./systemConstants"; diff --git a/packages/web/constants/src/platformConstants.ts b/packages/web/constants/src/platformConstants.ts new file mode 100644 index 00000000..4173d11b --- /dev/null +++ b/packages/web/constants/src/platformConstants.ts @@ -0,0 +1,9 @@ +export enum OperatingSystem { + Windows = "Windows", + MacOS = "MacOS", + UNIX = "UNIX", + Linux = "Linux", + Unknown = "Unknown", +} + +export type OS = keyof typeof OperatingSystem; diff --git a/packages/web/utils/src/index.ts b/packages/web/utils/src/index.ts index 6b247d85..11a15534 100644 --- a/packages/web/utils/src/index.ts +++ b/packages/web/utils/src/index.ts @@ -7,6 +7,7 @@ export * from "./domUtils"; export * from "./mitt"; export * from "./mock"; export * from "./moduleHelper"; +export * from "./platformUtils"; export * from "./menuHelper"; export * from "./router"; export * from "./typeChecks"; diff --git a/packages/web/utils/src/platformUtils.ts b/packages/web/utils/src/platformUtils.ts new file mode 100644 index 00000000..0f866b5c --- /dev/null +++ b/packages/web/utils/src/platformUtils.ts @@ -0,0 +1,71 @@ +import type { OS } from "@celeris/constants"; +import { OperatingSystem } from "@celeris/constants"; + +/** + * Retrieves the operating system (OS) of the current environment based on the user agent. + * 基于用户代理检索当前环境的操作系统(OS)。 + * + * @returns {OS} The operating system of the current environment. + * 当前环境的操作系统。 + */ +export function detectOperatingSystem(): OS { + const { userAgent } = navigator; + if (userAgent.includes("Win")) { + return OperatingSystem.Windows; + } + if (userAgent.includes("Mac")) { + return OperatingSystem.MacOS; + } + if (userAgent.includes("X11")) { + return OperatingSystem.UNIX; + } + if (userAgent.includes("Linux")) { + return OperatingSystem.Linux; + } + + return OperatingSystem.Unknown; +} + +/** + * Checks if the current environment is Windows. + * 检查当前环境是否为 Windows。 + * + * @returns {boolean} Returns true if the current environment is Windows, otherwise returns false. + * 如果当前环境为 Windows,则返回 true;否则返回 false。 + */ +export function isWindows(): boolean { + return detectOperatingSystem() === OperatingSystem.Windows; +} + +/** + * Checks if the current environment is MacOS. + * 检查当前环境是否为 MacOS。 + * + * @returns {boolean} Returns true if the current environment is MacOS, otherwise returns false. + * 如果当前环境为 MacOS,则返回 true;否则返回 false。 + */ +export function isMacOS(): boolean { + return detectOperatingSystem() === OperatingSystem.MacOS; +} + +/** + * Checks if the current environment is UNIX-based. + * 检查当前环境是否为基于 UNIX 的系统。 + * + * @returns {boolean} Returns true if the current environment is UNIX-based, otherwise returns false. + * 如果当前环境为基于 UNIX 的系统,则返回 true;否则返回 false。 + */ +export function isUnix(): boolean { + return detectOperatingSystem() === OperatingSystem.UNIX; +} + +/** + * Checks if the current environment is Linux. + * 检查当前环境是否为 Linux。 + * + * @returns {boolean} Returns true if the current environment is Linux, otherwise returns false. + * 如果当前环境为 Linux,则返回 true;否则返回 false。 + */ +export function isLinux(): boolean { + return detectOperatingSystem() === OperatingSystem.Linux; +}