diff --git a/docs/standards/plugin-source-of-truth.md b/docs/standards/plugin-source-of-truth.md index 4b844cf..484ad6f 100644 --- a/docs/standards/plugin-source-of-truth.md +++ b/docs/standards/plugin-source-of-truth.md @@ -72,6 +72,7 @@ node scripts/gen-market.js && node scripts/gen-market-page.js | `npm run validate`(源仓) | 源仓内部一致性:id/apiVersion/version 与 package.json 对齐 | 改插件的必跑 | | `npm run check:plugins`(桌面仓) | 桌面仓产物 == 源仓源码,且版本一致 | 手动 / gen-market 内自动 | | `node scripts/gen-market.js` | 生成市场索引与 zip,**开头先跑一致性检查** | 发市场时 | +| `npm run plugins:doctor`(`scripts/plugin-index-doctor.js`) | 体检**资料目录**的 `plugins/index.json` 与实际文件(版本/哈希/缺失/孤儿条目);默认只报告,`--write` 修复并备份 | 客户端报「完整性校验失败」或手工拷过插件时 | `check-plugins-sync.js` 的行为:源仓不存在(CI、别人机器)时打印一行并跳过,不阻塞;存在时逐插件比对 `main.js`/`styles.css` 的 sha256、`manifest.json`/`index.json` 的版本,任何一项不符即非 0 退出并给出修复命令。 @@ -96,7 +97,7 @@ node scripts/gen-market.js && node scripts/gen-market-page.js | 源仓显示「最近更新 3 天前」,但市场已发新版 | 改动只在桌面仓产物层 | 按 §5 回写源仓,或按 §3 从源仓改起 | | `validate` 报「entry 内 version 与 package.json 不一致」 | 只改了 package.json 或只改了 register 里的 version | 两处一起改 | | `validate` 报「entry 内 id (q) 与 ibwPlugin.id 不一致」 | 校验器在整份源码里找第一个 `id:`,命中业务代码 `{ id: 'q' + Date.now() }` | 已修:先扫 `register(` 之后的 manifest 块,找不到再回退整份文件 | -| 插件显示「完整性校验失败(main.js 的 sha256 不符)」 | 手工往 `<资料目录>/plugins/` 拷文件,没同步更新同目录的 `index.json`(它带着旧哈希) | ①正确做法:一律走「设置 → 插件 → 检查更新」安装,安装器会同时写文件和索引;②已手工拷过的,按实际文件重写一次 index.json(备份后重算每个条目的 version 与 files 哈希) | +| 插件显示「完整性校验失败(main.js 的 sha256 不符)」 | 手工往 `<资料目录>/plugins/` 拷文件,没同步更新同目录的 `index.json`(它带着旧哈希) | ①正确做法:一律走「设置 → 插件 → 检查更新」安装,安装器会同时写文件和索引;②已手工拷过的,跑一次 `npm run plugins:doctor -- <资料目录> --write`(先备份,再按实际文件重算版本与哈希) | | 市场装完插件样式丢失 | 插件带 styles.css 但被当单文件同步 | 源仓放 `styles.css` 会自动走目录形态,不要手工拼单文件 | ## 7. 版本与发布的关系 diff --git a/package.json b/package.json index 257caf0..59fa00e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:facts": "node tests/architecture-facts-guard-test.js && node tests/release-records-test.js", "plugins:gc": "node scripts/plugins-publish.mjs --gc", "check:plugins": "node scripts/check-plugins-sync.js", + "plugins:doctor": "node scripts/plugin-index-doctor.js", "hooks:install": "bash scripts/install-git-hooks.sh", "coverage:browser": "npm run build && node scripts/coverage-browser.js", "audit:pins": "node scripts/audit-source-pins.js", diff --git a/scripts/plugin-index-doctor.js b/scripts/plugin-index-doctor.js new file mode 100644 index 0000000..8e3aad3 --- /dev/null +++ b/scripts/plugin-index-doctor.js @@ -0,0 +1,107 @@ +'use strict' +// 资料目录插件索引体检/修复(v3.26.109) +// +// 背景:手工往 <资料目录>/plugins/ 拷插件文件、但没更新同目录的 index.json 时,加载器会按索引里的旧哈希 +// 校验 main.js → 「完整性校验失败(main.js 的 sha256 不符)」(2026-11-29 便利贴 1.0.8 实发)。 +// 这个脚本扫描目录与索引,报告差异;加 --write 会按实际文件重写索引(先备份)。 +// +// 用法: +// node scripts/plugin-index-doctor.js # 默认扫 ~/Documents/ImageWorkbench(只报告) +// node scripts/plugin-index-doctor.js /path/to/data # 指定资料目录 +// node scripts/plugin-index-doctor.js /path/to/data --write # 写入修复 +const fs = require('fs') +const path = require('path') +const os = require('os') +const crypto = require('crypto') + +const args = process.argv.slice(2) +const write = args.includes('--write') +const dataDir = path.resolve(args.find((a) => !a.startsWith('--')) || path.join(os.homedir(), 'Documents', 'ImageWorkbench')) +const dir = path.join(dataDir, 'plugins') +const indexPath = path.join(dir, 'index.json') +const sha = (p) => crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex') + +if (!fs.existsSync(dir)) { console.error('找不到 ' + dir + '(资料目录不对?)'); process.exit(2) } +let index = [] +try { index = JSON.parse(fs.readFileSync(indexPath, 'utf8')) } catch (e) { console.log('索引缺失或损坏,将按目录重建:' + (e && e.message)) } +if (!Array.isArray(index)) index = [] + +const byId = new Map(index.filter((e) => e && e.id).map((e) => [e.id, e])) +const rows = [] +const fixed = [] +const seen = new Set() + +for (const name of fs.readdirSync(dir).sort()) { + const abs = path.join(dir, name) + if (!fs.statSync(abs).isDirectory()) continue + const manifestPath = path.join(abs, 'manifest.json') + if (!fs.existsSync(manifestPath)) { rows.push(['warn', name, '目录里没有 manifest.json(不是插件目录,跳过)']); continue } + let m = null + try { m = JSON.parse(fs.readFileSync(manifestPath, 'utf8')) } catch (e) { rows.push(['bad', name, 'manifest.json 解析失败:' + (e && e.message)]); continue } + const id = String(m.id || name) + seen.add(id) + const main = String(m.main || 'main.js') + const style = String(m.style || '') + const files = {} + const missing = [] + for (const f of ['manifest.json', main, ...(style ? [style] : [])]) { + const p = path.join(abs, f) + if (!fs.existsSync(p)) { missing.push(f); continue } + files[f] = sha(p) + } + const entry = byId.get(id) + const next = { id: id, dir: name, main: main, style: style, version: String(m.version || ''), files: files } + if (missing.length) rows.push(['bad', id, '文件缺失:' + missing.join('、')]) + if (!entry) { + rows.push(['fix', id, '索引里没有这条 → 补登记(v' + next.version + ')']) + fixed.push({ before: null, after: next }) + continue + } + const diffs = [] + if (String(entry.version) !== next.version) diffs.push('版本 ' + entry.version + ' → ' + next.version) + const ef = entry.files || {} + for (const f of Object.keys(files)) { + if (!ef[f]) diffs.push(f + ' 未登记哈希') + else if (ef[f] !== files[f]) diffs.push(f + ' 哈希不符') + } + for (const f of Object.keys(ef)) if (!files[f]) diffs.push(f + ' 索引里有、磁盘上没有') + if (String(entry.dir) !== name) diffs.push('目录名 ' + entry.dir + ' → ' + name) + if (diffs.length) { rows.push(['fix', id, diffs.join(';')]); fixed.push({ before: entry, after: next }) } + else rows.push(['ok', id, 'v' + next.version]) +} + +// 索引里有、磁盘上没有的条目 +for (const e of index) { + if (!e || !e.id || seen.has(e.id)) continue + if (e.dir && !fs.existsSync(path.join(dir, e.dir))) { rows.push(['bad', e.id, '索引登记了但目录不存在 → 建议删除该条目']); fixed.push({ before: e, after: null }) } + else if (!e.dir) rows.push(['warn', e.id, '单文件插件条目(本脚本不处理文件搬运)']) +} + +const order = { bad: 0, fix: 1, warn: 2, ok: 3 } +rows.sort((a, b) => (order[a[0]] - order[b[0]]) || a[1].localeCompare(b[1])) +const mark = { ok: '✅', fix: '🔧', warn: '⚠️', bad: '❌' } +console.log('资料目录:' + dataDir) +console.log('索引:' + indexPath) +console.log('') +for (const [kind, id, msg] of rows) console.log(' ' + mark[kind] + ' ' + id.padEnd(16) + msg) + +const counts = rows.reduce((a, r) => { a[r[0]] = (a[r[0]] || 0) + 1; return a }, {}) +console.log('') +console.log('合计:' + Object.keys(counts).map((k) => mark[k] + ' ' + k + ' ' + counts[k]).join(' ') + '(共 ' + rows.length + ' 个目录)') + +if (!fixed.length) { console.log('索引与实际文件一致,无需修复。'); process.exit(0) } +if (!write) { console.log(''); console.log('以上差异可加 --write 修复(会先备份 index.json)。'); process.exit(1) } + +// 写回:保持原有顺序,更新/补登记/删除失效条目 +const out = [] +for (const e of index) { + if (!e || !e.id) continue + const upd = fixed.find((x) => x.after && x.after.id === e.id) + if (upd) out.push(upd.after) + else if (!fixed.some((x) => !x.after && x.before && x.before.id === e.id)) out.push(e) +} +for (const x of fixed) if (x.after && !out.some((e) => e.id === x.after.id)) out.push(x.after) +const stamp = new Date().toISOString().replace(/[:.]/g, '-') +try { fs.copyFileSync(indexPath, indexPath + '.bak-' + stamp) } catch (e) { /* 首次没有索引 */ } +fs.writeFileSync(indexPath, JSON.stringify(out, null, 2) + '\n') +console.log('已修复并写回:' + out.length + ' 条(备份 index.json.bak-' + stamp + ')')