Files
image-workbench-desktop/scripts/sync-plugins-from-repo.js
T
qiqi 7ed3ba6738 feat(plugin): 插件目录化 M1 —— 目录插件加载 + 旧单文件兼容 v3.25.41
- loader 新增 loadFolder():manifest → main.js(逐文件 sha256,内置信随包 index.json)→ blob 注入;
  样式文本暂存,激活时注入、停用时移除(配合 host 新增 onActivate/onDeactivate 钩子)
- storage-file 增加 plugins.listDirs/readFile/writeFile/writeBinary/mkdir/removeDir(按段校验,禁 ..)
- app.js:三路合并支持目录条目(dir/main/style/files),失败隔离 + 按 id 排序
- 修复:无 iwb_plugins 记录时插件永不激活(首次安装面板显示「未加载」)
- 备份模块兼容目录形态;市场暂时跳过目录插件(M3 zip 后恢复)
- 试点:image-annotate 转为 plugins/image-annotate/{manifest.json,main.js}
- 方案文档 docs/PLUGIN-ARCHITECTURE-PLAN.md 一并入库
2026-11-20 09:08:56 +08:00

72 lines
3.9 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.
#!/usr/bin/env node
'use strict'
// 从插件源仓同步产物到本仓 plugins/runtime/ 并重建 plugins/index.json(内容寻址 + SRI)。
// 用法:node scripts/sync-plugins-from-repo.mjs [源仓路径,默认 ../iwb-plugins-desktop]
// 幂等:内容不变时生成完全相同的文件名与清单(可直接用 git diff 验证)。
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const root = path.resolve(__dirname, '..')
const srcRoot = process.argv[2] ? path.resolve(process.argv[2]) : path.resolve(root, '..', 'iwb-plugins-desktop')
const outDir = path.join(root, 'plugins', 'runtime')
const IDS = ['batch-export', 'blank-canvas', 'browser', 'image-annotate', 'image-vectorize', 'style-source', 'white-bg-800', 'xianyi-cutout']
// v3.25.41(M1):目录化插件试点 —— 这些 id 产出 <id>/{manifest.json,main.js},其余仍是单文件(双轨兼容)
const FOLDER_IDS = ['image-annotate']
const sha = (buf) => crypto.createHash('sha256').update(buf).digest()
const shaHex = (buf) => sha(buf).toString('hex')
const sri = (buf) => 'sha256-' + sha(buf).toString('base64')
if (!fs.existsSync(srcRoot)) { console.error('找不到插件源仓:' + srcRoot); process.exit(1) }
fs.mkdirSync(outDir, { recursive: true })
const manifest = []
let copied = 0, changed = 0
const metaAll = (() => { try { return JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'meta.json'), 'utf8')) } catch (e) { return {} } })()
for (const id of IDS) {
const artifact = path.join(srcRoot, id, 'src', 'index.js')
if (!fs.existsSync(artifact)) { console.warn('跳过 ' + id + '(无 ' + artifact + ')'); continue }
const buf = fs.readFileSync(artifact)
const pkg = JSON.parse(fs.readFileSync(path.join(srcRoot, id, 'package.json'), 'utf8'))
if (FOLDER_IDS.indexOf(id) >= 0) {
// 目录形态:plugins/<id>/{manifest.json,main.js}
const dir = path.join(root, 'plugins', id)
fs.mkdirSync(dir, { recursive: true })
const mainPath = path.join(dir, 'main.js')
if (!fs.existsSync(mainPath) || shaHex(fs.readFileSync(mainPath)) !== shaHex(buf)) { fs.writeFileSync(mainPath, buf); copied++ }
const meta = metaAll[id] || {}
const manifestJson = {
id: id,
name: meta.name || id,
version: pkg.version,
apiVersion: Number((pkg.ibwPlugin && pkg.ibwPlugin.apiVersion) || 1),
main: 'main.js',
description: meta.desc || '',
author: meta.author || 'official',
icon: meta.icon || '',
tags: Array.isArray(meta.tags) ? meta.tags : [],
files: { 'main.js': shaHex(buf) },
}
fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify(manifestJson, null, 2) + '\n')
// 同 id 的旧单文件产物清理(目录优先,留着会造成两份)
for (const old of fs.readdirSync(outDir)) {
if (old.indexOf('plugin-' + id + '.') === 0) { fs.unlinkSync(path.join(outDir, old)); changed++ }
}
manifest.push({ id: id, dir: id, main: 'main.js', style: manifestJson.style || '', version: pkg.version, files: manifestJson.files })
console.log(id.padEnd(16), pkg.version.padEnd(8), 'dir/' + id + '/main.js')
continue
}
const name = 'plugin-' + id + '.' + shaHex(buf).slice(0, 12) + '.js'
for (const old of fs.readdirSync(outDir)) {
if (old.indexOf('plugin-' + id + '.') === 0 && old !== name) { fs.unlinkSync(path.join(outDir, old)); changed++ }
}
const target = path.join(outDir, name)
if (!fs.existsSync(target) || shaHex(fs.readFileSync(target)) !== shaHex(buf)) { fs.writeFileSync(target, buf); copied++ }
manifest.push({ id: id, file: name, version: pkg.version, integrity: sri(buf) })
console.log(id.padEnd(16), pkg.version.padEnd(8), name)
}
fs.writeFileSync(path.join(root, 'plugins', 'index.json'), JSON.stringify(manifest, null, 2) + '\n')
console.log('完成:' + manifest.length + ' 个插件,新写入 ' + copied + ' 个,清理旧产物 ' + changed + ' 个')