feat(refactor): PluginDefinition::platform (#3665)

This commit is contained in:
Angelos Bouklis
2025-09-05 09:28:17 +03:00
committed by GitHub
parent d329076b52
commit dcc611c7d0
9 changed files with 72 additions and 13 deletions

View File

@ -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": []
},

27
patches/electron-is.patch Normal file
View File

@ -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'

7
pnpm-lock.yaml generated
View File

@ -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

View File

@ -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({

View File

@ -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,
},

View File

@ -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,
},

2
src/reset.d.ts vendored
View File

@ -19,6 +19,8 @@ declare global {
'videodatachange': CustomEvent<VideoDataChanged>;
}
declare var electronIs: typeof import('electron-is')
interface Window {
trustedTypes?: typeof trustedTypes;
ipcRenderer: typeof electronIpcRenderer;

View File

@ -38,6 +38,13 @@ export type RendererPluginLifecycle<Config, Context, This> =
| PluginLifecycleSimple<Context, This>
| RendererPluginLifecycleExtra<Config, Context, This>;
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<Config>,

View File

@ -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;
}