1+ #!/usr/bin/env node
2+
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+ const { execSync } = require ( 'child_process' ) ;
6+
7+ function findAppBundles ( dir ) {
8+ const apps = [ ] ;
9+ if ( ! fs . existsSync ( dir ) ) return apps ;
10+
11+ const items = fs . readdirSync ( dir ) ;
12+ for ( const item of items ) {
13+ const itemPath = path . join ( dir , item ) ;
14+ const stat = fs . statSync ( itemPath ) ;
15+
16+ if ( stat . isDirectory ( ) ) {
17+ if ( item . endsWith ( '.app' ) ) {
18+ apps . push ( itemPath ) ;
19+ } else {
20+ apps . push ( ...findAppBundles ( itemPath ) ) ;
21+ }
22+ }
23+ }
24+ return apps ;
25+ }
26+
27+ function resignApps ( ) {
28+ console . log ( '🔍 Finding .app bundles...' ) ;
29+ const apps = findAppBundles ( 'out' ) ;
30+
31+ if ( apps . length === 0 ) {
32+ console . log ( 'ℹ️ No .app bundles found' ) ;
33+ return ;
34+ }
35+
36+ console . log ( `📝 Found ${ apps . length } .app bundles to re-sign:` ) ;
37+ apps . forEach ( app => console . log ( ` - ${ app } ` ) ) ;
38+
39+ for ( const app of apps ) {
40+ try {
41+ console . log ( `✍️ Re-signing: ${ app } ` ) ;
42+ execSync ( `codesign --force --deep --sign "-" "${ app } "` , { stdio : 'inherit' } ) ;
43+ } catch ( error ) {
44+ console . error ( `❌ Failed to sign ${ app } :` , error . message ) ;
45+ }
46+ }
47+ }
48+
49+ function recreateZips ( ) {
50+ console . log ( '🔍 Looking for zip distributions...' ) ;
51+ const makeDir = path . join ( 'out' , 'make' ) ;
52+
53+ if ( ! fs . existsSync ( makeDir ) ) {
54+ console . log ( 'ℹ️ No make directory found' ) ;
55+ return ;
56+ }
57+
58+ // 递归查找所有 .zip 文件
59+ function findZipFiles ( dir ) {
60+ const zipFiles = [ ] ;
61+ const items = fs . readdirSync ( dir ) ;
62+
63+ for ( const item of items ) {
64+ const itemPath = path . join ( dir , item ) ;
65+ const stat = fs . statSync ( itemPath ) ;
66+
67+ if ( stat . isDirectory ( ) ) {
68+ zipFiles . push ( ...findZipFiles ( itemPath ) ) ;
69+ } else if ( item . endsWith ( '.zip' ) ) {
70+ zipFiles . push ( itemPath ) ;
71+ }
72+ }
73+ return zipFiles ;
74+ }
75+
76+ const zipFiles = findZipFiles ( makeDir ) ;
77+
78+ if ( zipFiles . length === 0 ) {
79+ console . log ( 'ℹ️ No zip files found' ) ;
80+ return ;
81+ }
82+
83+ for ( const zipPath of zipFiles ) {
84+ const zipName = path . basename ( zipPath ) ;
85+ const zipDir = path . dirname ( zipPath ) ;
86+ const tempDir = path . join ( zipDir , 'temp_' + Date . now ( ) ) ;
87+
88+ try {
89+ console . log ( `🗜️ Processing zip: ${ zipName } ` ) ;
90+
91+ // 创建临时目录并解压
92+ fs . mkdirSync ( tempDir ) ;
93+ execSync ( `cd "${ tempDir } " && unzip -q "../${ zipName } "` , { stdio : 'pipe' } ) ;
94+
95+ // 查找并重新签名 .app 文件
96+ const apps = findAppBundles ( tempDir ) ;
97+ if ( apps . length > 0 ) {
98+ console . log ( `📝 Found ${ apps . length } .app bundles in zip to re-sign` ) ;
99+ for ( const app of apps ) {
100+ console . log ( `✍️ Re-signing in zip: ${ path . relative ( tempDir , app ) } ` ) ;
101+ execSync ( `codesign --force --deep --sign "-" "${ app } "` , { stdio : 'pipe' } ) ;
102+ }
103+
104+ // 删除旧的zip文件并创建新的
105+ fs . unlinkSync ( zipPath ) ;
106+ console . log ( `📦 Recreating zip: ${ zipName } ` ) ;
107+ execSync ( `cd "${ tempDir } " && zip -r "../${ zipName } " .` , { stdio : 'pipe' } ) ;
108+ console . log ( `✅ Successfully updated: ${ zipPath } ` ) ;
109+ } else {
110+ console . log ( `ℹ️ No .app bundles found in ${ zipName } ` ) ;
111+ }
112+
113+ // 清理临时目录
114+ execSync ( `rm -rf "${ tempDir } "` ) ;
115+
116+ } catch ( error ) {
117+ console . error ( `❌ Failed to process zip ${ zipName } :` , error . message ) ;
118+ // 清理临时目录
119+ if ( fs . existsSync ( tempDir ) ) {
120+ execSync ( `rm -rf "${ tempDir } "` ) ;
121+ }
122+ }
123+ }
124+ }
125+
126+ if ( process . platform === 'darwin' ) {
127+ console . log ( '🍎 Running macOS codesign fix...' ) ;
128+ resignApps ( ) ;
129+ recreateZips ( ) ;
130+ console . log ( '✅ macOS codesign fix completed!' ) ;
131+ } else {
132+ console . log ( 'ℹ️ Skipping codesign fix (not macOS)' ) ;
133+ }
0 commit comments