Skip to content

Commit 62a5a28

Browse files
committed
feat: 添加7z打包工具支持,更新配置以使用自定义打包脚本
1 parent dc06e4d commit 62a5a28

3 files changed

Lines changed: 117 additions & 7 deletions

File tree

forge.config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { FusesPlugin } = require('@electron-forge/plugin-fuses');
2+
const { FuseV1Options, FuseVersion } = require('@electron/fuses');
13

24
module.exports = {
35
packagerConfig: {
@@ -13,10 +15,10 @@ module.exports = {
1315
},
1416
rebuildConfig: {},
1517
makers: [
16-
/* {
17-
name: '@electron-forge/maker-zip',
18+
{
19+
name: './scripts/maker-7z.js',
1820
platforms: ['win32'],
19-
}, */
21+
},
2022
],
2123
plugins: [
2224
{
@@ -38,4 +40,4 @@ module.exports = {
3840
},
3941
},
4042
],
41-
};
43+
};

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"scripts": {
77
"start": "electron-forge start",
88
"package": "electron-forge package",
9-
"make": "electron-forge make --arch=ia32 && bun run pack-7z",
10-
"pack-7z": "cd out&&del HotPE_Client.7z&&.\\..\\resources\\tools\\7z\\7z.exe a -t7z -mx=9 HotPE_Client.7z -ir!HotPE_Client HotPE_Client-win32-ia32\\*",
9+
"make": "electron-forge make --arch=ia32",
1110
"publish": "electron-forge publish",
1211
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
1312
},
@@ -49,4 +48,4 @@
4948
"vite": "^4.3.9",
5049
"vite-plugin-react-virtualized": "^1.0.4"
5150
}
52-
}
51+
}

scripts/maker-7z.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { execSync } = require('child_process');
4+
5+
class Maker7z {
6+
constructor(config = {}) {
7+
this.name = '@electron-forge/maker-7z';
8+
this.config = {
9+
sevenZipPath: path.join(process.cwd(), 'resources', 'tools', '7z', '7z.exe'),
10+
...config
11+
};
12+
this.platforms = ['win32'];
13+
}
14+
15+
async make(options) {
16+
const { makeDir, targetPlatform, targetArch, packageJSON } = options;
17+
18+
const outDir = path.dirname(makeDir);
19+
const appName = 'HotPE_Client';
20+
const sevenZipExe = this.config.sevenZipPath;
21+
22+
// 检查7z工具是否存在
23+
if (!fs.existsSync(sevenZipExe)) {
24+
throw new Error(`7z executable not found at path: ${sevenZipExe}`);
25+
}
26+
27+
// 查找源目录
28+
const allDirs = fs.readdirSync(outDir).filter(f =>
29+
fs.statSync(path.join(outDir, f)).isDirectory()
30+
);
31+
32+
// 精简目录查找逻辑
33+
let actualSourceDir = allDirs.find(f =>
34+
f.startsWith(packageJSON.name) && f.includes(targetPlatform)
35+
) || allDirs.find(f => f.includes(targetPlatform));
36+
37+
if (!actualSourceDir) {
38+
actualSourceDir = allDirs.find(f => !f.startsWith('.') && f !== 'make' && f !== appName);
39+
}
40+
41+
if (!actualSourceDir) {
42+
throw new Error(`Source directory not found for ${targetPlatform}-${targetArch}`);
43+
}
44+
45+
try {
46+
// 切换到out目录
47+
const oldCwd = process.cwd();
48+
process.chdir(outDir);
49+
50+
// 重命名文件夹为统一名称
51+
if (actualSourceDir !== appName) {
52+
if (fs.existsSync(appName)) {
53+
fs.rmSync(appName, { recursive: true });
54+
}
55+
fs.renameSync(actualSourceDir, appName);
56+
}
57+
58+
// 检查目录是否存在
59+
if (!fs.existsSync(appName)) {
60+
throw new Error(`${appName} directory not found`);
61+
}
62+
63+
// 执行7z压缩命令
64+
const command = `"${sevenZipExe}" a -t7z -mx=9 "${appName}.7z" -ir!${appName} "${appName}\\*"`;
65+
execSync(command, { stdio: 'inherit' });
66+
67+
// 恢复工作目录
68+
process.chdir(oldCwd);
69+
70+
return [path.join(outDir, `${appName}.7z`)];
71+
} catch (error) {
72+
throw new Error(`7z compression failed: ${error.message}`);
73+
}
74+
}
75+
76+
isSupportedOnCurrentPlatform() {
77+
return process.platform === 'win32';
78+
}
79+
80+
async checkSystemPrerequisites() {
81+
return true;
82+
}
83+
84+
async ensureExternalBinariesExist() {
85+
return true;
86+
}
87+
88+
getDefaultConfig() {
89+
return {};
90+
}
91+
92+
prepareConfig(targetArch) {
93+
return this.config;
94+
}
95+
96+
getRequiredExternalBinaries() {
97+
return [];
98+
}
99+
100+
async resolveForgeConfig(forgeConfig) {
101+
return forgeConfig;
102+
}
103+
104+
getPlatforms() {
105+
return this.platforms;
106+
}
107+
}
108+
109+
module.exports = Maker7z;

0 commit comments

Comments
 (0)