fix: plugins: update was not respecting a customized folder name

Massimo Melina committed May 14, 2022 at 15:46 UTC 8c9719679cbe1470fcf671e4c4bfecaaf6286c30
2 files changed +20 -19
server/src/api.plugins.ts
+8 -8
@@ -13,7 +13,7 @@ import { objSameKeys, onOff, same, wait } from './misc'
13 import { ApiHandlers, sendList } from './apiMiddleware'
14 import events from './events'
15 import { rm } from 'fs/promises'
16 -import { downloadPlugin, getRepo2folder, getRepoInfo, readOnlinePlugin, searchPlugins } from './github'
16 +import { downloadPlugin, getFolder2repo, getRepoInfo, readOnlinePlugin, searchPlugins } from './github'
17
18 const apis: ApiHandlers = {
19
@@ -37,12 +37,12 @@ const apis: ApiHandlers = {
37 async get_plugin_updates() {
38 const list = sendList()
39 setTimeout(async () => {
40 - const repo2folder = getRepo2folder()
41 - for (const repo in repo2folder)
40 + for (const [folder, repo] of Object.entries(getFolder2repo()))
41 try {
42 + if (!repo) continue
43 const online = await readOnlinePlugin(await getRepoInfo(repo))
44 if (!online.apiRequired || online.badApi) continue
45 - const disk = getPluginInfo(repo2folder[repo])
45 + const disk = getPluginInfo(folder)
46 if (online.version! > disk.version)
47 list.add(online)
48 }
@@ -81,12 +81,12 @@ const apis: ApiHandlers = {
81
82 search_online_plugins({ text }, ctx) {
83 const list = sendList()
84 - const repo2folder = getRepo2folder()
84 setTimeout(async () => {
85 try {
86 + const folder2repo = getFolder2repo()
87 for await (const pl of searchPlugins(text)) {
88 const repo = pl.id
89 - Object.assign(pl, { installed: repo2folder[repo] })
89 + Object.assign(pl, { installed: _.includes(folder2repo, repo) })
90 list.add(pl)
91 // watch for events about this plugin, until this request is closed
92 ctx.req.on('close', onOff(events, {
@@ -94,8 +94,8 @@ const apis: ApiHandlers = {
94 if (p.repo === repo)
95 list.update({ id: repo }, { installed: true })
96 },
97 - pluginUninstalled: id => {
98 - if (repo === _.findKey(repo2folder, x => x === id))
97 + pluginUninstalled: folder => {
98 + if (repo === getFolder2repo()[folder])
99 list.update({ id: repo }, { installed: false })
100 },
101 ['pluginDownload_'+repo](status) {
server/src/github.ts
+12 -11
@@ -4,6 +4,7 @@ import { getAvailablePlugins, mapPlugins, parsePluginSource, PATH as PLUGINS_PAT
4 import unzipper from 'unzipper'
5 import { createWriteStream, mkdirSync } from 'fs'
6 import { ApiError } from './apiMiddleware'
7 +import _ from 'lodash'
8
9 const DIST_ROOT = 'dist/'
10
@@ -27,12 +28,13 @@ export async function downloadPlugin(repo: string, branch='', overwrite?: boolea
28 branch = rec.default_branch
29 const url = `https://github.com/${repo}/archive/refs/heads/${branch}.zip`
30 const res = await httpsStream(url)
30 - const repo2 = repo.split('/')[1] // second part, repo without the owner
31 - const repo2clash = !overwrite
32 - && (getAvailablePlugins().find(x => x.id === repo2) || mapPlugins(x => x.id === repo2).some(Boolean))
33 - const pluginFolder = repo2clash ? repo.replace('/','-') : repo2 // longer form only if necessary
34 - const installFolder = PLUGINS_PATH + '/' + pluginFolder
35 - const GITHUB_ZIP_ROOT = repo2 + '-' + (branch) // github puts everything within this folder
31 + const short = repo.split('/')[1] // second part, repo without the owner
32 + const folder2repo = getFolder2repo()
33 + const folder = overwrite ? _.findKey(folder2repo, x => x===repo) // use existing folder
34 + : short in folder2repo ? repo.replace('/','-') // longer form only if another plugin is using short form
35 + : short
36 + const installPath = PLUGINS_PATH + '/' + folder
37 + const GITHUB_ZIP_ROOT = short + '-' + branch // GitHub puts everything within this folder
38 const rootWithinZip = GITHUB_ZIP_ROOT + '/' + DIST_ROOT
39 return new Promise(resolve =>
40 res.pipe(unzipper.Parse())
@@ -40,7 +42,7 @@ export async function downloadPlugin(repo: string, branch='', overwrite?: boolea
42 const { path, type } = entry
43 if (!path.startsWith(rootWithinZip))
44 return entry.autodrain()
43 - const dest = installFolder + '/' + path.slice(rootWithinZip.length)
45 + const dest = installPath + '/' + path.slice(rootWithinZip.length)
46 if (type === 'File')
47 return entry.pipe(createWriteStream(dest))
48 mkdirSync(dest, { recursive: true }) // easy way be sure to have the folder ready before proceeding
@@ -66,10 +68,9 @@ export async function readOnlinePlugin(repoInfo: { full_name: string, default_br
68 return pl
69 }
70
69 -export function getRepo2folder() {
70 - const ret = Object.fromEntries(getAvailablePlugins().map(x => [x.repo, x.id]))
71 - Object.assign(ret, Object.fromEntries(mapPlugins(x => [x.getData().repo, x.id]))) // started ones
72 - delete ret.undefined
71 +export function getFolder2repo() {
72 + const ret = Object.fromEntries(getAvailablePlugins().map(x => [x.id, x.repo]))
73 + Object.assign(ret, Object.fromEntries(mapPlugins(x => [x.id, x.getData().repo])))
74 return ret
75 }
76