diff --git a/src/main/common/registerySystemPlugin.ts b/src/main/common/registerySystemPlugin.ts new file mode 100644 index 0000000..75b47f1 --- /dev/null +++ b/src/main/common/registerySystemPlugin.ts @@ -0,0 +1,50 @@ +/* eslint-disable */ +import path from "path"; +import {app} from "electron"; +import fs from "fs"; + +const appPath = app.getPath("cache"); + +export default () => { + // 读取所有插件 + const totalPlugins = global.LOCAL_PLUGINS.getLocalPlugins(); + let systemPlugins = totalPlugins.filter((plugin) => plugin.pluginType === "system"); + const baseDir = path.join(appPath, "./rubick-plugins"); + + systemPlugins = systemPlugins.map((plugin) => { + const pluginPath = path.resolve( + baseDir, + "node_modules", + plugin.name + ); + return { + ...plugin, + indexPath: path.join( + pluginPath, + "./", + plugin.entry + ), + } + }); + + const hooks = { + onReady: [], + }; + + systemPlugins.forEach((plugin) => { + if (fs.existsSync(plugin.indexPath)) { + const pluginModule = __non_webpack_require__(plugin.indexPath)(); + // @ts-ignore + hooks.onReady.push(pluginModule.onReady); + } + }); + + const triggerReadyHooks = (ctx) => { + // @ts-ignore + hooks.onReady.forEach(hook => hook(ctx)); + } + + return { + triggerReadyHooks + }; +} diff --git a/src/main/index.ts b/src/main/index.ts index 4d4fa7c..5db382c 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,10 @@ "use strict"; -import { app, globalShortcut, protocol, BrowserWindow } from "electron"; +import electron, { + app, + globalShortcut, + protocol, + BrowserWindow, +} from "electron"; import { main } from "./browsers"; import commonConst from "../common/utils/commonConst"; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -11,8 +16,11 @@ import registerHotKey from "./common/registerHotKey"; import "../common/utils/localPlugin"; import "../common/utils/localConfig"; +import registerySystemPlugin from "./common/registerySystemPlugin"; + class App { private windowCreator: { init: () => void; getWindow: () => BrowserWindow }; + private systemPlugins: any; constructor() { protocol.registerSchemesAsPrivileged([ @@ -23,6 +31,7 @@ class App { if (!gotTheLock) { app.quit(); } else { + this.systemPlugins = registerySystemPlugin(); this.beforeReady(); this.onReady(); this.onRunning(); @@ -54,6 +63,9 @@ class App { // this.init() createTray(this.windowCreator.getWindow()); registerHotKey(this.windowCreator.getWindow()); + this.systemPlugins.triggerReadyHooks( + Object.assign(electron, { mainWindow: this.windowCreator.getWindow() }) + ); }; if (!app.isReady()) { app.on("ready", readyFunction); diff --git a/src/renderer/plugins-manager/options.ts b/src/renderer/plugins-manager/options.ts index 5adadb3..d20448b 100644 --- a/src/renderer/plugins-manager/options.ts +++ b/src/renderer/plugins-manager/options.ts @@ -25,6 +25,8 @@ const optionsManager = ({ searchValue, baseDir, appList, openPlugin, currentPlug // todo 先搜索 plugin localPlugins.forEach((plugin) => { const feature = plugin.features; + // 系统插件无 features 的情况,不需要再搜索 + if (!feature) return; feature.forEach((fe) => { const cmds = searchKeyValues(fe.cmds, value); options = [