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)
This commit is contained in:
qiqi committed 2026-10-18 01:44:49 +08:00
1 parent 8b4749ba1f
commit a2ead34483
4 files changed
+114

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
name: validate
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: node scripts/validate.mjs
+8
View File
@@ -23,6 +23,14 @@ npm run deploy:test # 发到测试站
`plugins-sync.sh` 会打印来源 `仓库@tag (sha)`,请把它写进宿主的提交信息,便于追溯。
## 校验(本地 + CI)
```bash
npm run validate # 校验每个插件的 ibwPlugin 契约、entry 语法、entry 内 manifest 与契约是否一致
```
`.github/workflows/ci.yml` 在 push / PR 时跑同一条命令(只读源码,不需要网络或依赖)。
## 插件契约(写插件前必读)
每个插件 = **一个 IIFE 文件**,加载时向宿主注册:
+9
View File
@@ -0,0 +1,9 @@
{
"name": "@iwb-plugin/workspace",
"private": true,
"version": "1.0.0",
"description": "IWB 插件仓:只写插件,不复制宿主契约",
"scripts": {
"validate": "node scripts/validate.mjs"
}
}
+84
View File
@@ -0,0 +1,84 @@
// 校验本仓每个插件的契约: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()