65 lines
2.7 KiB
JavaScript
65 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
||
'use strict'
|
||
// 桌面版专用:把 plugins/runtime/ 下的运行时插件拷进 dist/plugins/。
|
||
// 网页版插件由 Cloudflare KV 在 /plugins/* 提供;桌面端没有服务端,只能随包发布。
|
||
// 刻意不改 scripts/build.js(上游文件),单独一步执行,保持桌面侧差异最小。
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const src = path.join(root, 'plugins', 'runtime')
|
||
const dst = path.join(root, 'dist', 'plugins')
|
||
|
||
if (!fs.existsSync(src)) {
|
||
console.error('[copy-plugins] 缺少 plugins/runtime,无法打包运行时插件')
|
||
process.exit(1)
|
||
}
|
||
fs.mkdirSync(dst, { recursive: true })
|
||
|
||
// 2026-12-03:文本类插件文件统一按 LF 写入 dist。
|
||
// Windows 构建机 core.autocrlf=true 时,检出的 .js 是 CRLF;直接 copyFile 会让 dist 里的字节
|
||
// 与 plugins/index.json 记的 SRI/sha256 对不上 → WebView2 拒绝执行(style-source 真实踩过)。
|
||
const TEXT_EXT = new Set(['.js', '.css', '.json', '.html', '.svg', '.txt', '.md'])
|
||
function copyNormalized(from, to) {
|
||
if (TEXT_EXT.has(path.extname(from).toLowerCase())) {
|
||
const buf = fs.readFileSync(from)
|
||
const txt = buf.toString('utf8')
|
||
if (txt.indexOf('\r\n') >= 0) {
|
||
fs.writeFileSync(to, txt.replace(/\r\n/g, '\n'))
|
||
return
|
||
}
|
||
}
|
||
fs.copyFileSync(from, to)
|
||
}
|
||
function normalizeTree(dir) {
|
||
for (const name of fs.readdirSync(dir)) {
|
||
const abs = path.join(dir, name)
|
||
const st = fs.statSync(abs)
|
||
if (st.isDirectory()) { normalizeTree(abs); continue }
|
||
if (!TEXT_EXT.has(path.extname(name).toLowerCase())) continue
|
||
const txt = fs.readFileSync(abs, 'utf8')
|
||
if (txt.indexOf('\r\n') >= 0) fs.writeFileSync(abs, txt.replace(/\r\n/g, '\n'))
|
||
}
|
||
}
|
||
|
||
let count = 0
|
||
for (const name of fs.readdirSync(src)) {
|
||
copyNormalized(path.join(src, name), path.join(dst, name))
|
||
count++
|
||
}
|
||
// v3.25.41(M1):目录插件 <plugins>/<id>/{manifest.json,main.js} 一并进包(应用按 /plugins/<id>/… 取)
|
||
let folders = 0
|
||
const pluginsRoot = path.join(root, 'plugins')
|
||
for (const name of fs.readdirSync(pluginsRoot)) {
|
||
const abs = path.join(pluginsRoot, name)
|
||
if (!fs.statSync(abs).isDirectory()) continue
|
||
if (name === 'runtime' || name === 'local') continue
|
||
if (!fs.existsSync(path.join(abs, 'manifest.json'))) continue
|
||
fs.cpSync(abs, path.join(dst, name), { recursive: true })
|
||
normalizeTree(path.join(dst, name))
|
||
folders++
|
||
}
|
||
// 索引与插件文件必须同源同包:应用按 index.json 里的 filename / dir 去 /plugins/ 取
|
||
copyNormalized(path.join(root, 'plugins', 'index.json'), path.join(dst, 'index.json'))
|
||
console.log('[copy-plugins] 已拷入 ' + count + ' 个文件 + ' + folders + ' 个目录插件 → dist/plugins/')
|