Files
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

133 lines
6.3 KiB
JavaScript
Raw Permalink 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.
#!/usr/bin/env node
'use strict'
// 生成插件市场索引(M3):plugins/index.json + plugins/meta.json(+ 目录插件)→ market/
//
// 产物(纯静态,供 market.maqiart.eu.org 与桌面端安装器共用):
// market/index.json 市场条目:bundle(zip) + files[逐文件 sha256] + 兼容字段 file
// market/plugins/<id>-<version>.zip 目录插件包:manifest.json + main.js [+ styles.css / assets]
// market/plugins/plugin-<id>.<hash>.js 单文件产物(旧客户端仍可安装,保持向后兼容)
//
// 包里统一用 STORE(不压缩):客户端 src/storage-zip.js 对 DEFLATE 依赖 DecompressionStream,
// 旧 macOS 的 WKWebView 没有该 API;插件包只有几十 KB,不压缩没有实际损失。
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const { zipStore } = require('./plugin-zip')
// v3.26.109:发市场前先确认「桌面仓 plugins/ == 插件源仓」(源仓不存在则跳过)。
// 2026-11-28 出现过直接改产物层、源仓停在旧版本,两仓分叉;这条守卫把问题挡在发市场之前。
try {
require('child_process').execFileSync(process.execPath, [path.join(__dirname, 'check-plugins-sync.js')], { stdio: 'inherit' })
} catch (e) {
console.error('[gen-market] 插件两仓不一致,已中止(先同步源码再发市场)')
process.exit(1)
}
const root = path.resolve(__dirname, '..')
const man = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'index.json'), 'utf8'))
const meta = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'meta.json'), 'utf8'))
const out = path.join(root, 'market')
fs.mkdirSync(path.join(out, 'plugins'), { recursive: true })
const sha = (buf) => crypto.createHash('sha256').update(buf)
const shaHex = (buf) => sha(buf).digest('hex')
function readFolderFiles (id) {
// 目录插件:manifest.json + main.js + 可选 styles.css + assets/**(限两级)
const dir = path.join(root, 'plugins', id)
const files = []
const manifestPath = path.join(dir, 'manifest.json')
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
files.push({ name: 'manifest.json', data: fs.readFileSync(manifestPath) })
const main = String(manifest.main || 'main.js')
files.push({ name: main, data: fs.readFileSync(path.join(dir, main)) })
if (manifest.style && fs.existsSync(path.join(dir, manifest.style))) files.push({ name: String(manifest.style), data: fs.readFileSync(path.join(dir, String(manifest.style))) })
const assetsDir = path.join(dir, 'assets')
if (fs.existsSync(assetsDir)) {
for (const name of fs.readdirSync(assetsDir)) {
const abs = path.join(assetsDir, name)
if (fs.statSync(abs).isFile()) files.push({ name: 'assets/' + name, data: fs.readFileSync(abs) })
}
}
// v3.26.109:manifest.files 是客户端装载时校验入口文件的依据;写成旧值会让市场安装直接
// 「完整性校验失败(main.js 的 sha256 不符)」(promptlib 1.14.0 就这样发出去过一次)。
// 生成阶段就对照真实字节,不符直接中断,别等用户装不上才发现。
const bad = []
for (const [name, expect] of Object.entries(manifest.files || {})) {
const f = files.find((x) => x.name === name)
if (!f) { bad.push(name + ' 缺失'); continue }
const actual = shaHex(f.data)
if (actual !== String(expect).toLowerCase()) bad.push(name + ' 期望 ' + String(expect).slice(0, 8) + ' 实际 ' + actual.slice(0, 8))
}
if (bad.length) {
console.error('[gen-market] ' + String(manifest.id) + ' 的 manifest.files 与实际字节不符:' + bad.join(';'))
console.error('[gen-market] 修好 manifest.files 再发市场(否则客户端会拒绝装载)')
process.exit(1)
}
return { files, manifest }
}
const plugins = []
let zipped = 0
for (const e of man) {
const m = meta[e.id] || {}
const base = {
id: e.id, name: m.name || e.id, desc: m.desc || '', author: m.author || 'official',
icon: m.icon || '', color: m.color || '', tags: Array.isArray(m.tags) ? m.tags : [],
version: e.version, apiVersion: 1,
}
// ---- 目录形态:打包 manifest + main(+style/assets) ----
let files = null
if (e.dir) {
const got = readFolderFiles(e.dir)
files = got.files
} else {
const src = path.join(root, 'plugins', 'runtime', e.file)
if (!fs.existsSync(src)) { console.warn('缺少产物:' + e.file); continue }
const js = fs.readFileSync(src)
// 单文件插件也打成目录包:manifest.json 由市场索引生成,main.js 就是原产物
// manifest 里带 files(main.js 的 sha256):手工把 zip 解到 <plugins>/<id>/ 时,
// 加载器扫描目录也能校验入口文件(manifest 自身不参与,避免自引用)。
const manifestJson = {
id: e.id, name: base.name, version: e.version, apiVersion: 1, main: 'main.js',
description: base.desc, author: base.author, icon: base.icon, tags: base.tags,
files: { 'main.js': shaHex(js) },
}
files = [
{ name: 'manifest.json', data: Buffer.from(JSON.stringify(manifestJson, null, 2) + '\n', 'utf8') },
{ name: 'main.js', data: js },
]
}
const fileList = files.map((f) => ({ path: f.name, sha256: shaHex(f.data), size: f.data.length }))
const zip = zipStore(files)
const zipName = e.id + '-' + e.version + '.zip'
fs.writeFileSync(path.join(out, 'plugins', zipName), zip)
zipped++
const item = Object.assign({}, base, {
bundle: 'plugins/' + zipName,
bundleSize: zip.length,
bundleSha256: shaHex(zip),
files: fileList,
})
// 兼容:单文件插件继续提供裸 .js(旧客户端 install 只认 file/integrity)
if (!e.dir) {
const src = path.join(root, 'plugins', 'runtime', e.file)
const buf = fs.readFileSync(src)
fs.writeFileSync(path.join(out, 'plugins', e.file), buf)
item.file = 'plugins/' + e.file
item.size = buf.length
item.sha256 = shaHex(buf)
item.integrity = 'sha256-' + sha(buf).digest('base64')
}
plugins.push(item)
}
const index = { version: 2, updatedAt: new Date().toISOString(), plugins: plugins }
fs.writeFileSync(path.join(out, 'index.json'), JSON.stringify(index, null, 2) + '\n')
console.log('market/index.json:' + plugins.length + ' 个插件 / ' + zipped + ' 个 zip 包')
for (const p of plugins) console.log(' ' + p.id.padEnd(16) + String(p.version).padEnd(8) + (p.bundleSize + ' B zip'))