Files
iwb-plugins-desktop/scripts/validate.mjs
T
qiqi d99173321e chore(validate): entry version 与 package.json 交叉核对;README 补复杂插件约定
- 需构建的插件由 build-plugin.mjs 从 package.json 注入版本,校验器负责兜底
- 根 README 记录 plugin/ → src/index.js 的三段构建原因与 shared/ 约定
- 修 xianyi-cutout/README 里 plugins-publish.sh 的过时警示(现已产出 integrity)
2026-10-19 14:03:55 +08:00

88 lines
4.5 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)')
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 + ') 不一致')
const versionMatch = /version:\s*'([^']+)'/.exec(src)
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(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()