mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-25 20:09:27 +08:00
:spark: 支持插件自动更新
This commit is contained in:
@@ -9,6 +9,7 @@ import fixPath from 'fix-path';
|
||||
|
||||
import spawn from 'cross-spawn';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import axios from 'axios';
|
||||
|
||||
fixPath();
|
||||
|
||||
@@ -22,6 +23,8 @@ class AdapterHandler {
|
||||
// 插件源地址
|
||||
readonly registry: string;
|
||||
|
||||
pluginCaches = {};
|
||||
|
||||
/**
|
||||
* Creates an instance of AdapterHandler.
|
||||
* @param {AdapterHandlerOptions} options
|
||||
@@ -52,6 +55,29 @@ class AdapterHandler {
|
||||
this.registry = register || 'https://registry.npm.taobao.org';
|
||||
}
|
||||
|
||||
async upgrade(name: string): Promise<void> {
|
||||
// 创建一个npm-registry-client实例
|
||||
const packageJSON = JSON.parse(
|
||||
fs.readFileSync(`${this.baseDir}/package.json`, 'utf-8')
|
||||
);
|
||||
const registryUrl = `https://registry.npm.taobao.org/${name}`;
|
||||
|
||||
// 从npm源中获取依赖包的最新版本
|
||||
try {
|
||||
const installedVersion = packageJSON.dependencies[name].replace('^', '');
|
||||
let latestVersion = this.pluginCaches[name];
|
||||
if (!latestVersion) {
|
||||
const { data } = await axios.get(registryUrl, { timeout: 2000 });
|
||||
latestVersion = data['dist-tags'].latest;
|
||||
this.pluginCaches[name] = latestVersion;
|
||||
}
|
||||
if (latestVersion > installedVersion) {
|
||||
await this.install([name], { isDev: false });
|
||||
}
|
||||
} catch (e) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取插件信息
|
||||
* @param {string} adapter 插件名称
|
||||
|
||||
@@ -29,11 +29,12 @@
|
||||
>
|
||||
<template #suffix>
|
||||
<div class="suffix-tool">
|
||||
<MoreOutlined @click="showSeparate()" class="icon-more" />
|
||||
<MoreOutlined v-show="!pluginLoading" @click="showSeparate()" class="icon-more" />
|
||||
<div
|
||||
v-if="currentPlugin && currentPlugin.logo"
|
||||
style="position: relative"
|
||||
>
|
||||
<div v-show="pluginLoading" class="update-tips">检测更新中...</div>
|
||||
<a-spin v-show="pluginLoading" class="loading">
|
||||
<template #indicator>
|
||||
<LoadingOutlined style="font-size: 42px" />
|
||||
@@ -309,6 +310,14 @@ window.rubick.hooks.onHide = () => {
|
||||
top: 0;
|
||||
left: 0;
|
||||
}
|
||||
.update-tips {
|
||||
position: absolute;
|
||||
right: 46px;
|
||||
top: 50%;
|
||||
font-size: 14px;
|
||||
transform: translateY(-50%);
|
||||
color: #aaa;
|
||||
}
|
||||
}
|
||||
.clipboard-tag {
|
||||
white-space: pre;
|
||||
|
||||
@@ -28,17 +28,25 @@ const createPluginManager = (): any => {
|
||||
appList.value = await appSearch(nativeImage);
|
||||
};
|
||||
|
||||
const loadPlugin = (plugin) => {
|
||||
const loadPlugin = async (plugin) => {
|
||||
setSearchValue('');
|
||||
ipcRenderer.send('msg-trigger', {
|
||||
type: 'setExpendHeight',
|
||||
data: 60,
|
||||
});
|
||||
state.pluginLoading = true;
|
||||
state.currentPlugin = plugin;
|
||||
// 自带的插件不需要检测更新
|
||||
if (plugin.name === 'rubick-system-feature') return;
|
||||
await pluginInstance.upgrade(plugin.name);
|
||||
};
|
||||
|
||||
const openPlugin = (plugin) => {
|
||||
const openPlugin = async (plugin) => {
|
||||
if (plugin.pluginType === 'ui' || plugin.pluginType === 'system') {
|
||||
if (state.currentPlugin && state.currentPlugin.name === plugin.name) {
|
||||
return;
|
||||
}
|
||||
loadPlugin(plugin);
|
||||
await loadPlugin(plugin);
|
||||
ipcRenderer.sendSync('msg-trigger', {
|
||||
type: 'openPlugin',
|
||||
data: JSON.parse(
|
||||
@@ -52,7 +60,6 @@ const createPluginManager = (): any => {
|
||||
})
|
||||
),
|
||||
});
|
||||
setSearchValue('');
|
||||
}
|
||||
if (plugin.pluginType === 'app') {
|
||||
execSync(plugin.action);
|
||||
|
||||
Reference in New Issue
Block a user