feat(server): 实现跨平台自动打开浏览器功能

- 移除对 'open' 包的依赖,改用 Node.js 内置模块实现自动打开浏览器
- 新增 openUrl 函数,支持 macOS、Windows 和 Linux 系统
- 更新网络接口遍历逻辑中的变量命名以提高可读性
- 静态资源处理中间件改为使用 Bun.file API 并内嵌到可执行文件中
- 优化控制台输出信息,增强用户体验和提示清晰度
- 调整服务器监听地址为 0.0.0.0,并移除 trust proxy 设置
- 修改获取局域网 IP 的函数名称和注释结构使其更加明确
- 删除 package.json 和 lock 文件中不再使用的依赖项及相关条目
- 更新 GitHub Actions 工作流配置以适配新的编译和打包方式
- 在 CI 流程中启用代码压缩选项以减小最终二进制文件体积
This commit is contained in:
coolxitech
2025-12-13 14:08:12 +08:00
parent 20dc6bd086
commit cd14e88cc0
4 changed files with 62 additions and 156 deletions

View File

@@ -3,7 +3,7 @@ name: 构建多平台可执行程序
on: on:
push: push:
branches: branches:
- main # 监听 main 分支的推送,不再强制要求手动推送 Tag - main
jobs: jobs:
build: build:
@@ -13,17 +13,11 @@ jobs:
matrix: matrix:
include: include:
- os: windows-latest - os: windows-latest
artifact_name: ogame-windows executable: ogame-windows.exe
asset_name: ogame-windows.zip
executable: ogame.exe
- os: ubuntu-latest - os: ubuntu-latest
artifact_name: ogame-linux executable: ogame-linux
asset_name: ogame-linux.tar.gz
executable: ogame
- os: macos-latest - os: macos-latest
artifact_name: ogame-macos executable: ogame-macos
asset_name: ogame-macos.tar.gz
executable: ogame
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -33,39 +27,32 @@ jobs:
with: with:
bun-version: latest bun-version: latest
# --- 关键步骤:读取 package.json 的版本号 --- # 取 package.json 的版本号
- name: Get version from package.json - name: Get version
id: get_version id: get_version
shell: bash shell: bash
run: | run: |
VERSION=$(node -p "require('./package.json').version") VERSION=$(node -p "require('./package.json').version")
echo "VERSION=v$VERSION" >> $GITHUB_OUTPUT echo "VERSION=v$VERSION" >> $GITHUB_OUTPUT
echo "Detected version: v$VERSION"
- name: Install Dependencies - name: Install Dependencies
run: bun install run: bun install
# 构建前端 Vue 项目
- name: Build Vue Frontend - name: Build Vue Frontend
run: bun run build run: bun run build
- name: Compile Executable # 编译单文件二进制程序
run: bun build ./server.js --compile --outfile ${{ matrix.executable }} - name: Compile Single Executable
run: |
- name: Package Assets (Windows) bun build ./server.js --compile --minify --outfile ${{ matrix.executable }}
if: matrix.os == 'windows-latest'
run: Compress-Archive -Path "${{ matrix.executable }}", "docs" -DestinationPath "${{ matrix.asset_name }}"
- name: Package Assets (Linux/macOS)
if: matrix.os != 'windows-latest'
run: tar -czvf ${{ matrix.asset_name }} ${{ matrix.executable }} docs/
- name: Upload Artifact - name: Upload Artifact
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: ${{ matrix.artifact_name }} name: ${{ matrix.executable }}
path: ${{ matrix.asset_name }} path: ${{ matrix.executable }}
# 将版本号传递给 release 任务
outputs: outputs:
app_version: ${{ steps.get_version.outputs.VERSION }} app_version: ${{ steps.get_version.outputs.VERSION }}
@@ -81,16 +68,12 @@ jobs:
- name: Create GitHub Release - name: Create GitHub Release
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
with: with:
# 使用从 build 任务传递过来的版本号
tag_name: ${{ needs.build.outputs.app_version }} tag_name: ${{ needs.build.outputs.app_version }}
name: Release ${{ needs.build.outputs.app_version }} name: Release ${{ needs.build.outputs.app_version }}
draft: false
prerelease: false
# 自动创建 Tag (非常重要)
generate_release_notes: true
files: | files: |
ogame-windows/ogame-windows.zip ogame-windows.exe/ogame-windows.exe
ogame-linux/ogame-linux.tar.gz ogame-linux/ogame-linux
ogame-macos/ogame-macos.tar.gz ogame-macos/ogame-macos
generate_release_notes: true
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -23,7 +23,6 @@
"express": "^5.2.1", "express": "^5.2.1",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"lucide-vue-next": "^0.556.0", "lucide-vue-next": "^0.556.0",
"open": "^11.0.0",
"pinia": "^3.0.4", "pinia": "^3.0.4",
"pinia-plugin-persistedstate": "^4.7.1", "pinia-plugin-persistedstate": "^4.7.1",
"reka-ui": "^2.6.1", "reka-ui": "^2.6.1",

96
pnpm-lock.yaml generated
View File

@@ -38,9 +38,6 @@ importers:
lucide-vue-next: lucide-vue-next:
specifier: ^0.556.0 specifier: ^0.556.0
version: 0.556.0(vue@3.5.25(typescript@5.9.3)) version: 0.556.0(vue@3.5.25(typescript@5.9.3))
open:
specifier: ^11.0.0
version: 11.0.0
pinia: pinia:
specifier: ^3.0.4 specifier: ^3.0.4
version: 3.0.4(typescript@5.9.3)(vue@3.5.25(typescript@5.9.3)) version: 3.0.4(typescript@5.9.3)(vue@3.5.25(typescript@5.9.3))
@@ -513,10 +510,6 @@ packages:
buffer-from@1.1.2: buffer-from@1.1.2:
resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==}
bundle-name@4.1.0:
resolution: {integrity: sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==}
engines: {node: '>=18'}
bytes@3.1.2: bytes@3.1.2:
resolution: {integrity: sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==} resolution: {integrity: sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==}
engines: {node: '>= 0.8'} engines: {node: '>= 0.8'}
@@ -574,18 +567,6 @@ packages:
supports-color: supports-color:
optional: true optional: true
default-browser-id@5.0.1:
resolution: {integrity: sha512-x1VCxdX4t+8wVfd1so/9w+vQ4vx7lKd2Qp5tDRutErwmR85OgmfX7RlLRMWafRMY7hbEiXIbudNrjOAPa/hL8Q==}
engines: {node: '>=18'}
default-browser@5.4.0:
resolution: {integrity: sha512-XDuvSq38Hr1MdN47EDvYtx3U0MTqpCEn+F6ft8z2vYDzMrvQhVp0ui9oQdqW3MvK3vqUETglt1tVGgjLuJ5izg==}
engines: {node: '>=18'}
define-lazy-prop@3.0.0:
resolution: {integrity: sha512-N+MeXYoqr3pOgn8xfyRPREN7gHakLYjhsHhWGT3fWAiL4IkAt0iDw14QiiEm2bE30c5XX5q0FtAA3CK5f9/BUg==}
engines: {node: '>=12'}
defu@6.1.4: defu@6.1.4:
resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==} resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==}
@@ -715,20 +696,6 @@ packages:
resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==} resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==}
engines: {node: '>= 0.10'} engines: {node: '>= 0.10'}
is-docker@3.0.0:
resolution: {integrity: sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
hasBin: true
is-in-ssh@1.0.0:
resolution: {integrity: sha512-jYa6Q9rH90kR1vKB6NM7qqd1mge3Fx4Dhw5TVlK1MUBqhEOuCagrEHMevNuCcbECmXZ0ThXkRm+Ymr51HwEPAw==}
engines: {node: '>=20'}
is-inside-container@1.0.0:
resolution: {integrity: sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA==}
engines: {node: '>=14.16'}
hasBin: true
is-promise@4.0.0: is-promise@4.0.0:
resolution: {integrity: sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==} resolution: {integrity: sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==}
@@ -736,10 +703,6 @@ packages:
resolution: {integrity: sha512-oG7cgbmg5kLYae2N5IVd3jm2s+vldjxJzK1pcu9LfpGuQ93MQSzo0okvRna+7y5ifrD+20FE8FvjusyGaz14fw==} resolution: {integrity: sha512-oG7cgbmg5kLYae2N5IVd3jm2s+vldjxJzK1pcu9LfpGuQ93MQSzo0okvRna+7y5ifrD+20FE8FvjusyGaz14fw==}
engines: {node: '>=18'} engines: {node: '>=18'}
is-wsl@3.1.0:
resolution: {integrity: sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==}
engines: {node: '>=16'}
jiti@2.6.1: jiti@2.6.1:
resolution: {integrity: sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==} resolution: {integrity: sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==}
hasBin: true hasBin: true
@@ -878,10 +841,6 @@ packages:
once@1.4.0: once@1.4.0:
resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==}
open@11.0.0:
resolution: {integrity: sha512-smsWv2LzFjP03xmvFoJ331ss6h+jixfA4UUV/Bsiyuu4YJPfN+FIQGOIiv4w9/+MoHkfkJ22UIaQWRVFRfH6Vw==}
engines: {node: '>=20'}
parseurl@1.3.3: parseurl@1.3.3:
resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==} resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==}
engines: {node: '>= 0.8'} engines: {node: '>= 0.8'}
@@ -929,10 +888,6 @@ packages:
resolution: {integrity: sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==} resolution: {integrity: sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==}
engines: {node: ^10 || ^12 || >=14} engines: {node: ^10 || ^12 || >=14}
powershell-utils@0.1.0:
resolution: {integrity: sha512-dM0jVuXJPsDN6DvRpea484tCUaMiXWjuCn++HGTqUWzGDjv5tZkEZldAJ/UMlqRYGFrD/etByo4/xOuC/snX2A==}
engines: {node: '>=20'}
proxy-addr@2.0.7: proxy-addr@2.0.7:
resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==}
engines: {node: '>= 0.10'} engines: {node: '>= 0.10'}
@@ -1006,10 +961,6 @@ packages:
resolution: {integrity: sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==} resolution: {integrity: sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==}
engines: {node: '>= 18'} engines: {node: '>= 18'}
run-applescript@7.1.0:
resolution: {integrity: sha512-DPe5pVFaAsinSaV6QjQ6gdiedWDcRCbUuiQfQa2wmWV7+xC9bGulGI8+TdRmoFkAPaBXk8CrAbnlY2ISniJ47Q==}
engines: {node: '>=18'}
safer-buffer@2.1.2: safer-buffer@2.1.2:
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
@@ -1162,10 +1113,6 @@ packages:
wrappy@1.0.2: wrappy@1.0.2:
resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
wsl-utils@0.3.0:
resolution: {integrity: sha512-3sFIGLiaDP7rTO4xh3g+b3AzhYDIUGGywE/WsmqzJWDxus5aJXVnPTNC/6L+r2WzrwXqVOdD262OaO+cEyPMSQ==}
engines: {node: '>=20'}
snapshots: snapshots:
'@babel/helper-string-parser@7.27.1': {} '@babel/helper-string-parser@7.27.1': {}
@@ -1580,10 +1527,6 @@ snapshots:
buffer-from@1.1.2: buffer-from@1.1.2:
optional: true optional: true
bundle-name@4.1.0:
dependencies:
run-applescript: 7.1.0
bytes@3.1.2: {} bytes@3.1.2: {}
call-bind-apply-helpers@1.0.2: call-bind-apply-helpers@1.0.2:
@@ -1625,15 +1568,6 @@ snapshots:
dependencies: dependencies:
ms: 2.1.3 ms: 2.1.3
default-browser-id@5.0.1: {}
default-browser@5.4.0:
dependencies:
bundle-name: 4.1.0
default-browser-id: 5.0.1
define-lazy-prop@3.0.0: {}
defu@6.1.4: {} defu@6.1.4: {}
depd@2.0.0: {} depd@2.0.0: {}
@@ -1776,22 +1710,10 @@ snapshots:
ipaddr.js@1.9.1: {} ipaddr.js@1.9.1: {}
is-docker@3.0.0: {}
is-in-ssh@1.0.0: {}
is-inside-container@1.0.0:
dependencies:
is-docker: 3.0.0
is-promise@4.0.0: {} is-promise@4.0.0: {}
is-what@5.5.0: {} is-what@5.5.0: {}
is-wsl@3.1.0:
dependencies:
is-inside-container: 1.0.0
jiti@2.6.1: {} jiti@2.6.1: {}
lightningcss-android-arm64@1.30.2: lightningcss-android-arm64@1.30.2:
@@ -1885,15 +1807,6 @@ snapshots:
dependencies: dependencies:
wrappy: 1.0.2 wrappy: 1.0.2
open@11.0.0:
dependencies:
default-browser: 5.4.0
define-lazy-prop: 3.0.0
is-in-ssh: 1.0.0
is-inside-container: 1.0.0
powershell-utils: 0.1.0
wsl-utils: 0.3.0
parseurl@1.3.3: {} parseurl@1.3.3: {}
path-browserify@1.0.1: {} path-browserify@1.0.1: {}
@@ -1925,8 +1838,6 @@ snapshots:
picocolors: 1.1.1 picocolors: 1.1.1
source-map-js: 1.2.1 source-map-js: 1.2.1
powershell-utils@0.1.0: {}
proxy-addr@2.0.7: proxy-addr@2.0.7:
dependencies: dependencies:
forwarded: 0.2.0 forwarded: 0.2.0
@@ -2009,8 +1920,6 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
run-applescript@7.1.0: {}
safer-buffer@2.1.2: {} safer-buffer@2.1.2: {}
send@1.2.0: send@1.2.0:
@@ -2156,8 +2065,3 @@ snapshots:
typescript: 5.9.3 typescript: 5.9.3
wrappy@1.0.2: {} wrappy@1.0.2: {}
wsl-utils@0.3.0:
dependencies:
is-wsl: 3.1.0
powershell-utils: 0.1.0

View File

@@ -1,20 +1,23 @@
const express = require('express'); const express = require('express');
const path = require('node:path'); const path = require('path');
const os = require('node:os'); const { exec } = require('child_process');
const os = require('os');
const app = express(); const app = express();
const HOST = '0.0.0.0';
app.set('trust proxy', true); // 1. 跨平台自动打开浏览器函数
// 指向 Vue 构建后的 docs 目录 function openUrl(url) {
app.use(express.static(path.join(process.cwd(), 'docs'))); const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open';
exec(`${start} "${url}"`);
}
const getLocalIp = () => { // 2. 获取局域网 IP
function getLocalIp() {
const interfaces = os.networkInterfaces(); const interfaces = os.networkInterfaces();
for (let devName in interfaces) { for (let devName in interfaces) {
let face = interfaces[devName]; let iface = interfaces[devName];
for (let i = 0; i < face.length; i++) { for (let i = 0; i < iface.length; i++) {
let alias = face[i]; let alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address; return alias.address;
} }
@@ -22,23 +25,40 @@ const getLocalIp = () => {
} }
return 'localhost'; return 'localhost';
} }
const server = app.listen(0, HOST, async () => {
const { port } = server.address();
const url = `http://localhost:${port}`; // 3. 核心:静态资源拦截器(实现单文件嵌入的关键)
app.get('*', async (req, res) => {
// 处理请求路径,默认为 index.html
let reqPath = req.path === '/' ? '/index.html' : req.path;
console.log('-----------------------------------'); // 这里的路径必须在构建时能找到对应的 docs 目录
console.log(`🚀 服务器已成功启动!`); // Bun 编译时会自动将 Bun.file 引用的静态资源打包进去
console.log(`🔗 本地地址: ${url}`); const filePath = path.join(__dirname, "docs", reqPath);
console.log(`🌐 默认允许局域网访问http://${getLocalIp()}:${port}`); const file = Bun.file(filePath);
console.log('-----------------------------------');
console.log('提示: 关闭此控制台窗口将停止服务。');
// 3. 自动打开浏览器 if (await file.exists()) {
try { res.type(file.type);
const { default: openUrl } = await import('open'); res.send(Buffer.from(await file.arrayBuffer()));
await openUrl(url); } else {
} catch (err) { // Vue History 模式支持:找不到的文件指向 index.html
console.error('无法自动打开浏览器:', err); const indexFile = Bun.file(path.join(__dirname, "docs", "index.html"));
res.type('text/html');
res.send(Buffer.from(await indexFile.arrayBuffer()));
} }
});
// 4. 启动服务器(随机端口)
const server = app.listen(0, '0.0.0.0', () => {
const { port } = server.address();
const url = `http://localhost:${port}`;
const lanUrl = `http://${getLocalIp()}:${port}`;
console.log("-----------------------------------");
console.log(`🚀 OGame 程序已启动!`);
console.log(`🔗 本地访问: ${url}`);
console.log(`🌐 局域网访问: ${lanUrl}`);
console.log("-----------------------------------");
console.log("提示: 关闭此窗口将停止服务。");
openUrl(url);
}); });