refactor bundle binary to modular function

This commit is contained in:
uinstinct 2025-06-06 23:03:20 +05:30
parent 1d4a31af22
commit a363e34f75
2 changed files with 29 additions and 17 deletions

View File

@ -9,6 +9,7 @@ const { fork } = require("child_process");
const {
installAndCopyNodeModules,
} = require("../extensions/vscode/scripts/install-copy-nodemodule");
const { bundleBinary } = require("./utils/bundle-binary");
const bin = path.join(__dirname, "bin");
const out = path.join(__dirname, "out");
@ -178,22 +179,7 @@ async function buildWithEsbuild() {
const buildBinaryPromises = [];
console.log("[info] Building binaries with pkg...");
for (const target of targets) {
const child = fork("./utils/bundle-binary.js", { stdio: "inherit" });
child.send({
payload: {
target,
},
});
buildBinaryPromises.push(
new Promise((resolve, reject) => {
child.on("message", (msg) => {
if (msg.error) {
reject();
}
resolve();
});
}),
);
buildBinaryPromises.push(bundleBinary(target));
}
await Promise.all(buildBinaryPromises).catch(() => {
console.error("[error] Failed to build binaries");

View File

@ -1,6 +1,7 @@
/**
* @file Builds the binary for the specified target. It is intended to run as a child process.
* @file Builds the binary for the specified target. It is also intended to run as a child process.
*/
const {
execCmdSync,
autodetectPlatformAndArch,
@ -11,6 +12,7 @@ const fs = require("fs");
const {
downloadSqlite,
} = require("../../extensions/vscode/scripts/download-copy-sqlite-esbuild");
const { fork } = require("child_process");
async function downloadNodeSqlite(target, targetDir) {
const [currentPlatform, currentArch] = autodetectPlatformAndArch();
@ -74,3 +76,27 @@ process.on("message", (msg) => {
process.send({ error: true });
});
});
/**
* @param {string} target the platform to bundle for
*/
async function bundleBinary(target) {
const child = fork(__filename, { stdio: "inherit" });
child.send({
payload: {
target,
},
});
return new Promise((resolve, reject) => {
child.on("message", (msg) => {
if (msg.error) {
reject();
}
resolve();
});
});
}
module.exports = {
bundleBinary,
};