拆分插件三级配置菜单并统一三插件构建打包
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-06 16:22:38 +08:00
1 parent 7d0e84a381
commit 4ea3a39e41
20 files changed
+486 -66

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
import { spawn } from 'node:child_process'
import { access, mkdir } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
export const root = fileURLToPath(new URL('../', import.meta.url))
export const plugins = [
{ id: 'spam_block', executable: 'spam_block', directory: 'backend', source: './hooks/spam_block' },
{ id: 'telegram_push', executable: 'pmail_telegram_push', directory: 'plugins/pmail_telegram_push', source: '.' },
{ id: 'wechat_push', executable: 'wechat_push', directory: 'backend', source: './hooks/wechat_push' }
]
export const targets = ['linux_amd64', 'linux_arm64', 'windows_amd64', 'windows_arm64', 'darwin_amd64', 'darwin_arm64']
export function hostTarget() {
const goos = { win32: 'windows', linux: 'linux', darwin: 'darwin' }[process.platform]
const goarch = { x64: 'amd64', arm64: 'arm64' }[process.arch]
return `${goos}_${goarch}`
}
export function buildPlan({ target = hostTarget(), plugin, development = false } = {}) {
if (!targets.includes(target)) throw new Error(`不支持的编译平台:${target}`)
if (plugin && !plugins.some(item => item.id === plugin)) throw new Error(`未知插件:${plugin}`)
if (development && target !== hostTarget()) throw new Error('开发运行目录只能使用本机平台插件')
const [goos, goarch] = target.split('_')
return plugins.filter(item => !plugin || item.id === plugin).map(item => ({
...item,
goos,
goarch,
output: path.join(root, 'out', 'plugins', ...(development ? [] : [target]), `${item.executable}${goos === 'windows' ? '.exe' : ''}`)
}))
}
function run(command, args, cwd, env = process.env) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { cwd, env, stdio: 'inherit', windowsHide: true })
child.once('error', reject)
child.once('exit', code => code === 0 ? resolve() : reject(new Error(`构建失败:${command},退出码 ${code}`)))
})
}
export async function buildPlugins(args) {
const options = {}
let all = false
for (const arg of args) {
if (arg === '--all') all = true
else if (arg === '--dev') options.development = true
else if (arg.startsWith('--target=')) options.target = arg.slice(9)
else if (arg.startsWith('--plugin=')) options.plugin = arg.slice(9)
else throw new Error(`未知构建参数:${arg}`)
}
if (all && (options.target || options.development)) throw new Error('全平台编译不能同时指定单个平台或开发目录')
const plan = (all ? targets : [options.target || hostTarget()]).flatMap(target => buildPlan({ ...options, target }))
if (plan.some(item => item.id === 'telegram_push')) {
const directory = path.join(root, 'plugins', 'pmail_telegram_push')
const vite = path.join(directory, 'node_modules', 'vite', 'bin', 'vite.js')
try { await access(vite) } catch { throw new Error('请先在 plugins/pmail_telegram_push 执行 npm ci 安装依赖') }
// 构建并同步 Telegram 单文件界面,保证 Go 嵌入的是本次资源。
await run(process.execPath, [vite, 'build'], directory)
await run(process.execPath, [path.join(root, 'deploy', 'prepare-frontend.mjs'), 'telegram'], root)
}
for (const item of plan) {
await mkdir(path.dirname(item.output), { recursive: true })
await run('go', ['build', '-ldflags', '-s -w', '-o', item.output, item.source], path.join(root, item.directory), {
...process.env, CGO_ENABLED: '0', GOOS: item.goos, GOARCH: item.goarch
})
console.log(`插件已编译:${path.relative(root, item.output)}`)
}
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
buildPlugins(process.argv.slice(2)).catch(error => {
console.error(error.message)
process.exitCode = 1
})
}
+3 -1
View File
@@ -20,4 +20,6 @@ try {
& go build -o (Join-Path $output 'smail-config.exe') ./cmd/smail-config
if ($LASTEXITCODE -ne 0) { throw '配置工具构建失败' }
} finally { Pop-Location }
Write-Output '构建完成:out/frontend 和 out/backend。正式发行请使用 Makefile 的跨平台构建目标。'
& node (Join-Path $PSScriptRoot 'build-plugins.mjs') --dev
if ($LASTEXITCODE -ne 0) { throw '插件构建失败' }
Write-Output '构建完成:out/frontend、out/backend 和 out/plugins,包含三个内置插件。正式发行请使用 Makefile 的跨平台构建目标。'
+44
View File
@@ -0,0 +1,44 @@
import { access, copyFile, mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { buildPlan, root, targets } from './build-plugins.mjs'
export function packagePlan(target) {
const pluginPlan = buildPlan({ target })
const [goos, goarch] = target.split('_')
const extension = goos === 'windows' ? '.exe' : ''
const suffix = `${goos === 'darwin' ? 'mac' : goos}_${goarch}${extension}`
const directory = path.join(root, 'out', 'package', target)
return [
...['smail', 'smail-admin'].map(name => ({
source: path.join(root, 'out', 'backend', `${name}_${suffix}`),
target: path.join(directory, `${name}${extension}`)
})),
...pluginPlan.map(item => ({ source: item.output, target: path.join(directory, 'plugins', path.basename(item.output)) })),
{ source: path.join(root, 'README.md'), target: path.join(directory, 'README.md') }
]
}
export async function packageRelease(selectedTargets = targets) {
const plan = selectedTargets.flatMap(packagePlan)
// 所有输入齐备后才写发布目录,不把旧的或其他平台插件混入发布包。
for (const item of plan) await access(item.source)
for (const item of plan) {
await mkdir(path.dirname(item.target), { recursive: true })
await copyFile(item.source, item.target)
}
for (const target of selectedTargets) {
await writeFile(path.join(root, 'out', 'package', target, 'release.json'), JSON.stringify({
version: process.env.SMAIL_VERSION || 'dev', gitHash: process.env.GIT_HASH || '',
protocolVersion: 1, target, plugins: buildPlan({ target }).map(item => item.id), createdAt: new Date().toISOString()
}, null, 2) + '\n')
}
console.log(`发布包已生成:${selectedTargets.map(target => `out/package/${target}`).join('、')}`)
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
packageRelease(process.argv.length > 2 ? process.argv.slice(2) : targets).catch(error => {
console.error(`发布打包失败:${error.message}`)
process.exitCode = 1
})
}
+7 -2
View File
@@ -21,8 +21,13 @@ try {
# SMAIL_ROOT 是 config 的父目录,不能指向 config 本身或编译输出目录。
$env:SMAIL_ROOT = $root
$arguments = @('--dev-disable-admin-reset')
# 插件工作目录保持不变;配置、数据库和密钥统一通过 SMAIL_ROOT 定位。
$workingDirectory = Join-Path $root 'backend'
# 插件从 out/plugins 加载;账号、配置和密钥仍通过 SMAIL_ROOT 定位到根目录 config。
foreach ($name in @('spam_block.exe', 'pmail_telegram_push.exe', 'wechat_push.exe')) {
if (-not (Test-Path -LiteralPath (Join-Path $root "out\plugins\$name") -PathType Leaf)) {
throw '内置插件产物不完整,请先运行 deploy/build.ps1'
}
}
$workingDirectory = Join-Path $root 'out'
} else {
if (Get-NetTCPConnection -State Listen -LocalPort 15173 -ErrorAction SilentlyContinue) { throw '15173 端口已占用,请先确认并停止旧前端' }
$binary = (Get-Command node -ErrorAction Stop).Source