plugins: api.setConfig
Massimo Melina committed
Jun 6, 2022 at 16:44 UTC
1e18a1e14fb89bb0c04b4237102c027ea7241121
4 files changed
+17
-13
dev-plugins.md
+2
@@ -100,6 +100,8 @@ The `api` object you get as parameter of the `init` contains the following:
100
101
- `getConfig(key: string): any` get config's value set up by using `exports.config`.
102
103
+- `setConfig(key: string, value: any)` set config's value set up by using `exports.config`.
104
+
105
- `log(...args)` print log in a standard form for plugins.
106
107
- `const: object` all constants of the `const.ts` file are exposed here. E.g. BUILD_TIMESTAMP, API_VERSION, etc.
server/src/api.plugins.ts
+4
-10
@@ -5,11 +5,11 @@ import {
5
getPluginConfigFields,
6
mapPlugins,
7
Plugin, pluginsConfig,
8
- PATH as PLUGINS_PATH, isPluginRunning, enablePlugin, getPluginInfo
8
+ PATH as PLUGINS_PATH, isPluginRunning, enablePlugin, getPluginInfo, setPluginConfig
9
} from './plugins'
10
import _ from 'lodash'
11
import assert from 'assert'
12
-import { objSameKeys, onOff, same, wait } from './misc'
12
+import { objSameKeys, onOff, wait } from './misc'
13
import { ApiHandlers, sendList } from './apiMiddleware'
14
import events from './events'
15
import { rm } from 'fs/promises'
@@ -58,14 +58,8 @@ const apis: ApiHandlers = {
58
assert(id, 'id')
59
if (enabled !== undefined)
60
enablePlugin(id, enabled)
61
- if (config) {
62
- const fields = getPluginConfigFields(id)
63
- config = _.pickBy(config, (v, k) =>
64
- v !== null && !same(v, fields?.[k]?.defaultValue))
65
- if (_.isEmpty(config))
66
- config = undefined
67
- pluginsConfig.set(v => ({ ...v, [id]: config }))
68
- }
61
+ if (config)
62
+ setPluginConfig(id, config)
63
return {}
64
},
65
server/src/plugins.ts
+11
-2
@@ -7,7 +7,7 @@ import pathLib from 'path'
7
import { API_VERSION, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
8
import * as Const from './const'
9
import Koa from 'koa'
10
-import { debounceAsync, getOrSet, onProcessExit, same, tryJson, wantArray, watchDir } from './misc'
10
+import { debounceAsync, Dict, getOrSet, onProcessExit, same, tryJson, wantArray, watchDir } from './misc'
11
import { defineConfig } from './config'
12
import { DirEntry } from './api.file_list'
13
import { VfsNode } from './vfs'
@@ -34,6 +34,13 @@ export function enablePlugin(id: string, state=true) {
34
)
35
}
36
37
+export function setPluginConfig(id: string, config: Dict) {
38
+ const fields = getPluginConfigFields(id)
39
+ config = _.pickBy(config, (v, k) =>
40
+ v !== null && !same(v, fields?.[k]?.defaultValue))
41
+ pluginsConfig.set(v => ({ ...v, [id]: _.isEmpty(config) ? undefined : config }))
42
+}
43
+
44
export function getPluginInfo(id: string) {
45
return plugins[id]?.getData() ?? availablePlugins[id]
46
}
@@ -222,7 +229,9 @@ export async function rescan() {
229
console.log('plugin', id, ':', ...args)
230
},
231
getConfig: (cfgKey: string) =>
225
- pluginsConfig.get()?.[id]?.[cfgKey] ?? data.config?.[cfgKey]?.defaultValue
232
+ pluginsConfig.get()?.[id]?.[cfgKey] ?? data.config?.[cfgKey]?.defaultValue,
233
+ setConfig: (cfgKey: string, value: any) =>
234
+ setPluginConfig(id, { [cfgKey]: value }),
235
})
236
Object.assign(data, res)
237
new Plugin(id, data, unwatch)
todo.md
-1
@@ -1,5 +1,4 @@
1
# To do
2
-- plugin api to read hfs config
2
- fix: root without can_download breaks
3
- use dialogs instead of side-forms on mobile (admin/fs+accounts)
4
- easier deploy on cloud server: --create-admin <password>