refactor(app): 插件加载/市场/备份抽成 src 模块(app.js 8880→8764 行);备份包含插件
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m31s
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m31s
This commit is contained in:
1 parent
0ec11bfca8
commit
ce575d2640
11 files changed
+297
-149
No files matched your search
@@ -0,0 +1,52 @@
|
||||
/* runtime-plugin-backup.js —— 插件进出备份
|
||||
*
|
||||
* 备份里带上插件清单与源码(体积很小),换机器恢复后不必重装。
|
||||
* collect() → [{ id, version, file, integrity, code }]
|
||||
* restore(v) → 写回 <资料目录>/plugins/ 并合并清单(备份里没有的插件保留)
|
||||
*/
|
||||
;(function (global) {
|
||||
'use strict'
|
||||
|
||||
function store () {
|
||||
const F = global.IWBStorageFile || null
|
||||
return (F && F.plugins) ? F.plugins : null
|
||||
}
|
||||
|
||||
async function collect () {
|
||||
const s = store()
|
||||
if (!s) return []
|
||||
let list = []
|
||||
try { list = await s.manifest() } catch (e) { return [] }
|
||||
const out = []
|
||||
for (const e of (Array.isArray(list) ? list : [])) {
|
||||
if (!e || !e.id || !e.file) continue
|
||||
try {
|
||||
const text = await s.read(e.file)
|
||||
out.push({ id: e.id, version: e.version || '', file: e.file, integrity: e.integrity || '', code: String(text || '') })
|
||||
} catch (err) { /* 单个插件读失败不影响其它 */ }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
async function restore (list) {
|
||||
const s = store()
|
||||
if (!s || !Array.isArray(list) || !list.length) return 0
|
||||
let cur = []
|
||||
try { cur = await s.manifest() } catch (e) { cur = [] }
|
||||
const byId = new Map((Array.isArray(cur) ? cur : []).map(function (e) { return [String(e && e.id), e] }))
|
||||
let n = 0
|
||||
for (const p of list) {
|
||||
if (!p || !p.id || !p.file) continue
|
||||
if (!/^[A-Za-z0-9._-]+$/.test(String(p.file))) continue
|
||||
try {
|
||||
await s.write(String(p.file), String(p.code || ''))
|
||||
byId.set(String(p.id), { id: String(p.id), file: String(p.file), version: String(p.version || ''), integrity: String(p.integrity || '') })
|
||||
n++
|
||||
} catch (err) { /* 单个失败忽略 */ }
|
||||
}
|
||||
try { await s.write('index.json', JSON.stringify(Array.from(byId.values()))) } catch (e) { /* ignore */ }
|
||||
return n
|
||||
}
|
||||
|
||||
global.IWBRuntimePluginBackup = { collect: collect, restore: restore }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user