Repository navigation
Expand file tree
/
Copy pathafter-pack-safe.js
More file actions
227 lines (196 loc) · 7.37 KB
/
Copy pathafter-pack-safe.js
File metadata and controls
227 lines (196 loc) · 7.37 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
exports.default = async function(context) {
const platform = context.electronPlatformName;
console.log(`🧹 Running safe after-pack cleanup for ${platform}...`);
// Determine app path based on platform
let appPath;
if (platform === 'darwin') {
appPath = path.join(
context.appOutDir,
`${context.packager.appInfo.productName}.app`,
'Contents',
'Resources',
'app'
);
} else if (platform === 'win32') {
appPath = path.join(
context.appOutDir,
'resources',
'app'
);
}
const nodeModulesPath = path.join(appPath, 'node_modules');
if (!fs.existsSync(nodeModulesPath)) {
console.log('node_modules not found in app bundle, skipping cleanup');
// Skip macOS code signing for non-darwin platforms
if (platform !== 'darwin') {
console.log('✅ After-pack complete (non-macOS platform)');
return;
}
return;
}
let totalSaved = 0;
// Safe patterns to remove - only documentation and test files
function cleanDirectory(dirPath, moduleName = '', depth = 0) {
if (depth > 3) return; // Limit recursion depth
// Skip critical modules entirely
if (moduleName && (
moduleName.startsWith('electron') ||
moduleName.includes('builder') ||
moduleName === 'app-builder-lib'
)) {
return;
}
try {
const items = fs.readdirSync(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
try {
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
// Only remove these specific directories
if (item === 'test' || item === 'tests' || item === '__tests__' ||
item === 'example' || item === 'examples' ||
item === 'docs' || item === 'documentation' ||
item === '.github' || item === 'coverage') {
const dirSize = getDirSize(itemPath);
fs.rmSync(itemPath, { recursive: true, force: true });
totalSaved += dirSize;
continue;
}
// Recurse into other directories
cleanDirectory(itemPath, moduleName || item, depth + 1);
} else {
// Only remove safe file types
const lowerItem = item.toLowerCase();
if (item.endsWith('.md') && lowerItem !== 'license.md' ||
item.endsWith('.markdown') ||
item.endsWith('.map') ||
lowerItem === 'changelog' ||
lowerItem === 'history' ||
lowerItem === 'readme' ||
lowerItem === 'readme.txt' ||
lowerItem === '.npmignore' ||
lowerItem === '.eslintrc' ||
lowerItem === '.prettierrc') {
const fileSize = stat.size;
fs.unlinkSync(itemPath);
totalSaved += fileSize;
}
}
} catch (e) {
// Ignore individual file errors
}
}
} catch (e) {
// Ignore directory errors
}
}
function getDirSize(dirPath) {
let size = 0;
try {
const items = fs.readdirSync(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
size += getDirSize(itemPath);
} else {
size += stat.size;
}
}
} catch (e) {
// Ignore errors
}
return size;
}
console.log('Cleaning node_modules safely...');
cleanDirectory(nodeModulesPath);
// Remove specific large modules we definitely don't need
const modulesToRemove = [
'@jest',
'jest',
'jsdom',
'@types',
'typescript'
];
for (const module of modulesToRemove) {
const modulePath = path.join(nodeModulesPath, module);
if (fs.existsSync(modulePath)) {
const dirSize = getDirSize(modulePath);
console.log(`Removing ${module}...`);
fs.rmSync(modulePath, { recursive: true, force: true });
totalSaved += dirSize;
}
}
const savedMB = (totalSaved / 1024 / 1024).toFixed(2);
console.log(`✅ Safe after-pack cleanup complete. Saved ${savedMB} MB`);
// macOS-only: Apply hardened runtime to all executables
if (platform !== 'darwin') {
console.log('✅ After-pack complete (skipping macOS code signing)');
return;
}
// Apply hardened runtime to all executables
console.log('🔐 Applying hardened runtime to all executables...');
const appBundlePath = path.join(
context.appOutDir,
`${context.packager.appInfo.productName}.app`
);
const cscName = process.env.CSC_NAME;
const entitlementsPath = 'build/entitlements.mac.plist';
if (!cscName) {
console.log('⚠️ CSC_NAME not found, skipping hardened runtime fix');
return;
}
if (!fs.existsSync(entitlementsPath)) {
console.log('⚠️ Entitlements file not found, skipping hardened runtime fix');
return;
}
try {
// Find and sign all helper apps
const helperApps = execSync(`find "${appBundlePath}" -name "*.app" -type d ! -path "${appBundlePath}"`, { encoding: 'utf8' })
.trim()
.split('\n')
.filter(Boolean);
for (const helperApp of helperApps) {
console.log(` Signing helper: ${path.basename(helperApp)}`);
execSync(`codesign --force --deep --options runtime --entitlements "${entitlementsPath}" --sign "${cscName}" "${helperApp}"`, { stdio: 'inherit' });
}
// Find and sign all frameworks
const frameworks = execSync(`find "${appBundlePath}" -name "*.framework" -type d`, { encoding: 'utf8' })
.trim()
.split('\n')
.filter(Boolean);
for (const framework of frameworks) {
console.log(` Signing framework: ${path.basename(framework)}`);
execSync(`codesign --force --deep --options runtime --entitlements "${entitlementsPath}" --sign "${cscName}" "${framework}"`, { stdio: 'inherit' });
}
// Sign other executables
const executables = execSync(`find "${appBundlePath}" -type f -perm +111 ! -name "*.dylib" ! -name "*.so" | xargs file | grep "Mach-O" | cut -d: -f1`, { encoding: 'utf8' })
.trim()
.split('\n')
.filter(Boolean);
for (const executable of executables) {
console.log(` Signing executable: ${path.basename(executable)}`);
try {
execSync(`codesign --force --options runtime --entitlements "${entitlementsPath}" --sign "${cscName}" "${executable}"`, { stdio: 'inherit' });
} catch (e) {
// Some executables might fail, continue
}
}
// Sign the main app last
console.log(` Signing main app: ${path.basename(appBundlePath)}`);
execSync(`codesign --force --deep --options runtime --entitlements "${entitlementsPath}" --sign "${cscName}" "${appBundlePath}"`, { stdio: 'inherit' });
// Verify hardened runtime
const verifyOutput = execSync(`codesign -dvvv "${appBundlePath}" 2>&1`, { encoding: 'utf8' });
if (verifyOutput.includes('flags=') && verifyOutput.includes('runtime')) {
console.log('✅ Hardened runtime applied successfully');
} else {
console.log('⚠️ Hardened runtime may not be applied correctly');
}
} catch (error) {
console.error('❌ Error applying hardened runtime:', error.message);
}
};