Files
qiqi a2ead34483 ci: 插件仓校验(P4-15 之三)——契约校验器 + GitHub Actions
- scripts/validate.mjs:逐个插件校验
  · package.json 的 ibwPlugin(id 须 ^[a-z][a-z0-9-]{2,31}$、apiVersion 正整数、entry 存在)
  · entry 语法(node --check)+ 必须调用 IWBPluginHost.register(
  · entry 内 manifest 与契约一致(id / apiVersion 对齐、name/version/capabilities 齐备)
  · 仓库内 id 不得重复
  只读源码、不构建 ⇒ CI 无需网络或依赖
- .github/workflows/ci.yml:push / PR 跑 node scripts/validate.mjs
- package.json(根,private):npm run validate
- README:补「校验(本地 + CI)」段

本地验证:node scripts/validate.mjs → 2/2 通过(template-demo / xianyi-cutout)
2026-10-18 01:44:49 +08:00

85 lines
4.2 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.
// 校验本仓每个插件的契约:package.json 的 ibwPlugin 字段、entry 语法、以及 entry 内 manifest 与契约一致。
// 只读源码、不构建(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)')
const idMatch = /(?:^|[\s{,])id:\s*'([^']+)'/.exec(src)
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 = /apiVersion:\s*(\d+)/.exec(src)
if (!apiMatch) errors.push('entry 内未找到 apiVersion 字面量')
else if (Number(apiMatch[1]) !== meta.apiVersion) errors.push('entry 内 apiVersion (' + apiMatch[1] + ') 与 ibwPlugin.apiVersion (' + meta.apiVersion + ') 不一致')
if (!/version:\s*'[^']+'/.test(src)) errors.push("entry 内未找到 version: '<ver>' 字面量")
if (!/name:\s*'[^']+'/.test(src)) errors.push("entry 内未找到 name: '<name>' 字面量")
if (!/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()