diff --git a/binary/.gitignore b/binary/.gitignore index d748f39f1..4a887119d 100644 --- a/binary/.gitignore +++ b/binary/.gitignore @@ -2,3 +2,4 @@ node_modules bin data out +tmp \ No newline at end of file diff --git a/binary/build.js b/binary/build.js index be2c09f05..93a6e0e1f 100644 --- a/binary/build.js +++ b/binary/build.js @@ -1,18 +1,18 @@ const esbuild = require("esbuild"); -const { execSync } = require("child_process"); const fs = require("fs"); const path = require("path"); const ncp = require("ncp").ncp; const { rimrafSync } = require("rimraf"); +const { validateFilesPresent, execCmdSync } = require("../scripts/util"); -function execCmdSync(cmd) { - try { - execSync(cmd); - } catch (err) { - console.error(`Error executing command '${cmd}': `, err.output.toString()); - process.exit(1); - } -} +// Clean slate +const bin = path.join(__dirname, "bin"); +const out = path.join(__dirname, "out"); +rimrafSync(bin); +rimrafSync(out); +rimrafSync(path.join(__dirname, "tmp")); +fs.mkdirSync(bin); +fs.mkdirSync(out); const esbuildOutputFile = "out/index.js"; const platforms = ["darwin", "linux", "win32"]; @@ -41,13 +41,14 @@ const targetToLanceDb = { "linux-arm64": "@lancedb/vectordb-linux-arm64-gnu", "linux-x64": "@lancedb/vectordb-linux-x64-gnu", "win32-x64": "@lancedb/vectordb-win32-x64-msvc", + "win32-arm64": "@lancedb/vectordb-win32-x64-msvc", // they don't have a win32-arm64 build }; -async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { - console.log(`Copying ${package} to ${toCopy}`); +async function installNodeModuleInTempDirAndCopyToCurrent(packageName, toCopy) { + console.log(`Copying ${packageName} to ${toCopy}`); // This is a way to install only one package without npm trying to install all the dependencies // Create a temporary directory for installing the package - const adjustedName = toCopy.replace(/^@/, "").replace("/", "-"); + const adjustedName = packageName.replace(/^@/, "").replace("/", "-"); const tempDir = path.join( __dirname, "tmp", @@ -66,13 +67,17 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { process.chdir(tempDir); // Initialize a new package.json and install the package - execCmdSync(`npm init -y && npm i -f ${package} --no-save`); + execCmdSync(`npm init -y && npm i -f ${packageName} --no-save`); console.log( - `Contents of: ${package}`, + `Contents of: ${packageName}`, fs.readdirSync(path.join(tempDir, "node_modules", toCopy)), ); + // Without this it seems the file isn't completely written to disk + // Ideally we validate file integrity in the validation at the end + await new Promise((resolve) => setTimeout(resolve, 2000)); + // Copy the installed package back to the current directory await new Promise((resolve, reject) => { ncp( @@ -81,7 +86,10 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { { dereference: true }, (error) => { if (error) { - console.error(`[error] Error copying ${package} package`, error); + console.error( + `[error] Error copying ${packageName} package`, + error, + ); reject(error); } else { resolve(); @@ -91,7 +99,7 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { }); } finally { // Clean up the temporary directory - rimrafSync(tempDir); + // rimrafSync(tempDir); // Return to the original directory process.chdir(currentDir); @@ -99,57 +107,10 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { } (async () => { - // console.log("[info] Building with ncc..."); - // execCmdSync(`npx ncc build src/index.ts -o out`); - - // Copy node_modules for pre-built binaries - const DYNAMIC_IMPORTS = [ - // "esbuild", - // "@esbuild", - // // "@lancedb", - // "posthog-node", - // "@octokit", - ]; - fs.mkdirSync("out/node_modules", { recursive: true }); - fs.mkdirSync("bin/node_modules", { recursive: true }); - - await Promise.all( - DYNAMIC_IMPORTS.map( - (mod) => - new Promise((resolve, reject) => { - ncp( - `node_modules/${mod}`, - `out/node_modules/${mod}`, - function (error) { - if (error) { - console.error(`[error] Error copying ${mod}`, error); - reject(error); - } else { - resolve(); - } - }, - ); - ncp( - `node_modules/${mod}`, - `bin/node_modules/${mod}`, - function (error) { - if (error) { - console.error(`[error] Error copying ${mod}`, error); - reject(error); - } else { - resolve(); - } - }, - ); - }), - ), - ); - console.log(`[info] Copied ${DYNAMIC_IMPORTS.join(", ")}`); - console.log("[info] Downloading prebuilt lancedb..."); for (const target of targets) { if (targetToLanceDb[target]) { - console.log(`[info] Downloading ${target}...`); + console.log(`[info] Downloading for ${target}...`); await installNodeModuleInTempDirAndCopyToCurrent( targetToLanceDb[target], "@lancedb", @@ -158,6 +119,8 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { } // tree-sitter-wasm + const treeSitterWasmsDir = path.join(out, "tree-sitter-wasms"); + fs.mkdirSync(treeSitterWasmsDir); await new Promise((resolve, reject) => { ncp( path.join( @@ -168,7 +131,7 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { "tree-sitter-wasms", "out", ), - path.join(__dirname, "out/tree-sitter-wasms"), + treeSitterWasmsDir, { dereference: true }, (error) => { if (error) { @@ -194,13 +157,13 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { fs.rmSync(assetPath, { force: true }); } - console.log("[info] Building with esbuild..."); // Bundles the extension into one file + console.log("[info] Building with esbuild..."); await esbuild.build({ entryPoints: ["src/index.ts"], bundle: true, outfile: esbuildOutputFile, - external: ["esbuild", ...DYNAMIC_IMPORTS, "./xhr-sync-worker.js", "vscode"], + external: ["esbuild", "./xhr-sync-worker.js", "vscode"], format: "cjs", platform: "node", sourcemap: true, @@ -235,6 +198,7 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { ); // Download and unzip prebuilt sqlite3 binary for the target + console.log("[info] Downloading node-sqlite3"); const downloadUrl = `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${ target === "win32-arm64" ? "win32-ia32" : target }.tar.gz`; @@ -267,9 +231,30 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { force: true, recursive: true, }); + + // copy @lancedb to bin folders + console.log("[info] Copying @lancedb files to bin"); + fs.copyFileSync( + `node_modules/${targetToLanceDb[target]}/index.node`, + `${targetDir}/index.node`, + ); } // execCmdSync( // `npx pkg out/index.js --target node18-darwin-arm64 --no-bytecode --public-packages "*" --public -o bin/pkg` // ); + + const pathsToVerify = []; + for (target of targets) { + const exe = target.startsWith("win") ? ".exe" : ""; + const targetDir = `bin/${target}`; + pathsToVerify.push( + `${targetDir}/continue-binary${exe}`, + `${targetDir}/esbuild${exe}`, + `${targetDir}/index.node`, // @lancedb + `${targetDir}/node_sqlite3.node`, + ); + } + validateFilesPresent(pathsToVerify); + console.log("[info] Done!"); })(); diff --git a/binary/pkgJson/darwin-arm64/package.json b/binary/pkgJson/darwin-arm64/package.json index 0cb1b4d32..cc41ae801 100644 --- a/binary/pkgJson/darwin-arm64/package.json +++ b/binary/pkgJson/darwin-arm64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-darwin-arm64/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*" ], diff --git a/binary/pkgJson/darwin-x64/package.json b/binary/pkgJson/darwin-x64/package.json index 65ad94d25..8ccb256e1 100644 --- a/binary/pkgJson/darwin-x64/package.json +++ b/binary/pkgJson/darwin-x64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-darwin-x64/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*" ], diff --git a/binary/pkgJson/linux-arm64/package.json b/binary/pkgJson/linux-arm64/package.json index fbea8ff60..9fe88da50 100644 --- a/binary/pkgJson/linux-arm64/package.json +++ b/binary/pkgJson/linux-arm64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-linux-arm64-gnu/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*" ], diff --git a/binary/pkgJson/linux-x64/package.json b/binary/pkgJson/linux-x64/package.json index 508a116d4..1c875baa0 100644 --- a/binary/pkgJson/linux-x64/package.json +++ b/binary/pkgJson/linux-x64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-linux-x64-gnu/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*" ], diff --git a/binary/pkgJson/win32-arm64/package.json b/binary/pkgJson/win32-arm64/package.json index d8e773753..3eeb12234 100644 --- a/binary/pkgJson/win32-arm64/package.json +++ b/binary/pkgJson/win32-arm64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-win32-x64-msvc/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*", "../../node_modules/win-ca/lib/crypt32-ia32.node", diff --git a/binary/pkgJson/win32-x64/package.json b/binary/pkgJson/win32-x64/package.json index 2153e9b5f..2e495524b 100644 --- a/binary/pkgJson/win32-x64/package.json +++ b/binary/pkgJson/win32-x64/package.json @@ -9,7 +9,6 @@ ], "assets": [ "../../../core/node_modules/sqlite3/**/*", - "../../node_modules/@lancedb/vectordb-win32-x64-msvc/index.node", "../../out/tree-sitter.wasm", "../../out/tree-sitter-wasms/*", "../../node_modules/win-ca/lib/crypt32-ia32.node", diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 16ce8a911..7149075e1 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -20,6 +20,7 @@ "cors": "^2.8.5", "dbinfoz": "^0.1.4", "downshift": "^7.6.0", + "esbuild": "^0.17.19", "express": "^4.18.2", "fkill": "^8.1.0", "follow-redirects": "^1.15.4", diff --git a/extensions/vscode/scripts/prepackage.js b/extensions/vscode/scripts/prepackage.js index 57ab0ecf7..6033bcce6 100644 --- a/extensions/vscode/scripts/prepackage.js +++ b/extensions/vscode/scripts/prepackage.js @@ -1,17 +1,11 @@ -const { execSync } = require("child_process"); const fs = require("fs"); const ncp = require("ncp").ncp; const path = require("path"); const { rimrafSync } = require("rimraf"); - -function execCmdSync(cmd) { - try { - execSync(cmd); - } catch (err) { - console.error(`Error executing command '${cmd}': `, err.output.toString()); - process.exit(1); - } -} +const { + validateFilesPresent, + execCmdSync, +} = require("../../../scripts/util/index"); // Clear folders that will be packaged to ensure clean slate rimrafSync(path.join(__dirname, "..", "bin")); @@ -315,11 +309,14 @@ const exe = os === "win32" ? ".exe" : ""; return target?.startsWith("win"); } - async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) { - console.log(`Copying ${package} to ${toCopy}`); + async function installNodeModuleInTempDirAndCopyToCurrent( + packageName, + toCopy, + ) { + console.log(`Copying ${packageName} to ${toCopy}`); // This is a way to install only one package without npm trying to install all the dependencies // Create a temporary directory for installing the package - const adjustedName = toCopy.replace(/^@/, "").replace("/", "-"); + const adjustedName = packageName.replace(/^@/, "").replace("/", "-"); const tempDir = `/tmp/continue-node_modules-${adjustedName}`; const currentDir = process.cwd(); @@ -335,13 +332,17 @@ const exe = os === "win32" ? ".exe" : ""; process.chdir(tempDir); // Initialize a new package.json and install the package - execCmdSync(`npm init -y && npm i -f ${package} --no-save`); + execCmdSync(`npm init -y && npm i -f ${packageName} --no-save`); console.log( - `Contents of: ${package}`, + `Contents of: ${packageName}`, fs.readdirSync(path.join(tempDir, "node_modules", toCopy)), ); + // Without this it seems the file isn't completely written to disk + // Ideally we validate file integrity in the validation at the end + await new Promise((resolve) => setTimeout(resolve, 2000)); + // Copy the installed package back to the current directory await new Promise((resolve, reject) => { ncp( @@ -350,7 +351,10 @@ const exe = os === "win32" ? ".exe" : ""; { dereference: true }, (error) => { if (error) { - console.error(`[error] Error copying ${package} package`, error); + console.error( + `[error] Error copying ${packageName} package`, + error, + ); reject(error); } else { resolve(); @@ -481,14 +485,7 @@ const exe = os === "win32" ? ".exe" : ""; ); // Validate the all of the necessary files are present - validateFilesPresent(); -})(); - -function validateFilesPresent() { - // This script verifies after pacakging that necessary files are in the correct locations - // In many cases just taking a sample file from the folder when they are all roughly the same thing - - const pathsToVerify = [ + validateFilesPresent([ // Queries used to create the index for @code context provider "tree-sitter/code-snippet-queries/tree-sitter-c_sharp-tags.scm", @@ -551,52 +548,5 @@ function validateFilesPresent() { }/index.node`, `out/node_modules/esbuild/lib/main.js`, `out/node_modules/esbuild/bin/esbuild`, - ]; - - let missingFiles = []; - for (const path of pathsToVerify) { - if (!fs.existsSync(path)) { - const parentFolder = path.split("/").slice(0, -1).join("/"); - const grandparentFolder = path.split("/").slice(0, -2).join("/"); - const grandGrandparentFolder = path.split("/").slice(0, -3).join("/"); - - console.error(`File ${path} does not exist`); - if (!fs.existsSync(parentFolder)) { - console.error(`Parent folder ${parentFolder} does not exist`); - } else { - console.error( - "Contents of parent folder:", - fs.readdirSync(parentFolder), - ); - } - if (!fs.existsSync(grandparentFolder)) { - console.error(`Grandparent folder ${grandparentFolder} does not exist`); - if (!fs.existsSync(grandGrandparentFolder)) { - console.error( - `Grandgrandparent folder ${grandGrandparentFolder} does not exist`, - ); - } else { - console.error( - "Contents of grandgrandparent folder:", - fs.readdirSync(grandGrandparentFolder), - ); - } - } else { - console.error( - "Contents of grandparent folder:", - fs.readdirSync(grandparentFolder), - ); - } - - missingFiles.push(path); - } - } - - if (missingFiles.length > 0) { - throw new Error( - `The following files were missing:\n- ${missingFiles.join("\n- ")}`, - ); - } else { - console.log("All paths exist"); - } -} + ]); +})();