@samitouri / QOSami-HFS / commits / 7957f859

better code: serverCode closer to a plugin

Massimo Melina committed Sep 24, 2023 at 14:01 UTC 7957f85995254d9472891a7e37d704bfa7fcf11f
2 files changed +28 -30
src/customHtml.ts
+1 -1
@@ -35,7 +35,7 @@ export function watchLoadCustomHtml(folder='') {
35
36 export function getSection(name: string) {
37 return (customHtmlState.sections.get(name) || '')
38 - + mapPlugins(pl => pl.getData().customHtml.get(name)).join('\n')
38 + + mapPlugins(pl => pl.getData().customHtml?.get(name)).join('\n')
39 }
40
41 export async function saveCustomHtml(sections: Dict<string>) {
src/plugins.ts
+27 -29
@@ -79,8 +79,6 @@ export function setPluginConfig(id: string, changes: Dict) {
79 }
80
81 export function getPluginInfo(id: string) {
82 - if (id === SERVER_CODE_ID)
83 - return serverCodeReturned
82 const running = plugins[id]?.getData()
83 return running && Object.assign(running, {id}) || availablePlugins[id]
84 }
@@ -98,32 +96,6 @@ export function getPluginConfigFields(id: string) {
96 return plugins[id]?.getData().config
97 }
98
101 -const serverCode = defineConfig('server_code', '', async (script, { k }) => {
102 - const res: any = {}
103 - try {
104 - new Function('exports', script)(res) // parse
105 - return await initPlugin(res)
106 - }
107 - catch (e: any) {
108 - return console.error(k + ':', e.message || String(e))
109 - }
110 -})
111 -
112 -const SERVER_CODE_ID = '.'
113 -let serverCodeReturned: any
114 -serverCode.sub(() => serverCode.compiled().then(x => serverCodeReturned = x))
115 -export function mapPlugins<T>(cb:(plugin:Readonly<Plugin>, pluginName:string)=> T, includeServerCode=true) {
116 - const entries = Object.entries(plugins)
117 - if (includeServerCode && serverCodeReturned)
118 - entries.push([SERVER_CODE_ID, serverCodeReturned])
119 - return entries.map(([plName,pl]) => {
120 - try { return cb(pl,plName) }
121 - catch(e) {
122 - console.log('plugin error', plName, String(e))
123 - }
124 - }).filter(x => x !== undefined) as Exclude<T,undefined>[]
125 -}
126 -
99 async function initPlugin<T>(pl: any, more?: T) {
100 return Object.assign(pl, await pl.init?.({
101 const: Const, // legacy, deprecated in 0.48
@@ -196,6 +168,7 @@ export class Plugin {
168 console.warn('invalid', k)
169 }
170 }
171 + plugins[id] = this
172 }
173 get version(): undefined | number {
174 return this.data?.version
@@ -235,6 +208,31 @@ export class Plugin {
208 }
209 }
210
211 +const serverCode = defineConfig('server_code', '', async (script, { k }) => {
212 + const res: any = {}
213 + try {
214 + new Function('exports', script)(res) // parse
215 + return new Plugin('.', '', await initPlugin(res), _.noop) // '.' is a name that will surely be not found among plugin folders
216 + }
217 + catch (e: any) {
218 + return console.error(k + ':', e.message || String(e))
219 + }
220 +})
221 +
222 +let serverCodePlugin: void | Plugin
223 +serverCode.sub(() => serverCode.compiled().then(x => serverCodePlugin = x))
224 +export function mapPlugins<T>(cb:(plugin:Readonly<Plugin>, pluginName:string)=> T, includeServerCode=true) {
225 + const entries = Object.entries(plugins)
226 + if (includeServerCode && serverCodePlugin)
227 + entries.push([serverCodePlugin.id, serverCodePlugin])
228 + return entries.map(([plName,pl]) => {
229 + try { return cb(pl,plName) }
230 + catch(e) {
231 + console.log('plugin error', plName, String(e))
232 + }
233 + }).filter(x => x !== undefined) as Exclude<T,undefined>[]
234 +}
235 +
236 type PluginMiddleware = (ctx:Koa.Context) => void | Stop | CallMeAfter
237 type Stop = true
238 type CallMeAfter = ()=>any
@@ -383,7 +381,7 @@ function watchPlugin(id: string, path: string) {
381 const folder = dirname(module)
382 const { state, unwatch } = watchLoadCustomHtml(folder)
383 pluginData.customHtml = state
386 - const plugin = plugins[id] = new Plugin(id, folder, pluginData, unwatch)
384 + const plugin = new Plugin(id, folder, pluginData, unwatch)
385 if (alreadyRunning)
386 events.emit('pluginUpdated', Object.assign(_.pick(plugin, 'started'), getPluginInfo(id)))
387 else {