plugin: api.storageDir
Massimo Melina committed
May 28, 2023 at 14:41 UTC
c7f3d8bc07c5a259961660977c38d3b8b912410c
3 files changed
+12
-5
dev-plugins.md
+4
-1
@@ -124,6 +124,8 @@ The `api` object you get as parameter of the `init` contains the following:
124
125
- `getConnections: Connections[]` retrieve current list of active connections.
126
127
+- `storageDir: string` folder where a plugin is supposed to store run-time data.
128
+
129
- `events: EventEmitter` this is the main events emitter used by HFS.
130
131
- `require: function` use this instead of standard `require` function to access modules already loaded by HFS. Example:
@@ -270,10 +272,11 @@ HFS will scan through them in inverted alphabetical order searching for a compat
272
273
## API version history
274
273
-- 8.2 (v0.46.0)
275
+- 8.21 (v0.46.0)
276
- entry.getNext, getPrevious, getNextFiltered, getPreviousFiltered, getDefaultIcon
277
- platform-dependent distribution
278
- HFS.watchState
279
+ - api.storageDir
280
- 8.1 (v0.45.0) should have been 0.44.0 but forgot to update number
281
- full URL support for frontend_js and frontend_css
282
- custom.html
src/const.ts
+1
-1
@@ -16,7 +16,7 @@ const pkg = JSON.parse(fs.readFileSync(PKG_PATH,'utf8'))
16
export const VERSION = pkg.version
17
export const DAY = 86_400_000
18
19
-export const API_VERSION = 8.2
19
+export const API_VERSION = 8.21
20
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
21
22
export const HFS_REPO = 'rejetto/hfs'
src/plugins.ts
+7
-3
@@ -3,7 +3,7 @@
3
import glob from 'fast-glob'
4
import { watchLoad } from './watchLoad'
5
import _ from 'lodash'
6
-import { API_VERSION, APP_PATH, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
6
+import { API_VERSION, APP_PATH, COMPATIBLE_API_VERSION, IS_WINDOWS, PLUGINS_PUB_URI } from './const'
7
import * as Const from './const'
8
import Koa from 'koa'
9
import {
@@ -23,7 +23,7 @@ import { DirEntry } from './api.file_list'
23
import { VfsNode } from './vfs'
24
import { serveFile } from './serveFile'
25
import events from './events'
26
-import { readFile } from 'fs/promises'
26
+import { mkdir, readFile } from 'fs/promises'
27
import { existsSync, mkdirSync } from 'fs'
28
import { getConnections } from './connections'
29
import { dirname, resolve } from 'path'
@@ -31,6 +31,7 @@ import { newCustomHtmlState, watchLoadCustomHtml } from './customHtml'
31
32
export const PATH = 'plugins'
33
export const DISABLING_POSTFIX = '-disabled'
34
+export const STORAGE_FOLDER = 'storage'
35
36
const plugins: Record<string, Plugin> = {}
37
@@ -265,14 +266,17 @@ function loadPlugin(id: string, path: string) {
266
267
await alreadyRunning?.unload(true)
268
console.debug("starting plugin", id)
269
+ const storageDir = resolve(module, '..', STORAGE_FOLDER) + (IS_WINDOWS ? '\\' : '/')
270
+ await mkdir(storageDir, { recursive: true })
271
const res = await init?.call(null, {
272
srcDir: __dirname,
273
+ storageDir,
274
const: Const,
275
require,
276
getConnections,
277
events,
278
log(...args: any[]) {
275
- console.log('plugin', id, ':', ...args)
279
+ console.log('plugin', id+':', ...args)
280
},
281
getConfig: (cfgKey: string) =>
282
pluginsConfig.get()?.[id]?.[cfgKey] ?? data.config?.[cfgKey]?.defaultValue,