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