Files
image-workbench-desktop/scripts/check-plugins-sync.js
T
qiqi fd9575a96e
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m0s
chore(plugins): 两仓同步 + 一致性守卫 + 插件规范(gallery 1.9.6 / promptlib 1.14.9 / sticky 1.0.8)
插件源码改回源仓(iwb-plugins-desktop),桌面仓 plugins/ 只作产物:
- 源仓侧:把 11-28 直接改在产物层的图库/提示词库改动回写为源码;修 validate 的字段扫描(先扫 register 块,
  找不到再回退整份文件,避免被业务代码 id: 'q' 误命中、也兼顾 __IWB_*_MANIFEST__ 构建产物);
  gallery 1.9.6 修「全部重试无效」与「量不到宽度时塌成 1 列」,promptlib 1.14.9
- 本仓侧:node scripts/sync-plugins-from-repo.js 重新生成 plugins/(gallery 1.9.6 / promptlib 1.14.9 / sticky 1.0.8)

新增机制:
- scripts/check-plugins-sync.js:逐插件比对产物与源仓(main.js/styles.css 的 sha256 + manifest/index 版本),
  不一致列出清单并给出修复命令;源仓不存在时跳过(CI/别人机器不阻塞)
- gen-market.js 开头调用该守卫:两仓不一致直接中止,不允许发市场
- package.json: npm run check:plugins
- docs/standards/plugin-source-of-truth.md:两仓职责、目录对照、三条红线、日常流程、应急回写步骤、常见错误对照

验证:源仓 npm run validate 15/15、npm test 全绿;桌面仓 verify:ci 32 passed。
2026-11-29 12:30:01 +08:00

78 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// 插件两仓一致性守卫(v3.26.109)
//
// 约定:插件源码只在 iwb-plugins-desktop 改,桌面仓 plugins/ 是**产物**。
// 但 2026-11-28 出现过「直接改桌面仓产物层、源仓没跟上」:源仓停在 gallery 1.7.5 / promptlib 1.13.1,
// 而市场已经发了 1.9.x / 1.14.x,两边内容与版本分叉。
//
// 这个脚本把「产物是否等于源仓 + 版本是否一致」变成一条命令;gen-market 前会自动跑。
// node scripts/check-plugins-sync.js # 默认源仓 ../iwb-plugins-desktop
// node scripts/check-plugins-sync.js <源仓路径>
// 源仓不存在时跳过(CI / 别人机器上不阻塞),打印一行提示。
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const root = path.resolve(__dirname, '..')
const srcRoot = process.argv[2] ? path.resolve(process.argv[2]) : path.resolve(root, '..', 'iwb-plugins-desktop')
const sha = (buf) => crypto.createHash('sha256').update(buf).digest('hex')
if (!fs.existsSync(srcRoot)) {
console.log('[check-plugins] 跳过:找不到插件源仓 ' + srcRoot + '(只在有源仓的机器上校验)')
process.exit(0)
}
const index = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'index.json'), 'utf8'))
const byId = new Map(index.map((e) => [e.id, e]))
const problems = []
for (const id of fs.readdirSync(srcRoot)) {
const dir = path.join(srcRoot, id)
if (!fs.statSync(dir).isDirectory()) continue
const pkgPath = path.join(dir, 'package.json')
const entry = path.join(dir, 'src', 'index.js')
if (!fs.existsSync(pkgPath) || !fs.existsSync(entry)) continue // template / node_modules / ai-chat 等
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
const meta = pkg.ibwPlugin || {}
if (!meta.id || meta.id !== id) continue
// 只写不发市场的插件(ai-chat 需本机 Magpie;template 是脚手架样例)不参与一致性比对
if (id === 'ai-chat' || id === 'template') continue
const idx = byId.get(id)
if (!idx) { problems.push(id + ':源仓有、桌面仓 plugins/index.json 没有(漏同步)'); continue }
if (String(idx.version) !== String(pkg.version)) {
problems.push(id + ':版本不一致(源仓 ' + pkg.version + ' / 桌面 ' + idx.version + ')')
}
const srcBytes = fs.readFileSync(entry)
const styleSrc = path.join(dir, 'styles.css')
const folderMode = fs.existsSync(styleSrc) || !!idx.dir
if (folderMode) {
const out = path.join(root, 'plugins', idx.dir || id, idx.main || 'main.js')
if (!fs.existsSync(out)) { problems.push(id + ':缺少产物 ' + path.relative(root, out)); continue }
if (sha(fs.readFileSync(out)) !== sha(srcBytes)) problems.push(id + ':产物 main.js 与源仓 src/index.js 不一致')
if (fs.existsSync(styleSrc)) {
const outStyle = path.join(root, 'plugins', idx.dir || id, idx.style || 'styles.css')
if (!fs.existsSync(outStyle) || sha(fs.readFileSync(outStyle)) !== sha(fs.readFileSync(styleSrc))) {
problems.push(id + ':产物 styles.css 与源仓不一致')
}
}
const man = path.join(root, 'plugins', idx.dir || id, 'manifest.json')
if (fs.existsSync(man)) {
const m = JSON.parse(fs.readFileSync(man, 'utf8'))
if (String(m.version) !== String(pkg.version)) problems.push(id + ':manifest.json 版本 ' + m.version + ' ≠ 源仓 ' + pkg.version)
}
} else {
const expected = 'plugin-' + id + '.' + sha(srcBytes).slice(0, 12) + '.js'
const out = path.join(root, 'plugins', 'runtime', expected)
if (!fs.existsSync(out)) problems.push(id + ':缺少产物 plugins/runtime/' + expected + '(源仓内容变了但没同步)')
}
}
if (problems.length) {
console.error('[check-plugins] 两仓不一致(' + problems.length + ' 项):')
for (const p of problems) console.error(' - ' + p)
console.error('[check-plugins] 修法:在 ' + srcRoot + ' 改源码 → 本仓 node scripts/sync-plugins-from-repo.js → 再发市场')
console.error('[check-plugins] 禁止直接改本仓 plugins/ 里的产物(那是构建输出,会被下一次 sync 覆盖)')
process.exit(1)
}
console.log('[check-plugins] 通过:桌面仓 plugins/ 与插件源仓一致(' + index.length + ' 个插件)')