fix: plugins folder is not monitored if not present from the beginning

Massimo Melina committed Jan 17, 2022 at 23:33 UTC 413297acb9e232b79815b41c667335ee6730ade7
8 files changed +23 -18
frontend/src/api.ts
+1 -1
@@ -58,7 +58,7 @@ export function apiEvents(cmd: string, params: Dict, cb:EventHandler) {
58 return source.close()
59 }
60 try { data = JSON.parse(data) }
61 - catch(e) {
61 + catch {
62 return cb('string', data)
63 }
64 cb('msg', data)
frontend/src/state.ts
+1 -1
@@ -42,7 +42,7 @@ function loadSettings() {
42 if (!json) return
43 let read
44 try { read = JSON.parse(json) }
45 - catch(e) {
45 + catch {
46 console.error('invalid settings stored', json)
47 return
48 }
src/misc.ts
+15 -2
@@ -1,5 +1,7 @@
1 import { EventEmitter } from 'events'
2 import fs from 'fs/promises'
3 +import { basename, dirname } from 'path'
4 +import { watch } from 'fs'
5
6 export function enforceFinal(sub:string, s:string) {
7 return s.endsWith(sub) ? s : s+sub
@@ -7,12 +9,12 @@ export function enforceFinal(sub:string, s:string) {
9
10 export async function isDirectory(path: string) {
11 try { return (await fs.stat(path)).isDirectory() }
10 - catch(e) { return false }
12 + catch { return false }
13 }
14
15 export async function isFile(path: string) {
16 try { return (await fs.stat(path)).isFile() }
15 - catch(e) { return false }
17 + catch { return false }
18 }
19
20 export function complySlashes(path: string) {
@@ -84,3 +86,14 @@ export function onFirstEvent(emitter:EventEmitter, events: string[], cb: (...arg
86 cb(...args)
87 })
88 }
89 +
90 +export function watchDir(dir: string, cb: ()=>void) {
91 + const base = basename(dir)
92 + watch(dirname(dir), (event,name) => {
93 + if (name === base)
94 + cb()
95 + })
96 + cb()
97 + try { watch(dir, cb) }
98 + catch {}
99 +}
src/plugins.ts
+3 -8
@@ -1,11 +1,10 @@
1 -import { watch } from 'fs'
1 import glob from 'fast-glob'
2 import { watchLoad } from './watchLoad'
3 import _ from 'lodash'
4 import { resolve } from 'path'
5 import { PLUGINS_PUB_URI } from './const'
6 import Koa from 'koa'
8 -import { getOrSet, onProcessExit, wantArray } from './misc'
7 +import { getOrSet, onProcessExit, wantArray, watchDir } from './misc'
8 import { getConfig, subscribeConfig } from './config'
9 import { DirEntry } from './api.file_list'
10 import { VfsNode } from './vfs'
@@ -59,12 +58,8 @@ export function pluginsMiddleware(): Koa.Middleware {
58 }
59
60 subscribeConfig({ k:'disable_plugins', defaultValue:[] }, () => {
62 - try {
63 - const debounced = _.debounce(rescan, 1000)
64 - watch(PATH, debounced)
65 - debounced()
66 - }
67 - catch(e){
61 + try { watchDir(PATH, _.debounce(rescan, 1000)) }
62 + catch {
63 console.debug('plugins not found')
64 }
65 })
src/vfs.ts
+1 -1
@@ -61,7 +61,7 @@ export class Vfs {
61 if (isMatch(source, wantArray(run.remove).map(x => baseSource + x)))
62 return
63 try { await fs.stat(source) } // check existence
64 - catch(e){ return }
64 + catch { return }
65 return {
66 source,
67 name: basename(source),
src/watchLoad.ts
+1 -2
@@ -18,8 +18,7 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
18 watcher = watch(path, debounced)
19 debounced()
20 }
21 - catch(e){
22 - }
21 + catch {}
22 }, 1000)
23 let running = true
24 return () => {
src/zip.ts
+1 -2
@@ -20,8 +20,7 @@ export async function zipStreamFromFolder(node: VfsNode, ctx: Koa.Context) {
20 return
21 return { path:el.name, size:st.size, ts:st.mtime||st.ctime, data: createReadStream(el.source) }
22 }
23 - catch(e) {}
24 -
23 + catch {}
24 })
25 const zip = new QuickZipStream(walker)
26 const time = 1000 * (getConfig('zip-calculate-size-for-seconds') ?? 1)
todo.md
-1
@@ -1,5 +1,4 @@
1 # To do
2 -- fix: if plugins folder is not found at the start, it won't try anymore
2 - log filter option
3 - log filter plugin
4 - frontend search supporting masks