Elffuss BraidHub

kikocis/continue public

f34c6dd29412 · 2.9 KiBRawPermalink
1/**
2 * @file Builds the binary for the specified target. It is also intended to run as a child process.
3 */
4
5const {
6 execCmdSync,
7 autodetectPlatformAndArch,
8} = require("../../scripts/util");
9const { downloadRipgrep } = require("./ripgrep");
10const { TARGET_TO_LANCEDB } = require("../utils/targets");
11const fs = require("fs");
12const {
13 downloadSqlite,
14} = require("../../extensions/vscode/scripts/download-copy-sqlite-esbuild");
15const { fork } = require("child_process");
16
17async function downloadNodeSqlite(target, targetDir) {
18 const [currentPlatform, currentArch] = autodetectPlatformAndArch();
19
20 // Download and unzip prebuilt sqlite3 binary for the target
21 console.log("[info] Downloading node-sqlite3");
22
23 await downloadSqlite(target, `${targetDir}/build.tar.gz`);
24
25 execCmdSync(`cd ${targetDir} && tar -xvzf build.tar.gz`);
26
27 // Copy to build directory for testing
28 try {
29 const [platform, arch] = target.split("-");
30 if (platform === currentPlatform && arch === currentArch) {
31 fs.copyFileSync(
32 `${targetDir}/build/Release/node_sqlite3.node`,
33 `build/node_sqlite3.node`,
34 );
35 }
36 } catch (error) {
37 console.log("[warn] Could not copy node_sqlite to build");
38 console.log(error);
39 }
40 fs.unlinkSync(`${targetDir}/build.tar.gz`);
41}
42
43/**
44 * @param {string} target the platform to build for
45 */
46async function bundleForBinary(target) {
47 const targetDir = `bin/${target}`;
48 fs.mkdirSync(targetDir, { recursive: true });
49 console.log(`[info] Building ${target}...`);
50 execCmdSync(
51 `npx pkg --no-bytecode --public-packages "*" --public --compress GZip pkgJson/${target} --out-path ${targetDir}`,
52 );
53
54 // copy @lancedb to bin folders
55 console.log("[info] Copying @lancedb files to bin");
56 fs.copyFileSync(
57 `node_modules/${TARGET_TO_LANCEDB[target]}/index.node`,
58 `${targetDir}/index.node`,
59 );
60
61 const downloadPromises = [];
62 downloadPromises.push(downloadRipgrep(target, targetDir));
63 downloadPromises.push(downloadNodeSqlite(target, targetDir));
64 await Promise.all(downloadPromises);
65
66 // Informs the `continue-binary` of where to look for node_sqlite3.node
67 // https://www.npmjs.com/package/bindings#:~:text=The%20searching%20for,file%20is%20found
68 fs.writeFileSync(`${targetDir}/package.json`, "");
69}
70
71process.on("message", (msg) => {
72 bundleForBinary(msg.payload.target)
73 .then(() => process.send({ done: true }))
74 .catch((error) => {
75 console.error(error); // show the error in the parent process
76 process.send({ error: true });
77 });
78});
79
80/**
81 * @param {string} target the platform to bundle for
82 */
83async function bundleBinary(target) {
84 const child = fork(__filename, { stdio: "inherit" });
85 child.send({
86 payload: {
87 target,
88 },
89 });
90 return new Promise((resolve, reject) => {
91 child.on("message", (msg) => {
92 if (msg.error) {
93 reject();
94 } else {
95 resolve();
96 }
97 });
98 });
99}
100
101module.exports = {
102 bundleBinary,
103};