forked from Haleclipse/CodexDesktop-Rebuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.config.js
More file actions
606 lines (559 loc) · 21.5 KB
/
forge.config.js
File metadata and controls
606 lines (559 loc) · 21.5 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
const { FuseV1Options, FuseVersion } = require("@electron/fuses");
const path = require("path");
const fs = require("fs");
// 平台架构 -> Codex vendor target triple 映射
const TARGET_TRIPLE_MAP = {
"darwin-arm64": "aarch64-apple-darwin",
"darwin-x64": "x86_64-apple-darwin",
"linux-arm64": "aarch64-unknown-linux-musl",
"linux-x64": "x86_64-unknown-linux-musl",
"win32-x64": "x86_64-pc-windows-msvc",
};
const OPENAI_PLATFORM_PACKAGE_MAP = {
"darwin-arm64": ["@openai", "codex-darwin-arm64"],
"darwin-x64": ["@openai", "codex-darwin-x64"],
"linux-arm64": ["@openai", "codex-linux-arm64"],
"linux-x64": ["@openai", "codex-linux-x64"],
"win32-x64": ["@openai", "codex-win32-x64"],
};
function getVendorRoots(platform, arch) {
const platformArch = `${platform}-${arch}`;
const roots = [];
const openaiPackagePath = OPENAI_PLATFORM_PACKAGE_MAP[platformArch];
if (openaiPackagePath) {
roots.push(
path.join(__dirname, "node_modules", "@openai", "codex", "node_modules", ...openaiPackagePath, "vendor"),
path.join(__dirname, "node_modules", ...openaiPackagePath, "vendor"),
);
}
// 兼容旧的 @cometix/codex vendor 布局。
roots.push(path.join(__dirname, "node_modules", "@cometix", "codex", "vendor"));
return roots;
}
// 获取 vendor 目录下的二进制路径
function getVendorBinaryPath(platform, arch, subdir, binaryName) {
const platformArch = `${platform}-${arch}`;
const targetTriple = TARGET_TRIPLE_MAP[platformArch];
if (!targetTriple) return null;
for (const vendorRoot of getVendorRoots(platform, arch)) {
const vendorPath = path.join(vendorRoot, targetTriple, subdir, binaryName);
if (fs.existsSync(vendorPath)) {
return vendorPath;
}
}
return null;
}
// 从 npm vendor 复制二进制到 resources/bin/(确保本地始终为最新)
function syncVendorToLocal(platform, arch) {
const platformArch = `${platform}-${arch}`;
const binaryName = platform === "win32" ? "codex.exe" : "codex";
const vendorPath = getVendorBinaryPath(platform, arch, "codex", binaryName);
if (!vendorPath) return;
const localDir = path.join(__dirname, "resources", "bin", platformArch);
fs.mkdirSync(localDir, { recursive: true });
fs.copyFileSync(vendorPath, path.join(localDir, binaryName));
fs.chmodSync(path.join(localDir, binaryName), 0o755);
console.log(`🔄 Synced codex binary: vendor → resources/bin/${platformArch}/${binaryName}`);
}
// 获取 codex 二进制路径(resources/bin 为主,npm vendor 为回退)
function getCodexBinaryPath(platform, arch) {
const platformArch = `${platform}-${arch}`;
const binaryName = platform === "win32" ? "codex.exe" : "codex";
// 先从 npm vendor 同步到 resources/bin/
syncVendorToLocal(platform, arch);
// 路径1: 本地 resources/bin/
const localPath = path.join(__dirname, "resources", "bin", platformArch, binaryName);
if (fs.existsSync(localPath)) {
return localPath;
}
// 路径2: npm vendor(@openai/codex 为主,@cometix/codex 为回退)
return getVendorBinaryPath(platform, arch, "codex", binaryName);
}
// 获取 rg (ripgrep) 二进制路径
function getRgBinaryPath(platform, arch) {
const binaryName = platform === "win32" ? "rg.exe" : "rg";
return getVendorBinaryPath(platform, arch, "path", binaryName);
}
module.exports = {
packagerConfig: {
name: "Codex",
executableName: "Codex",
appBundleId: "com.openai.codex",
icon: "./resources/electron",
asar: {
unpack: "{**/*.node,**/node-pty/build/Release/spawn-helper,**/node-pty/prebuilds/*/spawn-helper}",
},
extraResource: ["./resources/notification.wav"],
// 第一层:文件白名单 — 只放行运行时必要的文件,排除 Codex.app/、.github/ 等膨胀源
ignore: (filePath) => {
// 根目录本身必须放行
if (filePath === "") return false;
// 白名单前缀:运行时需要的顶层路径(对标官方 asar 结构)
// ignore 函数会收到目录和文件两种路径,需要同时匹配完整路径和中间目录
const allowedPrefixes = [
"/src/.vite/build", // 编译后的主进程代码
"/src/webview", // 前端 UI 资源
"/src/skills", // 技能目录
"/node_modules", // 本项目自身的原生依赖(afterPrune 阶段裁剪至仅保留原生模块)
];
// 精确匹配 package.json
if (filePath === "/package.json") return false;
// 检查:filePath 是否是某个白名单路径的前缀(即父目录),
// 或者 filePath 是否在某个白名单路径之下(即子文件/子目录)
for (const prefix of allowedPrefixes) {
if (prefix.startsWith(filePath) || filePath.startsWith(prefix)) {
return false;
}
}
return true;
},
// macOS 签名配置
osxSign: process.env.SKIP_SIGN
? undefined
: {
identity: process.env.APPLE_IDENTITY,
identityValidation: false,
},
osxNotarize: process.env.SKIP_NOTARIZE
? undefined
: {
appleId: process.env.APPLE_ID,
appleIdPassword: process.env.APPLE_PASSWORD,
teamId: process.env.APPLE_TEAM_ID,
},
// Windows 元数据
win32metadata: {
CompanyName: "OpenAI",
ProductName: "Codex",
},
},
rebuildConfig: {},
makers: [
// macOS DMG
{
name: "@electron-forge/maker-dmg",
config: {
format: "ULFO",
icon: "./resources/electron.icns",
},
},
// macOS ZIP
{
name: "@electron-forge/maker-zip",
platforms: ["darwin"],
},
// Windows Squirrel
{
name: "@electron-forge/maker-squirrel",
config: {
name: "Codex",
authors: "OpenAI, Cometix Space",
description: "Codex Desktop App",
setupIcon: "./resources/electron.ico",
iconUrl: "https://raw.githubusercontent.com/Haleclipse/CodexDesktop-Rebuild/master/resources/electron.ico",
},
},
// Windows ZIP
{
name: "@electron-forge/maker-zip",
platforms: ["win32"],
},
// Linux DEB
{
name: "@electron-forge/maker-deb",
config: {
options: {
name: "codex",
productName: "Codex",
genericName: "AI Coding Assistant",
categories: ["Development", "Utility"],
bin: "Codex",
maintainer: "Cometix Space",
homepage: "https://github.com/Haleclipse/CodexDesktop-Rebuild",
icon: "./resources/electron.png",
},
},
},
// Linux RPM
{
name: "@electron-forge/maker-rpm",
config: {
options: {
name: "codex",
productName: "Codex",
genericName: "AI Coding Assistant",
categories: ["Development", "Utility"],
bin: "Codex",
license: "Apache-2.0",
homepage: "https://github.com/Haleclipse/CodexDesktop-Rebuild",
icon: "./resources/electron.png",
},
},
},
// Linux ZIP
{
name: "@electron-forge/maker-zip",
platforms: ["linux"],
},
],
plugins: [
{
name: "@electron-forge/plugin-auto-unpack-natives",
config: {},
},
{
name: "@electron-forge/plugin-fuses",
config: {
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: true,
[FuseV1Options.EnableCookieEncryption]: false,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: true,
[FuseV1Options.EnableNodeCliInspectArguments]: true,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: false,
[FuseV1Options.OnlyLoadAppFromAsar]: false,
},
},
],
hooks: {
// 第二层:原生模块平台筛选 — Forge 裁剪 devDependencies 后,进一步清理非目标平台产物
packageAfterPrune: async (
config,
buildPath,
electronVersion,
platform,
arch,
) => {
const platformArch = `${platform}-${arch}`;
console.log(
`\n🧹 Pruning non-target platform files for ${platformArch}...`,
);
// --- 辅助函数 ---
const removeDirRecursive = (dirPath) => {
if (fs.existsSync(dirPath)) {
fs.rmSync(dirPath, { recursive: true, force: true });
console.log(` 🗑️ Removed: ${path.relative(buildPath, dirPath)}`);
}
};
const removeFile = (filePath) => {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(
` 🗑️ Removed: ${path.relative(buildPath, filePath)}`,
);
}
};
// 递归遍历目录收集文件
const walkDir = (dir, callback) => {
if (!fs.existsSync(dir)) return;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
walkDir(fullPath, callback);
} else {
callback(fullPath);
}
}
};
const nodeModulesPath = path.join(buildPath, "node_modules");
// 0. 原生模块白名单 — Vite 已将所有纯 JS 依赖 bundle 进 main.js,
// node_modules 只需保留不能 bundle 的原生模块及其运行时 binding 辅助包
// 对标官方构建的 7 个包:better-sqlite3, bindings, file-uri-to-path,
// node-addon-api, node-gyp-build, node-pty, electron-liquid-glass
const allowedModules = new Set([
"better-sqlite3", // SQLite 原生模块
"bindings", // 原生模块 .node 文件定位器(better-sqlite3 运行时 require)
"file-uri-to-path", // bindings 的运行时依赖
"node-addon-api", // N-API 辅助(better-sqlite3 运行时需要)
"node-pty", // 终端模拟原生模块
]);
// 平台条件依赖
if (platform === "darwin") {
allowedModules.add("electron-liquid-glass"); // macOS 液态玻璃效果
allowedModules.add("node-gyp-build"); // electron-liquid-glass 运行时 require
}
console.log(
` 📋 Native module whitelist: ${allowedModules.size} packages`,
);
// 删除不在白名单中的所有 node_modules 包
if (fs.existsSync(nodeModulesPath)) {
let removedPkgCount = 0;
const entries = fs.readdirSync(nodeModulesPath);
for (const entry of entries) {
// 跳过隐藏文件 (.bin, .package-lock.json)
if (entry.startsWith(".")) continue;
if (entry.startsWith("@")) {
// scoped 包:逐个检查子目录
const scopePath = path.join(nodeModulesPath, entry);
if (!fs.statSync(scopePath).isDirectory()) continue;
const scopedEntries = fs.readdirSync(scopePath);
for (const scopedEntry of scopedEntries) {
const fullName = `${entry}/${scopedEntry}`;
if (!allowedModules.has(fullName)) {
removeDirRecursive(path.join(scopePath, scopedEntry));
removedPkgCount++;
}
}
// scope 目录为空则删除
if (fs.readdirSync(scopePath).length === 0) {
removeDirRecursive(scopePath);
}
} else {
if (!allowedModules.has(entry)) {
removeDirRecursive(path.join(nodeModulesPath, entry));
removedPkgCount++;
}
}
}
console.log(
` 🗑️ Removed ${removedPkgCount} non-native packages from node_modules`,
);
}
// 清理 .bin 目录(不需要 bin link)
const binDir = path.join(nodeModulesPath, ".bin");
if (fs.existsSync(binDir)) {
removeDirRecursive(binDir);
}
// 1. 清理 node-pty prebuilds 中非目标平台的目录
const nodePtyPrebuilds = path.join(
nodeModulesPath,
"node-pty",
"prebuilds",
);
if (fs.existsSync(nodePtyPrebuilds)) {
const dirs = fs.readdirSync(nodePtyPrebuilds);
for (const dir of dirs) {
if (dir !== platformArch) {
removeDirRecursive(path.join(nodePtyPrebuilds, dir));
}
}
}
// 2. 删除所有 .pdb 调试符号文件(Windows 调试用,运行时不需要)
walkDir(nodeModulesPath, (filePath) => {
if (filePath.endsWith(".pdb")) {
removeFile(filePath);
}
});
// 3. 清理 electron-liquid-glass 中非目标平台的 prebuilds
const liquidGlassPrebuilds = path.join(
nodeModulesPath,
"electron-liquid-glass",
"prebuilds",
);
if (fs.existsSync(liquidGlassPrebuilds)) {
const dirs = fs.readdirSync(liquidGlassPrebuilds);
for (const dir of dirs) {
if (dir !== platformArch) {
removeDirRecursive(path.join(liquidGlassPrebuilds, dir));
}
}
}
// 4. 深度清理 better-sqlite3 — 只保留 build/Release/*.node、lib/、package.json、binding.gyp
const betterSqlitePath = path.join(nodeModulesPath, "better-sqlite3");
if (fs.existsSync(betterSqlitePath)) {
// 删除编译源码和 SQLite 源码
removeDirRecursive(path.join(betterSqlitePath, "deps"));
removeDirRecursive(path.join(betterSqlitePath, "src"));
// 清理 build/ 中除 Release/*.node 以外的所有文件
const bsBuild = path.join(betterSqlitePath, "build");
if (fs.existsSync(bsBuild)) {
const bsEntries = fs.readdirSync(bsBuild);
for (const entry of bsEntries) {
if (entry !== "Release") {
const entryPath = path.join(bsBuild, entry);
if (fs.statSync(entryPath).isDirectory()) {
removeDirRecursive(entryPath);
} else {
removeFile(entryPath);
}
}
}
// Release 中只保留 .node 文件
const bsRelease = path.join(bsBuild, "Release");
if (fs.existsSync(bsRelease)) {
walkDir(bsRelease, (fp) => {
if (!fp.endsWith(".node")) removeFile(fp);
});
}
}
}
// 5. 深度清理 node-pty — 按目标平台差分清理
const nodePtyPath = path.join(nodeModulesPath, "node-pty");
if (fs.existsSync(nodePtyPath)) {
// 删除编译源码、winpty deps、scripts、typings、测试文件
removeDirRecursive(path.join(nodePtyPath, "src"));
removeDirRecursive(path.join(nodePtyPath, "deps"));
removeDirRecursive(path.join(nodePtyPath, "scripts"));
removeDirRecursive(path.join(nodePtyPath, "typings"));
// third_party/conpty/ — Windows 运行时需要,其他平台全部删除
const thirdPartyPath = path.join(nodePtyPath, "third_party");
if (platform === "win32") {
// Windows:只保留目标架构的 conpty 二进制
const conptyBase = path.join(
thirdPartyPath,
"conpty",
);
if (fs.existsSync(conptyBase)) {
// 遍历版本目录(如 1.23.251008001/)
for (const ver of fs.readdirSync(conptyBase)) {
const verPath = path.join(conptyBase, ver);
if (!fs.statSync(verPath).isDirectory()) continue;
for (const platDir of fs.readdirSync(verPath)) {
// 目录格式: win10-x64, win10-arm64
if (!platDir.includes(arch)) {
removeDirRecursive(path.join(verPath, platDir));
}
}
}
}
} else {
// 非 Windows:conpty 完全不需要
removeDirRecursive(thirdPartyPath);
}
// bin/{platform}-{arch}-{abi}/ — 只保留目标平台的 prebuild
const binPath = path.join(nodePtyPath, "bin");
if (fs.existsSync(binPath)) {
for (const dir of fs.readdirSync(binPath)) {
if (!dir.startsWith(`${platform}-${arch}-`)) {
removeDirRecursive(path.join(binPath, dir));
}
}
}
// 清理 build/ 中除 Release/{pty.node, spawn-helper} 以外的所有内容
const nptBuild = path.join(nodePtyPath, "build");
if (fs.existsSync(nptBuild)) {
const nptEntries = fs.readdirSync(nptBuild);
for (const entry of nptEntries) {
if (entry !== "Release") {
const entryPath = path.join(nptBuild, entry);
if (fs.statSync(entryPath).isDirectory()) {
removeDirRecursive(entryPath);
} else {
removeFile(entryPath);
}
}
}
// Release 中只保留 pty.node 和 spawn-helper
const nptRelease = path.join(nptBuild, "Release");
if (fs.existsSync(nptRelease)) {
const releaseEntries = fs.readdirSync(nptRelease, {
withFileTypes: true,
});
for (const entry of releaseEntries) {
const fullPath = path.join(nptRelease, entry.name);
if (
entry.name !== "pty.node" &&
entry.name !== "spawn-helper"
) {
if (entry.isDirectory()) {
removeDirRecursive(fullPath);
} else {
removeFile(fullPath);
}
}
}
}
}
// 删除 node_modules/node-pty/node_modules(嵌套的 node-addon-api 构建产物)
removeDirRecursive(path.join(nodePtyPath, "node_modules"));
// 删除测试文件
walkDir(path.join(nodePtyPath, "lib"), (fp) => {
if (fp.endsWith(".test.js")) removeFile(fp);
});
}
// 6. 清理所有 node_modules 下的非运行时文件
const junkPatterns = [
/\.md$/i,
/LICENSE(\..*)?$/i,
/LICENCE(\..*)?$/i,
/CHANGELOG(\..*)?$/i,
/HISTORY(\..*)?$/i,
/\.npmignore$/,
/\.travis\.yml$/,
/\.eslintrc(\..*)?$/,
/\.prettierrc(\..*)?$/,
/\.editorconfig$/,
/\.jshintrc$/,
/tsconfig\.json$/,
/\.github$/,
/\.gitattributes$/,
/Makefile$/,
/Gruntfile\.js$/,
/Gulpfile\.js$/,
/\.DS_Store$/,
/\.map$/,
/\.ts$/, // TypeScript 源文件(保留 .d.ts)
/\.cc$/, // C++ 源文件
/\.cpp$/,
/\.hpp$/,
/\.h$/, // C/C++ 头文件
/\.c$/, // C 源文件
/\.o$/, // 编译中间产物
/\.gyp$/, // gyp 构建文件
/\.gypi$/,
/\.mk$/, // Makefile 片段
/\.stamp$/, // 构建 stamp
/\.d$/, // 依赖跟踪文件
];
let cleanedCount = 0;
walkDir(nodeModulesPath, (filePath) => {
const basename = path.basename(filePath);
// 保留 .d.ts 和 .node 文件
if (basename.endsWith(".d.ts") || basename.endsWith(".node")) return;
if (junkPatterns.some((p) => p.test(basename))) {
fs.unlinkSync(filePath);
cleanedCount++;
}
});
console.log(
` ✅ Cleaned ${cleanedCount} non-runtime files from node_modules`,
);
},
// 打包后复制对应平台的 codex + rg 二进制
packageAfterCopy: async (config, buildPath, electronVersion, platform, arch) => {
console.log(`\n📦 Packaging for ${platform}-${arch}...`);
console.log(` buildPath: ${buildPath}`);
// buildPath 指向 app 目录,其父目录即为 Resources (macOS) 或 resources (其他)
const resourcesPath = path.dirname(buildPath);
// --- 复制 codex 二进制 ---
const codexBinaryName = platform === "win32" ? "codex.exe" : "codex";
const codexSrc = getCodexBinaryPath(platform, arch);
const codexDest = path.join(resourcesPath, codexBinaryName);
if (codexSrc && fs.existsSync(codexSrc)) {
fs.copyFileSync(codexSrc, codexDest);
fs.chmodSync(codexDest, 0o755);
console.log(`✅ Copied codex binary: ${codexSrc} -> ${codexDest}`);
} else {
console.error(`❌ Codex binary not found for ${platform}-${arch}`);
console.error(` Tried: resources/bin/${platform}-${arch}/${codexBinaryName}`);
console.error(` Tried: node_modules/@openai/codex/.../vendor/.../codex/${codexBinaryName}`);
console.error(` Tried: node_modules/@cometix/codex/vendor/.../codex/${codexBinaryName}`);
process.exit(1);
}
// --- 复制 Windows 附属二进制(sandbox-setup, command-runner)---
if (platform === "win32") {
const winExtras = ["codex-command-runner.exe", "codex-windows-sandbox-setup.exe"];
for (const extra of winExtras) {
const extraSrc = getVendorBinaryPath(platform, arch, "codex", extra);
if (extraSrc) {
const extraDest = path.join(resourcesPath, extra);
fs.copyFileSync(extraSrc, extraDest);
fs.chmodSync(extraDest, 0o755);
console.log(`✅ Copied ${extra}: ${extraSrc} -> ${extraDest}`);
}
}
}
// --- 复制 rg (ripgrep) 二进制 ---
const rgBinaryName = platform === "win32" ? "rg.exe" : "rg";
const rgSrc = getRgBinaryPath(platform, arch);
const rgDest = path.join(resourcesPath, rgBinaryName);
if (rgSrc) {
fs.copyFileSync(rgSrc, rgDest);
fs.chmodSync(rgDest, 0o755);
console.log(`✅ Copied rg binary: ${rgSrc} -> ${rgDest}`);
} else {
console.warn(`⚠️ rg binary not found for ${platform}-${arch}, skipping`);
}
},
},
};