mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-26 12:42:34 +08:00
✨ 支持ui插件下载&运行
This commit is contained in:
7
src/common/utils/getLocalDataFile.ts
Normal file
7
src/common/utils/getLocalDataFile.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export default (): string => {
|
||||
let localDataFile: any = process.env.HOME;
|
||||
if (!localDataFile) {
|
||||
localDataFile = process.env.LOCALAPPDATA;
|
||||
}
|
||||
return localDataFile;
|
||||
};
|
||||
46
src/common/utils/localPlugin.ts
Normal file
46
src/common/utils/localPlugin.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import getLocalDataFile from "./getLocalDataFile";
|
||||
import { app } from "electron";
|
||||
import { PluginHandler } from "@/core";
|
||||
|
||||
const configPath = path.join(getLocalDataFile(), "./rubick-local-plugin.json");
|
||||
const appPath = app.getPath("cache");
|
||||
|
||||
const baseDir = path.join(appPath, "./rubick-plugins");
|
||||
const pluginInstance = new PluginHandler({
|
||||
baseDir: baseDir,
|
||||
});
|
||||
|
||||
global.LOCAL_PLUGINS = {
|
||||
PLUGINS: [],
|
||||
async downloadPlugin(plugin) {
|
||||
await pluginInstance.install([plugin.name]);
|
||||
global.LOCAL_PLUGINS.addPlugin(plugin);
|
||||
return global.LOCAL_PLUGINS.PLUGINS;
|
||||
},
|
||||
getLocalPlugins() {
|
||||
try {
|
||||
if (!global.LOCAL_PLUGINS.PLUGINS.length) {
|
||||
global.LOCAL_PLUGINS.PLUGINS = JSON.parse(
|
||||
fs.readFileSync(configPath, "utf-8")
|
||||
);
|
||||
}
|
||||
return global.LOCAL_PLUGINS.PLUGINS;
|
||||
} catch (e) {
|
||||
global.LOCAL_PLUGINS.PLUGINS = [];
|
||||
return global.LOCAL_PLUGINS.PLUGINS;
|
||||
}
|
||||
},
|
||||
addPlugin(plugin) {
|
||||
let has = false;
|
||||
global.LOCAL_PLUGINS.PLUGINS.some((p) => {
|
||||
has = p.name === plugin.name;
|
||||
return has;
|
||||
});
|
||||
if (!has) {
|
||||
global.LOCAL_PLUGINS.PLUGINS.unshift(plugin);
|
||||
fs.writeFileSync(configPath, JSON.stringify(global.LOCAL_PLUGINS.PLUGINS));
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user