Skip to content

Commit ba84342

Browse files
author
Taois
committed
feat:增加打包成一个二进制文件
1 parent 3c57593 commit ba84342

18 files changed

+1375
-797
lines changed

dashboard/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
/dist-binary/

dashboard/build-binary.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env node
2+
3+
import { execSync } from 'child_process';
4+
import { existsSync, rmSync, cpSync, mkdirSync, readdirSync, statSync } from 'fs';
5+
import { join, dirname } from 'path';
6+
import { fileURLToPath } from 'url';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
11+
// 定义路径
12+
const rootDir = __dirname;
13+
const tempServerDir = join(rootDir, 'temp-server');
14+
const distBinaryDir = join(rootDir, 'dist-binary');
15+
const distDir = join(rootDir, 'dist');
16+
const tempServerDistDir = join(tempServerDir, 'dist');
17+
18+
console.log('🚀 开始自动化打包流程...\n');
19+
20+
try {
21+
// 步骤 1: 前端构建
22+
console.log('📦 步骤 1: 构建前端项目...');
23+
execSync('pnpm build:apps', {
24+
stdio: 'inherit',
25+
cwd: rootDir
26+
});
27+
console.log('✅ 前端构建完成\n');
28+
29+
// 步骤 2: 清理 temp-server 中的旧文件
30+
console.log('🧹 步骤 2: 清理 temp-server 中的旧文件...');
31+
32+
// 清理 temp-server 中的旧 apps 目录
33+
const tempServerAppsDir = join(tempServerDir, 'apps');
34+
if (existsSync(tempServerAppsDir)) {
35+
rmSync(tempServerAppsDir, { recursive: true, force: true });
36+
console.log('✅ 已删除旧的 apps 目录');
37+
}
38+
console.log('');
39+
40+
// 步骤 3: 复制构建文件到 temp-server/apps/drplayer
41+
console.log('📁 步骤 3: 复制构建文件到 temp-server...');
42+
if (!existsSync(distDir)) {
43+
throw new Error('dist 目录不存在,请确保前端构建成功');
44+
}
45+
46+
// 确保 temp-server 目录存在
47+
if (!existsSync(tempServerDir)) {
48+
mkdirSync(tempServerDir, { recursive: true });
49+
}
50+
51+
// 创建 apps/drplayer 目录并复制 dist 内容
52+
const tempServerDrplayerDir = join(tempServerAppsDir, 'drplayer');
53+
if (!existsSync(tempServerAppsDir)) {
54+
mkdirSync(tempServerAppsDir, { recursive: true });
55+
}
56+
57+
// 将 dist 目录的内容复制到 apps/drplayer
58+
cpSync(distDir, tempServerDrplayerDir, { recursive: true });
59+
console.log('✅ 已将 dist 内容复制到 apps/drplayer');
60+
console.log('');
61+
62+
// 步骤 4: 在 temp-server 目录中打包二进制文件
63+
console.log('⚙️ 步骤 4: 打包二进制文件...');
64+
65+
// 确保 temp-server 有 node_modules
66+
const tempServerNodeModules = join(tempServerDir, 'node_modules');
67+
if (!existsSync(tempServerNodeModules)) {
68+
console.log('📦 安装 temp-server 依赖...');
69+
execSync('pnpm install', {
70+
stdio: 'inherit',
71+
cwd: tempServerDir
72+
});
73+
}
74+
75+
// 执行 pkg 打包
76+
execSync('pnpm pkg:win', {
77+
stdio: 'inherit',
78+
cwd: tempServerDir
79+
});
80+
console.log('✅ 二进制文件打包完成\n');
81+
82+
// 步骤 5: 移动二进制文件到 dist-binary 目录
83+
console.log('📦 步骤 5: 移动二进制文件到 dist-binary...');
84+
85+
// 确保 dist-binary 目录存在
86+
if (!existsSync(distBinaryDir)) {
87+
mkdirSync(distBinaryDir, { recursive: true });
88+
}
89+
90+
// 查找并移动二进制文件
91+
const tempDistBinaryDir = join(tempServerDir, 'dist-binary');
92+
if (existsSync(tempDistBinaryDir)) {
93+
const files = readdirSync(tempDistBinaryDir);
94+
for (const file of files) {
95+
const srcPath = join(tempDistBinaryDir, file);
96+
const destPath = join(distBinaryDir, file);
97+
98+
// 如果目标文件已存在,先删除
99+
if (existsSync(destPath)) {
100+
rmSync(destPath, { force: true });
101+
}
102+
103+
// 移动文件
104+
cpSync(srcPath, destPath, { recursive: true });
105+
console.log(`✅ 已移动: ${file}`);
106+
}
107+
108+
// 清理 temp-server 中的 dist-binary 目录
109+
try {
110+
rmSync(tempDistBinaryDir, { recursive: true, force: true });
111+
} catch (error) {
112+
console.log(`⚠️ 无法删除临时目录 (可能有进程正在使用): ${error.message}`);
113+
}
114+
}
115+
116+
// 步骤 6: 清理 temp-server 目录
117+
console.log('\n🧹 步骤 6: 清理 temp-server 临时文件...');
118+
119+
// 需要清理的目录列表
120+
const dirsToClean = [
121+
join(tempServerDir, 'apps'),
122+
join(tempServerDir, 'dist-binary')
123+
];
124+
125+
for (const dirPath of dirsToClean) {
126+
if (existsSync(dirPath)) {
127+
try {
128+
rmSync(dirPath, { recursive: true, force: true });
129+
console.log(`✅ 已清理: ${dirPath.replace(tempServerDir, 'temp-server')}`);
130+
} catch (error) {
131+
console.log(`⚠️ 无法清理目录 ${dirPath.replace(tempServerDir, 'temp-server')}: ${error.message}`);
132+
}
133+
}
134+
}
135+
136+
console.log('\n🎉 自动化打包流程完成!');
137+
console.log(`📁 二进制文件位置: ${distBinaryDir}`);
138+
139+
// 显示生成的文件
140+
if (existsSync(distBinaryDir)) {
141+
const files = readdirSync(distBinaryDir);
142+
if (files.length > 0) {
143+
console.log('\n📋 生成的文件:');
144+
files.forEach(file => {
145+
const filePath = join(distBinaryDir, file);
146+
const stats = statSync(filePath);
147+
const size = (stats.size / 1024 / 1024).toFixed(2);
148+
console.log(` - ${file} (${size} MB)`);
149+
});
150+
}
151+
}
152+
153+
} catch (error) {
154+
console.error('\n❌ 打包过程中出现错误:');
155+
console.error(error.message);
156+
process.exit(1);
157+
}

dashboard/build-for-fastify.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)