From a2ead344835bae6084310b5a7782fab574a81ae4 Mon Sep 17 00:00:00 2001 From: qiqi Date: Sun, 18 Oct 2026 01:44:49 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=8F=92=E4=BB=B6=E4=BB=93=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=EF=BC=88P4-15=20=E4=B9=8B=E4=B8=89=EF=BC=89=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=A5=91=E7=BA=A6=E6=A0=A1=E9=AA=8C=E5=99=A8=20+=20Gi?= =?UTF-8?q?tHub=20Actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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) --- .github/workflows/ci.yml | 13 +++++++ README.md | 8 ++++ package.json | 9 +++++ scripts/validate.mjs | 84 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 package.json create mode 100644 scripts/validate.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2795216 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index a598ffe..d344ae6 100644 --- a/README.md +++ b/README.md @@ -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 文件**,加载时向宿主注册: diff --git a/package.json b/package.json new file mode 100644 index 0000000..d0a932e --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "@iwb-plugin/workspace", + "private": true, + "version": "1.0.0", + "description": "IWB 插件仓:只写插件,不复制宿主契约", + "scripts": { + "validate": "node scripts/validate.mjs" + } +} diff --git a/scripts/validate.mjs b/scripts/validate.mjs new file mode 100644 index 0000000..89680f4 --- /dev/null +++ b/scripts/validate.mjs @@ -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: '' 字面量(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: '' 字面量") + if (!/name:\s*'[^']+'/.test(src)) errors.push("entry 内未找到 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()