53 lines
2.0 KiB
JavaScript
53 lines
2.0 KiB
JavaScript
/* 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)
|