fix: plugin with locked files couldn't update

Massimo Melina committed May 28, 2023 at 16:41 UTC 37a7fc63ae0e031fb5cc6bf285e45da4d8a151d7
2 files changed +8 -6
src/api.plugins.ts
+2 -4
@@ -127,15 +127,13 @@ const apis: ApiHandlers = {
127 },
128
129 async update_plugin(pl) {
130 + await enablePlugin(pl.id, false, true)
131 await downloadPlugin(pl.id, pl.branch, true)
132 return {}
133 },
134
135 async uninstall_plugin({ id }) {
135 - while (isPluginRunning(id)) {
136 - enablePlugin(id, false)
137 - await wait(500)
138 - }
136 + await enablePlugin(id, false, true)
137 await rm(PLUGINS_PATH + '/' + id, { recursive: true, force: true })
138 return {}
139 }
src/plugins.ts
+6 -2
@@ -15,6 +15,7 @@ import {
15 onProcessExit,
16 same,
17 tryJson,
18 + wait,
19 wantArray,
20 watchDir
21 } from './misc'
@@ -36,15 +37,18 @@ export const STORAGE_FOLDER = 'storage'
37 const plugins: Record<string, Plugin> = {}
38
39 export function isPluginRunning(id: string) {
39 - return plugins[id]?.started
40 + return Boolean(plugins[id]?.started)
41 }
42
42 -export function enablePlugin(id: string, state=true) {
43 +export async function enablePlugin(id: string, state=true, waitForIt=false) {
44 enablePlugins.set( arr =>
45 arr.includes(id) === state ? arr
46 : state ? [...arr, id]
47 : arr.filter((x: string) => x !== id)
48 )
49 + if (!waitForIt) return
50 + while (isPluginRunning(id) !== state)
51 + await wait(500)
52 }
53
54 // nullish values are equivalent to defaultValues