:spark: 支持插件自动更新

This commit is contained in:
muwoo
2023-03-28 10:26:33 +08:00
parent bfbbf3463e
commit 57781e7b5a
5 changed files with 68 additions and 6 deletions

View File

@@ -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 插件名称