Skip to content

Commit ab84453

Browse files
committed
Fix issues with make-static command.
1 parent 512cdd8 commit ab84453

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

cli.js

+49-39
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,64 @@ compilerInstance.ports.completeStaticBuild.subscribe(async function (output) {
2828
// the Gren application, with a hint that tells the V8 engine what the
2929
// main function is
3030

31-
const compiledSrc = await fs.readFile(jsBuildPath, "utf-8");
32-
33-
const initRegex = /this\.Gren\..+\(\{\}\);/g;
34-
const initCall = compiledSrc.match(initRegex)[0];
35-
const snapshotCompatibleSrc = compiledSrc.replace(
36-
initCall,
37-
`
38-
const v8 = require('node:v8');
39-
v8.startupSnapshot.setDeserializeMainFunction(function() {
40-
${initCall}
41-
});
42-
`,
43-
);
31+
try {
32+
const compiledSrc = await fs.readFile(jsBuildPath, "utf-8");
33+
34+
const initRegex = /this\.Gren\..+\(\{\}\);/g;
35+
const initCall = compiledSrc.match(initRegex)[0];
36+
const snapshotCompatibleSrc = compiledSrc.replace(
37+
initCall,
38+
`
39+
const v8 = require('node:v8');
40+
v8.startupSnapshot.setDeserializeMainFunction(function() {
41+
${initCall}
42+
});
43+
`,
44+
);
4445

45-
await fs.writeFile(jsBuildPath, snapshotCompatibleSrc);
46+
await fs.writeFile(jsBuildPath, snapshotCompatibleSrc);
4647

47-
// We then need to generate the snapshot
48+
// We then need to generate the snapshot
4849

49-
const nodePath = process.execPath;
50-
await fs.writeFile(seaConfigPath, JSON.stringify(seaConfig));
51-
cp.execFileSync(nodePath, ["--experimental-sea-config", seaConfigPath]);
50+
const nodePath = process.execPath;
51+
await fs.writeFile(seaConfigPath, JSON.stringify(seaConfig));
52+
cp.execFileSync(nodePath, ["--experimental-sea-config", seaConfigPath]);
5253

53-
// Then copy the node executable and inject the snapshot into it
54-
await fs.copyFile(nodePath, binPath);
54+
// Then copy the node executable and inject the snapshot into it
55+
await fs.copyFile(nodePath, binPath);
56+
await fs.chmod(binPath, "755");
5557

56-
if (isMac) {
57-
// required on mac, optional on windows, not required on linux
58-
cp.execFileSync("codesign", ["--remove-signature", binPath]);
59-
}
58+
if (isMac) {
59+
// required on mac, optional on windows, not required on linux
60+
cp.execFileSync("codesign", ["--remove-signature", binPath]);
61+
}
6062

61-
const blobContent = await fs.readFile(blobPath);
63+
const blobContent = await fs.readFile(blobPath);
6264

63-
await postject.inject(binPath, "NODE_SEA_BLOB", blobContent, {
64-
sentinelFuse: "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2",
65-
machoSegmentName: isMac ? "NODE_SEA" : undefined,
66-
});
65+
await postject.inject(binPath, "NODE_SEA_BLOB", blobContent, {
66+
sentinelFuse: "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2",
67+
machoSegmentName: isMac ? "NODE_SEA" : undefined,
68+
});
6769

68-
if (isMac) {
69-
// required on mac
70-
cp.execFileSync("codesign", ["--sign", "-", binPath]);
71-
}
70+
if (isMac) {
71+
// required on mac
72+
cp.execFileSync("codesign", ["--sign", "-", binPath]);
73+
}
7274

73-
// cleanup
75+
const outputPath = isWin ? jsBuildPath + ".exe" : jsBuildPath;
76+
await fs.rename(binPath, outputPath);
7477

75-
await fs.rm(jsBuildPath);
76-
await fs.rm(blobPath);
77-
await fs.rm(seaConfigPath);
78+
if (isWin) {
79+
await fs.rm(jsBuildPath);
80+
}
7881

79-
const outputPath = isWin ? jsBuildPath + ".exe" : jsBuildPath;
80-
await fs.rename(binPath, outputPath);
82+
console.log("Done!");
83+
} catch (e) {
84+
console.error("Failed to create static executable", e);
85+
await fs.rm(jsBuildPath);
86+
} finally {
87+
// cleanup
88+
await fs.rm(blobPath);
89+
await fs.rm(seaConfigPath);
90+
}
8191
});

0 commit comments

Comments
 (0)