This commit is contained in:
parent
807d2af2ee
commit
f7f387971f
@ -55,6 +55,7 @@ export async function buildPlugins(args) {
|
|||||||
const vite = path.join(directory, 'node_modules', 'vite', 'bin', 'vite.js')
|
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 安装依赖') }
|
try { await access(vite) } catch { throw new Error('请先在 plugins/pmail_telegram_push 执行 npm ci 安装依赖') }
|
||||||
// 构建并同步 Telegram 单文件界面,保证 Go 嵌入的是本次资源。
|
// 构建并同步 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, [vite, 'build'], directory)
|
||||||
await run(process.execPath, [path.join(root, 'deploy', 'prepare-frontend.mjs'), 'telegram'], root)
|
await run(process.execPath, [path.join(root, 'deploy', 'prepare-frontend.mjs'), 'telegram'], root)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 { fileURLToPath } from 'node:url'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
const root = fileURLToPath(new URL('../', import.meta.url))
|
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 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']))
|
const target = path.join(root, ...(plugin ? ['plugins', 'pmail_telegram_push', 'hook', 'dist'] : ['backend', 'listen', 'http_server', 'dist']))
|
||||||
|
|
||||||
// Go 嵌入文件不能越过模块目录;这里仅同步构建中间文件,不保存运行数据。
|
async function validateDirectory(directory) {
|
||||||
await readFile(path.join(source, 'index.html'))
|
const relative = path.relative(root, path.resolve(directory))
|
||||||
await mkdir(target, { recursive: true })
|
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
|
||||||
for (const entry of await readdir(source)) {
|
throw new Error('编译资源路径越过项目目录,已停止')
|
||||||
await cp(path.join(source, entry), path.join(target, entry), { recursive: true })
|
}
|
||||||
|
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 嵌入目录。')
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
"prebuild": "node ../deploy/prepare-frontend.mjs --prepare",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"postbuild": "node ../deploy/prepare-frontend.mjs",
|
"postbuild": "node ../deploy/prepare-frontend.mjs",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "run-p type-check \"build-only {@}\" --",
|
"build": "run-p type-check \"build-only {@}\" --",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
|
"prebuild-only": "node ../../deploy/prepare-frontend.mjs telegram --prepare",
|
||||||
"build-only": "vite build",
|
"build-only": "vite build",
|
||||||
"postbuild-only": "node ../../deploy/prepare-frontend.mjs telegram",
|
"postbuild-only": "node ../../deploy/prepare-frontend.mjs telegram",
|
||||||
"type-check": "vue-tsc --build",
|
"type-check": "vue-tsc --build",
|
||||||
|
|||||||
@ -23,12 +23,26 @@ test('跨平台构建输出不得越过项目 out 目录', async () => {
|
|||||||
test('前端构建必须自动同步 Go 嵌入资源', async () => {
|
test('前端构建必须自动同步 Go 嵌入资源', async () => {
|
||||||
const frontend = JSON.parse(await read('frontend/package.json'))
|
const frontend = JSON.parse(await read('frontend/package.json'))
|
||||||
const telegram = JSON.parse(await read('plugins/pmail_telegram_push/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(frontend.scripts.postbuild, 'node ../deploy/prepare-frontend.mjs')
|
||||||
assert.equal(telegram.scripts['postbuild-only'], 'node ../../deploy/prepare-frontend.mjs telegram')
|
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('frontend/vite.config.js'), /\.\.\/out\/frontend/)
|
||||||
assert.match(await read('plugins/pmail_telegram_push/vite.config.ts'), /\.\.\/\.\.\/out\/plugins\/telegram-ui/)
|
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 () => {
|
test('前端入口按需加载 Element Plus,并延迟设置页和写信编辑器', async () => {
|
||||||
const main = await read('frontend/src/main.js')
|
const main = await read('frontend/src/main.js')
|
||||||
const mainApp = await read('frontend/src/main-app.js')
|
const mainApp = await read('frontend/src/main-app.js')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user