Skip to content

Commit a4a6c56

Browse files
committed
optimize: Update build-optimize
optimize: Update notarize
1 parent e7302b6 commit a4a6c56

2 files changed

Lines changed: 47 additions & 63 deletions

File tree

‎scripts/build-optimize.cjs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,48 @@ function prepareMacExecutable(file) {
118118
runOptional("codesign", ["--force", "--sign", "-", file], "codesign CLI");
119119
}
120120

121+
function findCodesignIdentity() {
122+
const result = childProcess.spawnSync(
123+
"bash",
124+
["-c", `security find-identity -v -p codesigning | grep "Developer ID Application" | head -1 | sed 's/.*"\\(.*\\)"/\\1/'`],
125+
{encoding: "utf8"}
126+
);
127+
return (result.stdout || "").trim();
128+
}
129+
130+
/**
131+
* Sign every .node binary while the app bundle is still unsealed (afterPack runs
132+
* before electron-builder codesigns). Re-signing them in afterSign would modify
133+
* sealed resources and make notarization fail with
134+
* "a sealed resource is missing or invalid".
135+
*/
136+
function signNodeBinaries(context) {
137+
const appPath = path.join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`);
138+
const identity = findCodesignIdentity();
139+
if (!identity) {
140+
console.log(" [sign] no Developer ID Application certificate, skipping .node signing");
141+
return;
142+
}
143+
const nodeFiles = [];
144+
walkFiles(appPath, (file, stat) => {
145+
if (!stat.isDirectory() && file.endsWith(".node")) nodeFiles.push(file);
146+
});
147+
if (nodeFiles.length === 0) return;
148+
console.log(` [sign] ${nodeFiles.length} .node file(s) with: ${identity}`);
149+
for (const file of nodeFiles) {
150+
const result = childProcess.spawnSync(
151+
"codesign",
152+
["--sign", identity, "--force", "--options", "runtime", "--timestamp", file],
153+
{encoding: "utf8", timeout: 30000}
154+
);
155+
if (result.status === 0) {
156+
console.log(` ✓ ${file}`);
157+
} else {
158+
console.log(` [warn] failed to sign ${file}: ${(result.stderr || result.error || "").toString().trim()}`);
159+
}
160+
}
161+
}
162+
121163
// ── main ─────────────────────────────────────────────────────────
122164
exports.default = async function (context) {
123165
console.log("BuildOptimize", {name: common.platformName(), arch: common.platformArch()});
@@ -163,4 +205,9 @@ exports.default = async function (context) {
163205
assertExists(path.join(taskDir, "lib"), "Python runtime lib");
164206
assertExists(path.join(taskDir, initScriptName), "Python runtime init script");
165207
console.log(` [check] Python runtime size: ${formatBytes(dirSize(pythonDir))}`);
208+
209+
// ── 3. Sign native addons before electron-builder seals the bundle ──
210+
if (platformName === "osx") {
211+
signNodeBinaries(context);
212+
}
166213
};

‎scripts/notarize.cjs‎

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,6 @@
11
const {notarize} = require("@electron/notarize");
2-
const {execSync} = require("node:child_process");
3-
const {readdirSync, lstatSync} = require("node:fs");
4-
const {resolve, join} = require("node:path");
52
const common = require('./common.cjs')
63

7-
/**
8-
* Walk a directory recursively, yielding absolute paths.
9-
*/
10-
function walkDir(dir) {
11-
const results = [];
12-
if (!common.exists(dir)) return results;
13-
for (const entry of readdirSync(dir)) {
14-
const full = join(dir, entry);
15-
const stat = lstatSync(full);
16-
if (stat.isDirectory()) {
17-
results.push(...walkDir(full));
18-
} else {
19-
results.push(full);
20-
}
21-
}
22-
return results;
23-
}
24-
25-
/**
26-
* Sign every .node file inside the .app bundle using the first available
27-
* Developer ID certificate. This avoids Gatekeeper errors on quarantined
28-
* apps for unsigned native addons (rollup, fsevents, etc.).
29-
*/
30-
function signNodeModules(appPath) {
31-
const identity = execSync(
32-
`security find-identity -v -p codesigning | grep "Developer ID Application" | head -1 | sed 's/.*"\\(.*\\)"/\\1/'`,
33-
{encoding: "utf8"}
34-
).trim();
35-
if (!identity) {
36-
console.warn(" • [WARN] No Developer ID Application certificate found, skipping .node signing");
37-
return;
38-
}
39-
const contents = resolve(appPath, "Contents");
40-
if (!common.exists(contents)) {
41-
console.warn(" • [WARN] .app/Contents not found, skipping .node signing");
42-
return;
43-
}
44-
const allFiles = walkDir(contents);
45-
const nodeFiles = allFiles.filter(f => f.endsWith(".node"));
46-
if (nodeFiles.length === 0) {
47-
console.log(" • No .node files found, skipping code signing");
48-
return;
49-
}
50-
console.log(` • Signing ${nodeFiles.length} .node file(s) with identity: ${identity}`);
51-
for (const file of nodeFiles) {
52-
try {
53-
execSync(
54-
`codesign --sign "${identity}" --force --options runtime --timestamp "${file}" 2>&1`,
55-
{stdio: ["ignore", "pipe", "pipe"], timeout: 30000}
56-
);
57-
console.log(` ✓ ${file}`);
58-
} catch (err) {
59-
console.warn(` ⚠ Failed to sign ${file}: ${err.stderr || err.message}`);
60-
}
61-
}
62-
}
63-
644
exports.default = async function notarizing(context) {
655
const appName = context.packager.appInfo.productFilename;
666
const {electronPlatformName, appOutDir} = context;
@@ -82,9 +22,6 @@ exports.default = async function notarizing(context) {
8222

8323
let appPath = `${appOutDir}/${appName}.app`;
8424

85-
// ── Sign all .node files before notarization ───────────────────
86-
signNodeModules(appPath);
87-
8825
let {APPLE_ID, APPLE_ID_PASSWORD, APPLE_TEAM_ID} = process.env;
8926
if (!APPLE_ID) {
9027
console.info(" • Notarization ignore: APPLE_ID is empty");

0 commit comments

Comments
 (0)