Files
image-workbench-desktop/scripts/check-plugins-sync.js
T

98 lines
5.4 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 + '(源仓内容变了但没同步)')
}
}
// ── 图片策略守卫(v3.26.124,warn 阶段)─────────────────────────────────
// 插件里出现「取整幅原图再自己缩放」的写法,在 WebKit 上每次都是 21MB 级解码,
// 正是 mac 端 GB 级占用的来源。这里只报警不阻塞(下一版转 error)。
const IMG_WARN = [
{ re: /ctx\.images\.dataUrl\(/g, why: 'ctx.images.dataUrl 会取整幅原图;预览请用 ctx.images.preview({max}),网格请用 thumb({max})' },
{ re: /getNodeImage\(\s*[A-Za-z0-9_.$]+\s*\)/g, why: 'getNodeImage 未传 {max}:插件只需看/预览时请传 {max:2048}(导出才拿原图)' },
{ re: /createImageBitmap\(/g, why: '插件里 createImageBitmap 会整幅解码;改用 ctx.images.thumb/preview 让宿主原生缩放' },
]
try {
for (const id of fs.readdirSync(srcRoot)) {
const entry = path.join(srcRoot, id, 'src', 'index.js')
if (!fs.existsSync(entry)) continue
const code = fs.readFileSync(entry, 'utf8')
for (const rule of IMG_WARN) {
const hits = (code.match(rule.re) || []).length
if (hits) console.log('[img-policy][warn] ' + id + ' ×' + hits + ':' + rule.why)
}
}
} catch (e) { /* 源码仓不可读时跳过 */ }
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 + ' 个插件)')