From dcc611c7d0d7316d07f2099c602a3212b5d12dd4 Mon Sep 17 00:00:00 2001 From: Angelos Bouklis Date: Fri, 5 Sep 2025 09:28:17 +0300 Subject: [PATCH] feat(refactor): PluginDefinition::platform (#3665) --- package.json | 3 ++- patches/electron-is.patch | 27 +++++++++++++++++++++++ pnpm-lock.yaml | 7 ++++-- src/index.ts | 9 +------- src/plugins/taskbar-mediacontrol/index.ts | 2 ++ src/plugins/touchbar/index.ts | 2 ++ src/reset.d.ts | 2 ++ src/types/plugins.ts | 8 +++++++ vite-plugins/plugin-importer.mts | 25 +++++++++++++++++++-- 9 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 patches/electron-is.patch diff --git a/package.json b/package.json index 3ce60559..de79b5ab 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,8 @@ "vudio@2.1.1": "patches/vudio@2.1.1.patch", "@malept/flatpak-bundler@0.4.0": "patches/@malept__flatpak-bundler@0.4.0.patch", "kuromoji@0.1.2": "patches/kuromoji@0.1.2.patch", - "file-type@16.5.4": "patches/file-type@16.5.4.patch" + "file-type@16.5.4": "patches/file-type@16.5.4.patch", + "electron-is": "patches/electron-is.patch" }, "neverBuiltDependencies": [] }, diff --git a/patches/electron-is.patch b/patches/electron-is.patch new file mode 100644 index 00000000..2b6e456f --- /dev/null +++ b/patches/electron-is.patch @@ -0,0 +1,27 @@ +diff --git a/is.d.ts b/is.d.ts +index fb861f7b401914f0f89cb4edf25c51df5cb05812..82144733cd34d88e2deb2e4713b104418e673f2e 100644 +--- a/is.d.ts ++++ b/is.d.ts +@@ -5,6 +5,7 @@ declare namespace is { + export function macOS(): boolean; + export function windows(): boolean; + export function linux(): boolean; ++ export function freebsd(): boolean; + export function x86(): boolean; + export function x64(): boolean; + export function production(): boolean; +diff --git a/is.js b/is.js +index a76bb1755a2728bde185b35d847031d3b8ea4ab0..f6b03406c17342f5af078de069e5bbbd2246e152 100644 +--- a/is.js ++++ b/is.js +@@ -39,6 +39,10 @@ module.exports = { + linux: function () { + return process.platform === 'linux' + }, ++ // Checks if we are under FreeBSD OS ++ freebsd: function () { ++ return process.platform === "freebsd" ++ }, + // Checks if we are the processor's arch is x86 + x86: function () { + return process.arch === 'ia32' diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c59d881..80ac08aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,9 @@ patchedDependencies: '@malept/flatpak-bundler@0.4.0': hash: c787371eeb2af011ea934e8818a0dad6d7dcb2df31bbb1686babc7231af0183c path: patches/@malept__flatpak-bundler@0.4.0.patch + electron-is: + hash: 08664a5453f092fe45542f9005ffc07527b1f207447318374cba0f4d30f466a0 + path: patches/electron-is.patch file-type@16.5.4: hash: fa6e3546c096bc3579fd83808c2dfac1a64546eb2ce096bf838076bda630baf5 path: patches/file-type@16.5.4.patch @@ -113,7 +116,7 @@ importers: version: 4.1.0 electron-is: specifier: 3.0.0 - version: 3.0.0 + version: 3.0.0(patch_hash=08664a5453f092fe45542f9005ffc07527b1f207447318374cba0f4d30f466a0) electron-localshortcut: specifier: 3.2.1 version: 3.2.1 @@ -6850,7 +6853,7 @@ snapshots: electron-is-dev@3.0.1: {} - electron-is@3.0.0: + electron-is@3.0.0(patch_hash=08664a5453f092fe45542f9005ffc07527b1f207447318374cba0f4d30f466a0): dependencies: electron-is-dev: 0.3.0 semver: 5.7.2 diff --git a/src/index.ts b/src/index.ts index 62512c02..a42bb6b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,14 +59,7 @@ import ErrorHtmlAsset from '@assets/error.html?asset'; import { defaultAuthProxyConfig } from '@/plugins/auth-proxy-adapter/config'; -import type { PluginConfig } from '@/types/plugins'; - -if (!is.macOS()) { - delete (await allPlugins())['touchbar']; -} -if (!is.windows()) { - delete (await allPlugins())['taskbar-mediacontrol']; -} +import { type PluginConfig } from '@/types/plugins'; // Catch errors and log them unhandled({ diff --git a/src/plugins/taskbar-mediacontrol/index.ts b/src/plugins/taskbar-mediacontrol/index.ts index 1ee1882f..da33c1b4 100644 --- a/src/plugins/taskbar-mediacontrol/index.ts +++ b/src/plugins/taskbar-mediacontrol/index.ts @@ -14,11 +14,13 @@ import registerCallback, { } from '@/providers/song-info'; import { mediaIcons } from '@/types/media-icons'; import { t } from '@/i18n'; +import { Platform } from '@/types/plugins'; export default createPlugin({ name: () => t('plugins.taskbar-mediacontrol.name'), description: () => t('plugins.taskbar-mediacontrol.description'), restartNeeded: true, + platform: Platform.Windows, config: { enabled: false, }, diff --git a/src/plugins/touchbar/index.ts b/src/plugins/touchbar/index.ts index 501452fb..587ea066 100644 --- a/src/plugins/touchbar/index.ts +++ b/src/plugins/touchbar/index.ts @@ -6,11 +6,13 @@ import registerCallback, { SongInfoEvent } from '@/providers/song-info'; import { t } from '@/i18n'; import youtubeMusicIcon from '@assets/youtube-music.png?asset&asarUnpack'; +import { Platform } from '@/types/plugins'; export default createPlugin({ name: () => t('plugins.touchbar.name'), description: () => t('plugins.touchbar.description'), restartNeeded: true, + platform: Platform.macOS, config: { enabled: false, }, diff --git a/src/reset.d.ts b/src/reset.d.ts index bb204966..e2818940 100644 --- a/src/reset.d.ts +++ b/src/reset.d.ts @@ -19,6 +19,8 @@ declare global { 'videodatachange': CustomEvent; } + declare var electronIs: typeof import('electron-is') + interface Window { trustedTypes?: typeof trustedTypes; ipcRenderer: typeof electronIpcRenderer; diff --git a/src/types/plugins.ts b/src/types/plugins.ts index f05df709..d8d07dfe 100644 --- a/src/types/plugins.ts +++ b/src/types/plugins.ts @@ -38,6 +38,13 @@ export type RendererPluginLifecycle = | PluginLifecycleSimple | RendererPluginLifecycleExtra; +export enum Platform { + Windows = 1 << 0, + macOS = 1 << 1, + Linux = 1 << 2, + Freebsd = 1 << 3 +} + export interface PluginDef< BackendProperties, PreloadProperties, @@ -49,6 +56,7 @@ export interface PluginDef< description?: () => string; addedVersion?: string; config?: Config; + platform?: Platform; menu?: ( ctx: MenuContext, diff --git a/vite-plugins/plugin-importer.mts b/vite-plugins/plugin-importer.mts index 44bd3e98..533b8139 100644 --- a/vite-plugins/plugin-importer.mts +++ b/vite-plugins/plugin-importer.mts @@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'; import { globSync } from 'glob'; import { Project } from 'ts-morph'; +import { Platform } from '../src/types/plugins' const kebabToCamel = (text: string) => text.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase()); @@ -73,6 +74,12 @@ export const pluginVirtualModuleGenerator = ( } } + writer.blankLine(); + if (mode === "main" || mode === "preload") { + writer.writeLine("import * as is from 'electron-is';"); + writer.writeLine('globalThis.electronIs = is;'); + } + writer.write(supportsPlatform.toString()); writer.blankLine(); // Context-specific exports @@ -95,7 +102,7 @@ export const pluginVirtualModuleGenerator = ( } writer.writeLine(' ]);'); writer.writeLine( - ' resolve(pluginEntries.filter((entry) => entry).reduce((acc, [name, plg]) => { acc[name] = plg; return acc; }, {}));', + ' resolve(pluginEntries.filter((entry) => entry && supportsPlatform(entry[1])).reduce((acc, [name, plg]) => { acc[name] = plg; return acc; }, {}));', ); writer.writeLine(` return await ${mode}PluginsCache;`); writer.writeLine('};'); @@ -117,7 +124,7 @@ export const pluginVirtualModuleGenerator = ( } writer.writeLine(' ]);'); writer.writeLine( - ' resolve(stubEntries.reduce((acc, [name, plg]) => { acc[name] = plg; return acc; }, {}));', + ' resolve(stubEntries.filter(entry => entry && supportsPlatform(entry[1])).reduce((acc, [name, plg]) => { acc[name] = plg; return acc; }, {}));', ); writer.writeLine(' return await promise;'); writer.writeLine('};'); @@ -128,3 +135,17 @@ export const pluginVirtualModuleGenerator = ( return src.getText(); }; + +function supportsPlatform({ platform }: { platform: string }) { + if (typeof platform !== "number") return true; + + const is = globalThis.electronIs; + + if (is.windows()) return (platform & Platform.Windows) !== 0; + if (is.macOS()) return (platform & Platform.macOS) !== 0; + if (is.linux()) return (platform & Platform.Linux) !== 0; + if (is.freebsd()) return (platform & Platform.Freebsd) !== 0; + + // unknown platform + return false; +}