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