Files
iwb-plugins-desktop/scripts/validate.mjs
T
qiqi 23cba0ccf3
validate / validate (push) Canceled after 0s
sync(gallery,promptlib): 把桌面仓产物层的改动回写为源码(gallery 1.9.5 / promptlib 1.14.9)
背景:11-28 的图库性能改造与提示词库参考图开关/勾选框/暗色对比度,是直接改在桌面仓
plugins/<id>/main.js 上的(产物层),本仓源码没跟上,导致本仓停在 gallery 1.7.5 / promptlib 1.13.1。

- gallery/src/index.js、styles.css ← 桌面仓 plugins/gallery(虚拟列表、拖动只改列宽、按宽高比占位等)
- promptlib/src/index.js、styles.css ← 桌面仓 plugins/promptlib(参考图上移、自绘蓝勾、accent token、兜底)
- 两处 package.json 版本与 entry 内 register manifest 的 version 同步到 1.9.5 / 1.14.9
  (此前 entry 内还是 1.7.5 / 1.14.0,被 validate 判为不一致)

scripts/validate.mjs:字段扫描改为「优先 register( 之后,找不到再回退整份源码」。
旧逻辑在整份源码里找第一个 id:/version:,会被 promptlib 业务代码里的 { id: 'q' + Date.now() } 误命中,
误报「entry 内 id (q) 与 ibwPlugin.id 不一致」;反过来只扫 register 之后又会让 __IWB_ANNOTATE_MANIFEST__ 这类
构建产物插件找不到字段。现在两段扫描,validate 15/15 通过。
2026-11-29 12:16:06 +08:00

97 lines
5.3 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.
// 校验本仓每个插件的契约:package.json 的 ibwPlugin 字段、entry 语法、以及 entry 内 manifest 与契约一致
//(含 version 与 package.json 一致 —— 需构建的插件由 build-plugin.mjs 从 package.json 注入版本)。
// 只读源码、不构建(dist/ 被 gitignore),因此 CI 不需要网络或依赖。
//
// 用法:node scripts/validate.mjs (退出码 0 = 全部通过)
import { readdirSync, readFileSync, existsSync, statSync } from 'node:fs'
import { join, resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawnSync } from 'node:child_process'
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const ID_PATTERN = /^[a-z][a-z0-9-]{2,31}$/
const SKIP = new Set(['node_modules', 'dist', '.git', '.github', '.omo', 'scripts'])
export const checkPlugin = (dir) => {
const errors = []
const pkgPath = join(dir, 'package.json')
if (!existsSync(pkgPath)) return ['缺少 package.json']
let pkg = null
try { pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) } catch (e) { return ['package.json 不是合法 JSON:' + e.message] }
const meta = pkg && pkg.ibwPlugin
if (!meta || typeof meta !== 'object') return ['package.json 缺少 ibwPlugin 字段']
const id = String(meta.id || '')
if (!ID_PATTERN.test(id)) errors.push('ibwPlugin.id 不合法(须 ^[a-z][a-z0-9-]{2,31}$):' + JSON.stringify(meta.id))
if (!Number.isInteger(meta.apiVersion) || meta.apiVersion < 1) errors.push('ibwPlugin.apiVersion 必须是正整数:' + JSON.stringify(meta.apiVersion))
const entry = String(meta.entry || '')
if (!entry) { errors.push('ibwPlugin.entry 缺失'); return errors }
const entryPath = join(dir, entry)
if (!existsSync(entryPath)) { errors.push('entry 文件不存在:' + entry); return errors }
const src = readFileSync(entryPath, 'utf8')
const syntax = spawnSync(process.execPath, ['--check', entryPath], { encoding: 'utf8' })
if (syntax.status !== 0) errors.push('entry 语法检查失败:' + String(syntax.stderr || '').trim().split('\n')[0])
if (src.indexOf('IWBPluginHost.register(') < 0) errors.push('entry 未调用 IWBPluginHost.register(manifest, factory)')
// v1.0.2:只在 register(...) 的 manifest 块里找字段。
// 旧写法在整份源码里找第一个 id:/version:,会被业务代码里的 { id: 'q' + Date.now() } 误命中
// (promptlib 就因此被判「entry 内 id (q) 与 ibwPlugin.id 不一致」)。
const regAt = src.indexOf('IWBPluginHost.register(')
// 从 register( 开始截到文件末尾:manifest 一定在 factory 之前,而业务代码里的同名键都在 register 之前
const regBlock = regAt >= 0 ? src.slice(regAt) : src
// 两段扫描:优先 register( 之后(避开业务代码里的同名键),找不到字面量再回退整份源码——
// image-annotate / image-vectorize 是构建产物,manifest 用 __IWB_XXX_MANIFEST__ 常量传入。
const pick = (re) => re.exec(regBlock) || re.exec(src)
const idMatch = pick(/(?:^|[\s{,])id:\s*'([^']+)'/)
if (!idMatch) errors.push("entry 内未找到 id: '<id>' 字面量(manifest 必须显式声明 id)")
else if (idMatch[1] !== id) errors.push('entry 内 id (' + idMatch[1] + ') 与 ibwPlugin.id (' + id + ') 不一致')
const apiMatch = pick(/apiVersion:\s*(\d+)/)
if (!apiMatch) errors.push('entry 内未找到 apiVersion 字面量')
else if (Number(apiMatch[1]) !== meta.apiVersion) errors.push('entry 内 apiVersion (' + apiMatch[1] + ') 与 ibwPlugin.apiVersion (' + meta.apiVersion + ') 不一致')
const versionMatch = pick(/version:\s*'([^']+)'/)
if (!versionMatch) errors.push("entry 内未找到 version: '<ver>' 字面量")
else if (versionMatch[1] !== String(pkg.version)) errors.push('entry 内 version (' + versionMatch[1] + ') 与 package.json version (' + pkg.version + ') 不一致')
if (!/name:\s*'[^']+'/.test(regBlock) && !/name:\s*'[^']+'/.test(src)) errors.push("entry 内未找到 name: '<name>' 字面量")
if (!/capabilities:\s*\[/.test(regBlock) && !/capabilities:\s*\[/.test(src)) errors.push('entry 内未找到 capabilities 数组')
return errors
}
const discover = () => {
const out = []
for (const name of readdirSync(ROOT)) {
if (SKIP.has(name)) continue
const dir = join(ROOT, name)
try { if (!statSync(dir).isDirectory()) continue } catch (e) { continue }
if (existsSync(join(dir, 'package.json'))) out.push(dir)
}
return out.sort()
}
const main = () => {
const dirs = discover()
if (!dirs.length) { console.error('[validate] 未发现任何插件目录'); process.exit(1) }
const seen = new Map()
let failed = 0
for (const dir of dirs) {
const name = dir.slice(ROOT.length + 1)
const errors = checkPlugin(dir)
let id = ''
try { id = String(JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).ibwPlugin.id || '') } catch (e) { /* 上面已报 */ }
if (id && seen.has(id)) errors.push('插件 id 与 ' + seen.get(id) + ' 重复:' + id)
else if (id) seen.set(id, name)
if (errors.length) { failed++; console.error('✗ ' + name + '\n - ' + errors.join('\n - ')) }
else console.log('✓ ' + name + ' (id=' + id + ')')
}
console.log('[validate] ' + (dirs.length - failed) + '/' + dirs.length + ' 通过')
if (failed) process.exit(1)
}
if (process.argv[1] && resolve(process.argv[1]) === resolve(fileURLToPath(import.meta.url))) main()