Skip to content

Commit

Permalink
feat(utils): add platform utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Jan 7, 2024
1 parent aaa9551 commit 3c62595
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/web/constants/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./celerisWebConstants";
export * from "./commonConstants";
export * from "./pageConstants";
export * from "./platformConstance";
export * from "./storageConstants";
export * from "./requestConstants";
export * from "./systemConstants";
Expand Down
9 changes: 9 additions & 0 deletions packages/web/constants/src/platformConstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum OperatingSystem {
Windows = "Windows",
MacOS = "MacOS",
UNIX = "UNIX",
Linux = "Linux",
Unknown = "Unknown",
}

export type OS = keyof typeof OperatingSystem;
1 change: 1 addition & 0 deletions packages/web/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
71 changes: 71 additions & 0 deletions packages/web/utils/src/platformUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

2 comments on commit 3c62595

@vercel
Copy link

@vercel vercel bot commented on 3c62595 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web – ./apps/admin

celeris-web-git-master-kirklin.vercel.app
celeris-web-kirklin.vercel.app
celeris-web.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3c62595 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web-api – ./services/admin

celeris-web-api-git-master-kirklin.vercel.app
celeris-web-api.vercel.app
celeris-web-api-kirklin.vercel.app

Please sign in to comment.