From b1cea2359e0e006158644c07a583fae4a5293eb8 Mon Sep 17 00:00:00 2001 From: addisonbeck Date: Sat, 9 Aug 2025 12:00:44 -0400 Subject: [PATCH] refactor: introduce @bitwarden/device-type --- .github/CODEOWNERS | 1 + libs/common/src/enums/device-type.enum.ts | 71 +---------------------- libs/device-type/README.md | 5 ++ libs/device-type/eslint.config.mjs | 3 + libs/device-type/jest.config.js | 10 ++++ libs/device-type/package.json | 11 ++++ libs/device-type/project.json | 33 +++++++++++ libs/device-type/src/device-type.spec.ts | 8 +++ libs/device-type/src/index.ts | 70 ++++++++++++++++++++++ libs/device-type/tsconfig.eslint.json | 6 ++ libs/device-type/tsconfig.json | 13 +++++ libs/device-type/tsconfig.lib.json | 10 ++++ libs/device-type/tsconfig.spec.json | 10 ++++ package-lock.json | 9 +++ tsconfig.base.json | 1 + 15 files changed, 191 insertions(+), 70 deletions(-) create mode 100644 libs/device-type/README.md create mode 100644 libs/device-type/eslint.config.mjs create mode 100644 libs/device-type/jest.config.js create mode 100644 libs/device-type/package.json create mode 100644 libs/device-type/project.json create mode 100644 libs/device-type/src/device-type.spec.ts create mode 100644 libs/device-type/src/index.ts create mode 100644 libs/device-type/tsconfig.eslint.json create mode 100644 libs/device-type/tsconfig.json create mode 100644 libs/device-type/tsconfig.lib.json create mode 100644 libs/device-type/tsconfig.spec.json diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 2665f345568..c6cb1c5689f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -102,6 +102,7 @@ libs/client-type @bitwarden/team-platform-dev libs/core-test-utils @bitwarden/team-platform-dev libs/state @bitwarden/team-platform-dev libs/state-test-utils @bitwarden/team-platform-dev +libs/device-type @bitwarden/team-platform-dev # Web utils used across app and connectors apps/web/src/utils/ @bitwarden/team-platform-dev # Web core and shared files diff --git a/libs/common/src/enums/device-type.enum.ts b/libs/common/src/enums/device-type.enum.ts index f7215ac7446..e07582c6346 100644 --- a/libs/common/src/enums/device-type.enum.ts +++ b/libs/common/src/enums/device-type.enum.ts @@ -1,70 +1 @@ -// FIXME: update to use a const object instead of a typescript enum -// eslint-disable-next-line @bitwarden/platform/no-enums -export enum DeviceType { - Android = 0, - iOS = 1, - ChromeExtension = 2, - FirefoxExtension = 3, - OperaExtension = 4, - EdgeExtension = 5, - WindowsDesktop = 6, - MacOsDesktop = 7, - LinuxDesktop = 8, - ChromeBrowser = 9, - FirefoxBrowser = 10, - OperaBrowser = 11, - EdgeBrowser = 12, - IEBrowser = 13, - UnknownBrowser = 14, - AndroidAmazon = 15, - UWP = 16, - SafariBrowser = 17, - VivaldiBrowser = 18, - VivaldiExtension = 19, - SafariExtension = 20, - SDK = 21, - Server = 22, - WindowsCLI = 23, - MacOsCLI = 24, - LinuxCLI = 25, - DuckDuckGoBrowser = 26, -} - -/** - * Device type metadata - * Each device type has a category corresponding to the client type and platform (Android, iOS, Chrome, Firefox, etc.) - */ -interface DeviceTypeMetadata { - category: "mobile" | "extension" | "webApp" | "desktop" | "cli" | "sdk" | "server"; - platform: string; -} - -export const DeviceTypeMetadata: Record = { - [DeviceType.Android]: { category: "mobile", platform: "Android" }, - [DeviceType.iOS]: { category: "mobile", platform: "iOS" }, - [DeviceType.AndroidAmazon]: { category: "mobile", platform: "Amazon" }, - [DeviceType.ChromeExtension]: { category: "extension", platform: "Chrome" }, - [DeviceType.FirefoxExtension]: { category: "extension", platform: "Firefox" }, - [DeviceType.OperaExtension]: { category: "extension", platform: "Opera" }, - [DeviceType.EdgeExtension]: { category: "extension", platform: "Edge" }, - [DeviceType.VivaldiExtension]: { category: "extension", platform: "Vivaldi" }, - [DeviceType.SafariExtension]: { category: "extension", platform: "Safari" }, - [DeviceType.ChromeBrowser]: { category: "webApp", platform: "Chrome" }, - [DeviceType.FirefoxBrowser]: { category: "webApp", platform: "Firefox" }, - [DeviceType.OperaBrowser]: { category: "webApp", platform: "Opera" }, - [DeviceType.EdgeBrowser]: { category: "webApp", platform: "Edge" }, - [DeviceType.IEBrowser]: { category: "webApp", platform: "IE" }, - [DeviceType.SafariBrowser]: { category: "webApp", platform: "Safari" }, - [DeviceType.VivaldiBrowser]: { category: "webApp", platform: "Vivaldi" }, - [DeviceType.DuckDuckGoBrowser]: { category: "webApp", platform: "DuckDuckGo" }, - [DeviceType.UnknownBrowser]: { category: "webApp", platform: "Unknown" }, - [DeviceType.WindowsDesktop]: { category: "desktop", platform: "Windows" }, - [DeviceType.MacOsDesktop]: { category: "desktop", platform: "macOS" }, - [DeviceType.LinuxDesktop]: { category: "desktop", platform: "Linux" }, - [DeviceType.UWP]: { category: "desktop", platform: "Windows UWP" }, - [DeviceType.WindowsCLI]: { category: "cli", platform: "Windows" }, - [DeviceType.MacOsCLI]: { category: "cli", platform: "macOS" }, - [DeviceType.LinuxCLI]: { category: "cli", platform: "Linux" }, - [DeviceType.SDK]: { category: "sdk", platform: "" }, - [DeviceType.Server]: { category: "server", platform: "" }, -}; +export { DeviceType, DeviceTypeMetadata } from "@bitwarden/device-type"; diff --git a/libs/device-type/README.md b/libs/device-type/README.md new file mode 100644 index 00000000000..73b3ef5703f --- /dev/null +++ b/libs/device-type/README.md @@ -0,0 +1,5 @@ +# device-type + +Owned by: platform + +Constants and helpers for working with specific device types diff --git a/libs/device-type/eslint.config.mjs b/libs/device-type/eslint.config.mjs new file mode 100644 index 00000000000..9c37d10e3ff --- /dev/null +++ b/libs/device-type/eslint.config.mjs @@ -0,0 +1,3 @@ +import baseConfig from "../../eslint.config.mjs"; + +export default [...baseConfig]; diff --git a/libs/device-type/jest.config.js b/libs/device-type/jest.config.js new file mode 100644 index 00000000000..350abd9801a --- /dev/null +++ b/libs/device-type/jest.config.js @@ -0,0 +1,10 @@ +module.exports = { + displayName: "device-type", + preset: "../../jest.preset.js", + testEnvironment: "node", + transform: { + "^.+\\.[tj]s$": ["ts-jest", { tsconfig: "/tsconfig.spec.json" }], + }, + moduleFileExtensions: ["ts", "js", "html"], + coverageDirectory: "../../coverage/libs/device-type", +}; diff --git a/libs/device-type/package.json b/libs/device-type/package.json new file mode 100644 index 00000000000..61e1d9665ed --- /dev/null +++ b/libs/device-type/package.json @@ -0,0 +1,11 @@ +{ + "name": "@bitwarden/device-type", + "version": "0.0.1", + "description": "Constants and helpers for working with specific device types", + "private": true, + "type": "commonjs", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "license": "GPL-3.0", + "author": "platform" +} diff --git a/libs/device-type/project.json b/libs/device-type/project.json new file mode 100644 index 00000000000..213695af257 --- /dev/null +++ b/libs/device-type/project.json @@ -0,0 +1,33 @@ +{ + "name": "device-type", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/device-type/src", + "projectType": "library", + "tags": [], + "targets": { + "build": { + "executor": "@nx/js:tsc", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/libs/device-type", + "main": "libs/device-type/src/index.ts", + "tsConfig": "libs/device-type/tsconfig.lib.json", + "assets": ["libs/device-type/*.md"] + } + }, + "lint": { + "executor": "@nx/eslint:lint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["libs/device-type/**/*.ts"] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "libs/device-type/jest.config.js" + } + } + } +} diff --git a/libs/device-type/src/device-type.spec.ts b/libs/device-type/src/device-type.spec.ts new file mode 100644 index 00000000000..4ee09cb9229 --- /dev/null +++ b/libs/device-type/src/device-type.spec.ts @@ -0,0 +1,8 @@ +import * as lib from "./index"; + +describe("device-type", () => { + // This test will fail until something is exported from index.ts + it("should work", () => { + expect(lib).toBeDefined(); + }); +}); diff --git a/libs/device-type/src/index.ts b/libs/device-type/src/index.ts new file mode 100644 index 00000000000..f7215ac7446 --- /dev/null +++ b/libs/device-type/src/index.ts @@ -0,0 +1,70 @@ +// FIXME: update to use a const object instead of a typescript enum +// eslint-disable-next-line @bitwarden/platform/no-enums +export enum DeviceType { + Android = 0, + iOS = 1, + ChromeExtension = 2, + FirefoxExtension = 3, + OperaExtension = 4, + EdgeExtension = 5, + WindowsDesktop = 6, + MacOsDesktop = 7, + LinuxDesktop = 8, + ChromeBrowser = 9, + FirefoxBrowser = 10, + OperaBrowser = 11, + EdgeBrowser = 12, + IEBrowser = 13, + UnknownBrowser = 14, + AndroidAmazon = 15, + UWP = 16, + SafariBrowser = 17, + VivaldiBrowser = 18, + VivaldiExtension = 19, + SafariExtension = 20, + SDK = 21, + Server = 22, + WindowsCLI = 23, + MacOsCLI = 24, + LinuxCLI = 25, + DuckDuckGoBrowser = 26, +} + +/** + * Device type metadata + * Each device type has a category corresponding to the client type and platform (Android, iOS, Chrome, Firefox, etc.) + */ +interface DeviceTypeMetadata { + category: "mobile" | "extension" | "webApp" | "desktop" | "cli" | "sdk" | "server"; + platform: string; +} + +export const DeviceTypeMetadata: Record = { + [DeviceType.Android]: { category: "mobile", platform: "Android" }, + [DeviceType.iOS]: { category: "mobile", platform: "iOS" }, + [DeviceType.AndroidAmazon]: { category: "mobile", platform: "Amazon" }, + [DeviceType.ChromeExtension]: { category: "extension", platform: "Chrome" }, + [DeviceType.FirefoxExtension]: { category: "extension", platform: "Firefox" }, + [DeviceType.OperaExtension]: { category: "extension", platform: "Opera" }, + [DeviceType.EdgeExtension]: { category: "extension", platform: "Edge" }, + [DeviceType.VivaldiExtension]: { category: "extension", platform: "Vivaldi" }, + [DeviceType.SafariExtension]: { category: "extension", platform: "Safari" }, + [DeviceType.ChromeBrowser]: { category: "webApp", platform: "Chrome" }, + [DeviceType.FirefoxBrowser]: { category: "webApp", platform: "Firefox" }, + [DeviceType.OperaBrowser]: { category: "webApp", platform: "Opera" }, + [DeviceType.EdgeBrowser]: { category: "webApp", platform: "Edge" }, + [DeviceType.IEBrowser]: { category: "webApp", platform: "IE" }, + [DeviceType.SafariBrowser]: { category: "webApp", platform: "Safari" }, + [DeviceType.VivaldiBrowser]: { category: "webApp", platform: "Vivaldi" }, + [DeviceType.DuckDuckGoBrowser]: { category: "webApp", platform: "DuckDuckGo" }, + [DeviceType.UnknownBrowser]: { category: "webApp", platform: "Unknown" }, + [DeviceType.WindowsDesktop]: { category: "desktop", platform: "Windows" }, + [DeviceType.MacOsDesktop]: { category: "desktop", platform: "macOS" }, + [DeviceType.LinuxDesktop]: { category: "desktop", platform: "Linux" }, + [DeviceType.UWP]: { category: "desktop", platform: "Windows UWP" }, + [DeviceType.WindowsCLI]: { category: "cli", platform: "Windows" }, + [DeviceType.MacOsCLI]: { category: "cli", platform: "macOS" }, + [DeviceType.LinuxCLI]: { category: "cli", platform: "Linux" }, + [DeviceType.SDK]: { category: "sdk", platform: "" }, + [DeviceType.Server]: { category: "server", platform: "" }, +}; diff --git a/libs/device-type/tsconfig.eslint.json b/libs/device-type/tsconfig.eslint.json new file mode 100644 index 00000000000..3daf120441a --- /dev/null +++ b/libs/device-type/tsconfig.eslint.json @@ -0,0 +1,6 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": ["src/**/*.ts", "src/**/*.js"], + "exclude": ["**/build", "**/dist"] +} diff --git a/libs/device-type/tsconfig.json b/libs/device-type/tsconfig.json new file mode 100644 index 00000000000..62ebbd94647 --- /dev/null +++ b/libs/device-type/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/device-type/tsconfig.lib.json b/libs/device-type/tsconfig.lib.json new file mode 100644 index 00000000000..9cbf6736007 --- /dev/null +++ b/libs/device-type/tsconfig.lib.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["jest.config.js", "src/**/*.spec.ts"] +} diff --git a/libs/device-type/tsconfig.spec.json b/libs/device-type/tsconfig.spec.json new file mode 100644 index 00000000000..1275f148a18 --- /dev/null +++ b/libs/device-type/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "moduleResolution": "node10", + "types": ["jest", "node"] + }, + "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"] +} diff --git a/package-lock.json b/package-lock.json index 3bf9db97ddb..a24a9fd2aa9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -337,6 +337,11 @@ "version": "0.0.1", "license": "GPL-3.0" }, + "libs/device-type": { + "name": "@bitwarden/device-type", + "version": "0.0.1", + "license": "GPL-3.0" + }, "libs/dirt/card": { "name": "@bitwarden/dirt-card", "version": "0.0.0", @@ -4599,6 +4604,10 @@ "resolved": "apps/desktop/desktop_native/napi", "link": true }, + "node_modules/@bitwarden/device-type": { + "resolved": "libs/device-type", + "link": true + }, "node_modules/@bitwarden/dirt-card": { "resolved": "libs/dirt/card", "link": true diff --git a/tsconfig.base.json b/tsconfig.base.json index c1256383915..7379b940052 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -29,6 +29,7 @@ "@bitwarden/common/*": ["./libs/common/src/*"], "@bitwarden/components": ["./libs/components/src"], "@bitwarden/core-test-utils": ["libs/core-test-utils/src/index.ts"], + "@bitwarden/device-type": ["libs/device-type/src/index.ts"], "@bitwarden/dirt-card": ["./libs/dirt/card/src"], "@bitwarden/generator-components": ["./libs/tools/generator/components/src"], "@bitwarden/generator-core": ["./libs/tools/generator/core/src"],