MCPcopy Create free account
hub / github.com/ZToolsCenter/ZTools / installPluginFromSyncZip

Method installPluginFromSyncZip

src/main/core/sync/syncEngine.ts:988–1045  ·  view source on GitHub ↗

* 从同步下载的 zip 安装插件

(pluginName: string, zipBuffer: Buffer)

Source from the content-addressed store, hash-verified

986 * 从同步下载的 zip 安装插件
987 */
988 private async installPluginFromSyncZip(pluginName: string, zipBuffer: Buffer): Promise<void> {
989 const targetDir = path.join(PLUGIN_DIR, pluginName)
990
991 // 确保插件目录存在
992 if (!fs.existsSync(PLUGIN_DIR)) {
993 fs.mkdirSync(PLUGIN_DIR, { recursive: true })
994 }
995
996 // 如果已存在则先删除
997 if (fs.existsSync(targetDir)) {
998 fs.rmSync(targetDir, { recursive: true, force: true })
999 }
1000
1001 // 解压 zip
1002 const zip = new AdmZip(zipBuffer)
1003 zip.extractAllTo(targetDir, true)
1004
1005 // 读取 plugin.json
1006 const pluginJsonPath = path.join(targetDir, 'plugin.json')
1007 if (!fs.existsSync(pluginJsonPath)) {
1008 console.warn(`[Sync] 安装的插件缺少 plugin.json: ${pluginName}`)
1009 return
1010 }
1011
1012 const pluginJson = JSON.parse(fs.readFileSync(pluginJsonPath, 'utf-8'))
1013
1014 // 更新 LMDB 插件列表
1015 const pluginsDoc = await this.db.promises.get('ZTOOLS/plugins')
1016 const plugins: any[] = pluginsDoc?.data ? JSON.parse(JSON.stringify(pluginsDoc.data)) : []
1017
1018 // 检查是否已存在
1019 const existingIndex = plugins.findIndex((p: any) => p.name === pluginName)
1020 const pluginEntry = {
1021 name: pluginJson.name || pluginName,
1022 title: pluginJson.title || pluginJson.name || pluginName,
1023 path: targetDir,
1024 version: pluginJson.version || '0.0.0',
1025 description: pluginJson.description || '',
1026 logo: pluginJson.logo || '',
1027 features: pluginJson.features || [],
1028 isDevelopment: false
1029 }
1030
1031 if (existingIndex >= 0) {
1032 plugins[existingIndex] = pluginEntry
1033 } else {
1034 plugins.push(pluginEntry)
1035 }
1036
1037 await this.db.promises.put({
1038 _id: 'ZTOOLS/plugins',
1039 _rev: pluginsDoc?._rev,
1040 data: plugins
1041 })
1042
1043 // 通知渲染进程
1044 this.notifyPluginsChanged()
1045 }

Callers 2

syncPluginsMethod · 0.95

Calls 4

notifyPluginsChangedMethod · 0.95
stringifyMethod · 0.80
getMethod · 0.65
putMethod · 0.45

Tested by

no test coverage detected