From f7f387971fab1d236695e5cc99e8d9b861b26348 Mon Sep 17 00:00:00 2001 From: smail Date: Sat, 3 Oct 2026 22:58:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=AD=A3=E5=BC=8F=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E7=9A=84=E6=97=A7=E5=89=8D=E7=AB=AF=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E9=9A=94=E7=A6=BB=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/build-plugins.mjs | 1 + deploy/prepare-frontend.mjs | 61 ++++++++++++++++++++---- frontend/package.json | 1 + plugins/pmail_telegram_push/package.json | 1 + test/frontend/output-paths.test.mjs | 14 ++++++ 5 files changed, 70 insertions(+), 8 deletions(-) diff --git a/deploy/build-plugins.mjs b/deploy/build-plugins.mjs index e32c0ba..2a715ea 100644 --- a/deploy/build-plugins.mjs +++ b/deploy/build-plugins.mjs @@ -55,6 +55,7 @@ export async function buildPlugins(args) { 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, [path.join(root, 'deploy', 'prepare-frontend.mjs'), 'telegram', '--prepare'], root) await run(process.execPath, [vite, 'build'], directory) await run(process.execPath, [path.join(root, 'deploy', 'prepare-frontend.mjs'), 'telegram'], root) } diff --git a/deploy/prepare-frontend.mjs b/deploy/prepare-frontend.mjs index 3ad443a..98b6220 100644 --- a/deploy/prepare-frontend.mjs +++ b/deploy/prepare-frontend.mjs @@ -1,16 +1,61 @@ -import { cp, mkdir, readdir, readFile } from 'node:fs/promises' +import { cp, lstat, mkdir, mkdtemp, readdir, readFile, rename } from 'node:fs/promises' import { fileURLToPath } from 'node:url' import path from 'node:path' const root = fileURLToPath(new URL('../', import.meta.url)) -const plugin = process.argv[2] === 'telegram' +const args = process.argv.slice(2) +if (args.some(arg => !['telegram', '--prepare'].includes(arg))) throw new Error('未知前端构建参数') +const plugin = args.includes('telegram') const source = path.join(root, 'out', ...(plugin ? ['plugins', 'telegram-ui'] : ['frontend'])) const target = path.join(root, ...(plugin ? ['plugins', 'pmail_telegram_push', 'hook', 'dist'] : ['backend', 'listen', 'http_server', 'dist'])) -// Go 嵌入文件不能越过模块目录;这里仅同步构建中间文件,不保存运行数据。 -await readFile(path.join(source, 'index.html')) -await mkdir(target, { recursive: true }) -for (const entry of await readdir(source)) { - await cp(path.join(source, entry), path.join(target, entry), { recursive: true }) +async function validateDirectory(directory) { + const relative = path.relative(root, path.resolve(directory)) + if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) { + throw new Error('编译资源路径越过项目目录,已停止') + } + let current = root + for (const part of relative.split(path.sep)) { + current = path.join(current, part) + let info + try { info = await lstat(current) } catch (error) { + if (error.code === 'ENOENT') return false + throw error + } + if (!info.isDirectory() || info.isSymbolicLink()) throw new Error('编译资源目录无效或为链接,已停止') + } + return true +} + +async function archiveDirectories(directories) { + const existing = [] + for (const directory of directories) { + if (await validateDirectory(directory)) existing.push(directory) + } + if (!existing.length) return + const historyRoot = path.join(root, 'out', 'build-history') + await validateDirectory(historyRoot) + await mkdir(historyRoot, { recursive: true }) + const history = await mkdtemp(path.join(historyRoot, plugin ? 'telegram-ui-' : 'frontend-')) + for (const [index, directory] of existing.entries()) { + const destination = path.join(history, `${index}-${path.basename(directory)}`) + await validateDirectory(destination) + await rename(directory, destination) + } +} + +if (args.includes('--prepare')) { + // 只移动项目内的生成目录,不删除文件,也不让旧散列资源进入新产物。 + await archiveDirectories([source, target]) + console.log('旧前端编译资源已隔离,本次将从空目录构建。') +} else { + // Go 嵌入文件不能越过模块目录;这里仅同步构建中间文件,不保存运行数据。 + if (!await validateDirectory(source)) throw new Error('前端构建目录不存在') + await readFile(path.join(source, 'index.html')) + await archiveDirectories([target]) + await mkdir(target, { recursive: true }) + for (const entry of await readdir(source)) { + await cp(path.join(source, entry), path.join(target, entry), { recursive: true }) + } + console.log('前端产物已生成至 out,并同步 Go 嵌入目录。') } -console.log('前端产物已生成至 out,并同步 Go 嵌入目录。') diff --git a/frontend/package.json b/frontend/package.json index 44bfac1..bfe1869 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -4,6 +4,7 @@ "private": true, "scripts": { "dev": "vite", + "prebuild": "node ../deploy/prepare-frontend.mjs --prepare", "build": "vite build", "postbuild": "node ../deploy/prepare-frontend.mjs", "preview": "vite preview", diff --git a/plugins/pmail_telegram_push/package.json b/plugins/pmail_telegram_push/package.json index 8f49510..0c3e100 100644 --- a/plugins/pmail_telegram_push/package.json +++ b/plugins/pmail_telegram_push/package.json @@ -10,6 +10,7 @@ "dev": "vite", "build": "run-p type-check \"build-only {@}\" --", "preview": "vite preview", + "prebuild-only": "node ../../deploy/prepare-frontend.mjs telegram --prepare", "build-only": "vite build", "postbuild-only": "node ../../deploy/prepare-frontend.mjs telegram", "type-check": "vue-tsc --build", diff --git a/test/frontend/output-paths.test.mjs b/test/frontend/output-paths.test.mjs index 9f0ae9f..2aba677 100644 --- a/test/frontend/output-paths.test.mjs +++ b/test/frontend/output-paths.test.mjs @@ -23,12 +23,26 @@ test('跨平台构建输出不得越过项目 out 目录', async () => { test('前端构建必须自动同步 Go 嵌入资源', async () => { const frontend = JSON.parse(await read('frontend/package.json')) const telegram = JSON.parse(await read('plugins/pmail_telegram_push/package.json')) + assert.equal(frontend.scripts.prebuild, 'node ../deploy/prepare-frontend.mjs --prepare') + assert.equal(telegram.scripts['prebuild-only'], 'node ../../deploy/prepare-frontend.mjs telegram --prepare') assert.equal(frontend.scripts.postbuild, 'node ../deploy/prepare-frontend.mjs') assert.equal(telegram.scripts['postbuild-only'], 'node ../../deploy/prepare-frontend.mjs telegram') assert.match(await read('frontend/vite.config.js'), /\.\.\/out\/frontend/) assert.match(await read('plugins/pmail_telegram_push/vite.config.ts'), /\.\.\/\.\.\/out\/plugins\/telegram-ui/) }) +test('正式构建先隔离旧资源,复制时不合并旧嵌入目录', async () => { + const prepare = await read('deploy/prepare-frontend.mjs') + assert.match(prepare, /archiveDirectories\(\[source, target\]\)/) + assert.match(prepare, /archiveDirectories\(\[target\]\)/) + assert.match(prepare, /info\.isSymbolicLink\(\)/) + assert.match(prepare, /relative\.startsWith\('\.\.'\)/) + assert.doesNotMatch(prepare, /\b(?:rm|unlink|rmdir)\s*\(/) + const build = await read('deploy/build-plugins.mjs') + const isolate = build.indexOf("'telegram', '--prepare'") + assert.ok(isolate >= 0 && isolate < build.indexOf("[vite, 'build']")) +}) + test('前端入口按需加载 Element Plus,并延迟设置页和写信编辑器', async () => { const main = await read('frontend/src/main.js') const mainApp = await read('frontend/src/main-app.js')