95 lines
5.5 KiB
JavaScript
95 lines
5.5 KiB
JavaScript
#!/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 = ['album', 'batch-export', 'blank-canvas', 'browser', 'gallery', 'grouplib', 'promptlib', 'image-annotate', 'image-vectorize', 'sticky', 'style-source', 'white-bg-800', 'xianyi-cutout']
|
||
// v3.25.41(M1):目录化插件试点 —— 这些 id 产出 <id>/{manifest.json,main.js},其余仍是单文件(双轨兼容)
|
||
// ⚠️ 2026-11-20 修正:桌面包不随包发插件(scripts/strip-plugins.js),插件只能从市场装,
|
||
// 而市场目前只支持单文件 ⇒ 目录形态暂不用于生产插件,等 M3(zip 安装)落地再启用。
|
||
// 目录形态(plugins/<id>/{manifest.json,main.js[,styles.css]})的手写插件。
|
||
// 注意:源仓里存在 <id>/styles.css 的插件会**自动**走目录形态 —— 单文件产物带不走样式,
|
||
// 若不同步成目录,插件自带样式会被静默忽略(v1.0.3 grouplib 换行样式的坑)。
|
||
const FOLDER_IDS = ['gallery']
|
||
|
||
// 刻意不随客户端/市场发布的插件(开发者直装):
|
||
// ai-chat —— 依赖本机 Magpie(3425) + 需宿主 CSP 放行,上架会让多数用户装上连不上。
|
||
// 详见 ../iwb-plugins-desktop/ai-chat/README.md「状态」一节。
|
||
const DEV_ONLY_IDS = ['ai-chat']
|
||
|
||
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'))
|
||
|
||
const folderMode = FOLDER_IDS.indexOf(id) >= 0 || fs.existsSync(path.join(srcRoot, id, 'styles.css'))
|
||
if (folderMode) {
|
||
// 目录形态: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] || {}
|
||
// 可选 styles.css:插件自带样式(宿主 CSS 只在客户端发版时更新,视觉调整走插件更灵活)
|
||
const files = { 'main.js': shaHex(buf) }
|
||
let styleName = ''
|
||
const styleSrc = path.join(srcRoot, id, 'styles.css')
|
||
if (fs.existsSync(styleSrc)) {
|
||
const sbuf = fs.readFileSync(styleSrc)
|
||
const styleDest = path.join(dir, 'styles.css')
|
||
if (!fs.existsSync(styleDest) || shaHex(fs.readFileSync(styleDest)) !== shaHex(sbuf)) { fs.writeFileSync(styleDest, sbuf); copied++ }
|
||
styleName = 'styles.css'
|
||
files['styles.css'] = shaHex(sbuf)
|
||
}
|
||
const manifestJson = {
|
||
id: id,
|
||
name: meta.name || id,
|
||
version: pkg.version,
|
||
apiVersion: Number((pkg.ibwPlugin && pkg.ibwPlugin.apiVersion) || 1),
|
||
main: 'main.js',
|
||
...(styleName ? { style: styleName } : {}),
|
||
description: meta.desc || '',
|
||
author: meta.author || 'official',
|
||
icon: meta.icon || '',
|
||
tags: Array.isArray(meta.tags) ? meta.tags : [],
|
||
files: files,
|
||
}
|
||
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 + ' 个') |