From 3722b8c4f8a3896f2410897cb8b9874a4dec2bcb Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Fri, 6 Jun 2025 22:51:55 +0530 Subject: [PATCH] refactor download node-sqlite to common --- binary/utils/bundle-binary.js | 49 ++++++++----------- .../scripts/install-copy-sqlite-esbuild.js | 33 +++++++------ 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/binary/utils/bundle-binary.js b/binary/utils/bundle-binary.js index c218166cd..91d2f4593 100644 --- a/binary/utils/bundle-binary.js +++ b/binary/utils/bundle-binary.js @@ -8,41 +8,34 @@ const { const { downloadRipgrep } = require("./ripgrep"); const { TARGET_TO_LANCEDB } = require("../utils/targets"); const fs = require("fs"); +const { + downloadSqlite, +} = require("../../extensions/vscode/scripts/install-copy-sqlite-esbuild"); async function downloadNodeSqlite(target, targetDir) { - return new Promise((resolve) => { - const [currentPlatform, currentArch] = autodetectPlatformAndArch(); + const [currentPlatform, currentArch] = autodetectPlatformAndArch(); - // Download and unzip prebuilt sqlite3 binary for the target - console.log("[info] Downloading node-sqlite3"); + // Download and unzip prebuilt sqlite3 binary for the target + console.log("[info] Downloading node-sqlite3"); - const downloadUrl = - // node-sqlite3 doesn't have a pre-built binary for win32-arm64 - target === "win32-arm64" - ? "https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz" - : `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${ - target - }.tar.gz`; + await downloadSqlite(target, `${targetDir}/build.tar.gz`); - execCmdSync(`curl -L -o ${targetDir}/build.tar.gz ${downloadUrl}`); - execCmdSync(`cd ${targetDir} && tar -xvzf build.tar.gz`); + execCmdSync(`cd ${targetDir} && tar -xvzf build.tar.gz`); - // Copy to build directory for testing - try { - const [platform, arch] = target.split("-"); - if (platform === currentPlatform && arch === currentArch) { - fs.copyFileSync( - `${targetDir}/build/Release/node_sqlite3.node`, - `build/node_sqlite3.node`, - ); - } - } catch (error) { - console.log("[warn] Could not copy node_sqlite to build"); - console.log(error); + // Copy to build directory for testing + try { + const [platform, arch] = target.split("-"); + if (platform === currentPlatform && arch === currentArch) { + fs.copyFileSync( + `${targetDir}/build/Release/node_sqlite3.node`, + `build/node_sqlite3.node`, + ); } - fs.unlinkSync(`${targetDir}/build.tar.gz`); - resolve(); - }); + } catch (error) { + console.log("[warn] Could not copy node_sqlite to build"); + console.log(error); + } + fs.unlinkSync(`${targetDir}/build.tar.gz`); } /** diff --git a/extensions/vscode/scripts/install-copy-sqlite-esbuild.js b/extensions/vscode/scripts/install-copy-sqlite-esbuild.js index 370876c7f..ad33b2eb6 100644 --- a/extensions/vscode/scripts/install-copy-sqlite-esbuild.js +++ b/extensions/vscode/scripts/install-copy-sqlite-esbuild.js @@ -1,4 +1,4 @@ -const { fork, execSync } = require("child_process"); +const { fork } = require("child_process"); const fs = require("fs"); const https = require("https"); const path = require("path"); @@ -80,23 +80,27 @@ async function downloadFile(url, outputPath, maxRedirects = 5) { }); } +/** + * + * @param {string} target platform specific target + * @param {string} targetDir the directory to download into + */ +async function downloadSqlite(target, targetDir) { + const downloadUrl = + // node-sqlite3 doesn't have a pre-built binary for win32-arm64 + target === "win32-arm64" + ? "https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz" + : `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${ + target + }.tar.gz`; + await downloadFile(downloadUrl, targetDir); +} + async function installAndCopySqlite(target) { // Replace the installed with pre-built console.log("[info] Downloading pre-built sqlite3 binary"); rimrafSync("../../core/node_modules/sqlite3/build"); - const downloadUrl = { - "darwin-arm64": - "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-darwin-arm64.tar.gz", - "linux-arm64": - "https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v3-linux-arm64.tar.gz", - // node-sqlite3 doesn't have a pre-built binary for win32-arm64 - "win32-arm64": - "https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz", - }[target]; - await downloadFile( - downloadUrl, - "../../core/node_modules/sqlite3/build.tar.gz", - ); + await downloadSqlite(target, "../../core/node_modules/sqlite3/build.tar.gz"); execCmdSync("cd ../../core/node_modules/sqlite3 && tar -xvzf build.tar.gz"); fs.unlinkSync("../../core/node_modules/sqlite3/build.tar.gz"); } @@ -179,6 +183,7 @@ async function copyEsbuild(target) { } module.exports = { + downloadSqlite, copySqlite, copyEsbuild, };