支持系统插件

This commit is contained in:
muwoo 2021-12-08 20:01:25 +08:00
parent 8a35e60e48
commit 64f2eba2fa
3 changed files with 65 additions and 1 deletions

View File

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

View File

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

View File

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