| 1 | const fs = require("fs"); |
| 2 | const path = require("path"); |
| 3 | |
| 4 | const ncp = require("ncp").ncp; |
| 5 | const { rimrafSync } = require("rimraf"); |
| 6 | |
| 7 | const { |
| 8 | validateFilesPresent, |
| 9 | execCmdSync, |
| 10 | autodetectPlatformAndArch, |
| 11 | } = require("../../../scripts/util/index"); |
| 12 | |
| 13 | const { copySqlite, copyEsbuild } = require("./download-copy-sqlite-esbuild"); |
| 14 | const { generateAndCopyConfigYamlSchema } = require("./generate-copy-config"); |
| 15 | const { installAndCopyNodeModules } = require("./install-copy-nodemodule"); |
| 16 | const { npmInstall } = require("./npm-install"); |
| 17 | const { writeBuildTimestamp, continueDir } = require("./utils"); |
| 18 | |
| 19 | // Clear folders that will be packaged to ensure clean slate |
| 20 | rimrafSync(path.join(__dirname, "..", "bin")); |
| 21 | rimrafSync(path.join(__dirname, "..", "out")); |
| 22 | fs.mkdirSync(path.join(__dirname, "..", "out", "node_modules"), { |
| 23 | recursive: true, |
| 24 | }); |
| 25 | const guiDist = path.join(__dirname, "..", "..", "..", "gui", "dist"); |
| 26 | if (!fs.existsSync(guiDist)) { |
| 27 | fs.mkdirSync(guiDist, { recursive: true }); |
| 28 | } |
| 29 | |
| 30 | const skipInstalls = process.env.SKIP_INSTALLS === "true"; |
| 31 | |
| 32 | // Get the target to package for |
| 33 | let target = undefined; |
| 34 | const args = process.argv; |
| 35 | if (args[2] === "--target") { |
| 36 | target = args[3]; |
| 37 | } |
| 38 | |
| 39 | let os; |
| 40 | let arch; |
| 41 | if (target) { |
| 42 | [os, arch] = target.split("-"); |
| 43 | } else { |
| 44 | [os, arch] = autodetectPlatformAndArch(); |
| 45 | } |
| 46 | |
| 47 | if (os === "alpine") { |
| 48 | os = "linux"; |
| 49 | } |
| 50 | if (arch === "armhf") { |
| 51 | arch = "arm64"; |
| 52 | } |
| 53 | target = `${os}-${arch}`; |
| 54 | console.log("[info] Using target: ", target); |
| 55 | |
| 56 | const exe = os === "win32" ? ".exe" : ""; |
| 57 | |
| 58 | const isInGitHubAction = !!process.env.GITHUB_ACTIONS; |
| 59 | |
| 60 | const isArmTarget = |
| 61 | target === "darwin-arm64" || |
| 62 | target === "linux-arm64" || |
| 63 | target === "win32-arm64"; |
| 64 | |
| 65 | const isWinTarget = target?.startsWith("win"); |
| 66 | const isLinuxTarget = target?.startsWith("linux"); |
| 67 | const isMacTarget = target?.startsWith("darwin"); |
| 68 | |
| 69 | void (async () => { |
| 70 | console.log("[info] Packaging extension for target ", target); |
| 71 | |
| 72 | // Make sure we have an initial timestamp file |
| 73 | writeBuildTimestamp(); |
| 74 | |
| 75 | if (!skipInstalls) { |
| 76 | await Promise.all([generateAndCopyConfigYamlSchema(), npmInstall()]); |
| 77 | } |
| 78 | |
| 79 | process.chdir(path.join(continueDir, "gui")); |
| 80 | |
| 81 | if (isInGitHubAction) { |
| 82 | execCmdSync("npm run build"); |
| 83 | } |
| 84 | |
| 85 | // Copy over the dist folder to the JetBrains extension // |
| 86 | const intellijExtensionWebviewPath = path.join( |
| 87 | "..", |
| 88 | "extensions", |
| 89 | "intellij", |
| 90 | "src", |
| 91 | "main", |
| 92 | "resources", |
| 93 | "webview", |
| 94 | ); |
| 95 | |
| 96 | const indexHtmlPath = path.join(intellijExtensionWebviewPath, "index.html"); |
| 97 | fs.copyFileSync(indexHtmlPath, "tmp_index.html"); |
| 98 | rimrafSync(intellijExtensionWebviewPath); |
| 99 | fs.mkdirSync(intellijExtensionWebviewPath, { recursive: true }); |
| 100 | |
| 101 | await new Promise((resolve, reject) => { |
| 102 | ncp("dist", intellijExtensionWebviewPath, (error) => { |
| 103 | if (error) { |
| 104 | console.warn( |
| 105 | "[error] Error copying React app build to JetBrains extension: ", |
| 106 | error, |
| 107 | ); |
| 108 | reject(error); |
| 109 | } |
| 110 | resolve(); |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | // Put back index.html |
| 115 | if (fs.existsSync(indexHtmlPath)) { |
| 116 | rimrafSync(indexHtmlPath); |
| 117 | } |
| 118 | fs.copyFileSync("tmp_index.html", indexHtmlPath); |
| 119 | fs.unlinkSync("tmp_index.html"); |
| 120 | |
| 121 | console.log("[info] Copied gui build to JetBrains extension"); |
| 122 | |
| 123 | // Then copy over the dist folder to the VSCode extension // |
| 124 | const vscodeGuiPath = path.join("../extensions/vscode/gui"); |
| 125 | fs.mkdirSync(vscodeGuiPath, { recursive: true }); |
| 126 | await new Promise((resolve, reject) => { |
| 127 | ncp("dist", vscodeGuiPath, (error) => { |
| 128 | if (error) { |
| 129 | console.log( |
| 130 | "Error copying React app build to VSCode extension: ", |
| 131 | error, |
| 132 | ); |
| 133 | reject(error); |
| 134 | } else { |
| 135 | console.log("Copied gui build to VSCode extension"); |
| 136 | resolve(); |
| 137 | } |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | if (!fs.existsSync(path.join("dist", "assets", "index.js"))) { |
| 142 | throw new Error("gui build did not produce index.js"); |
| 143 | } |
| 144 | if (!fs.existsSync(path.join("dist", "assets", "index.css"))) { |
| 145 | throw new Error("gui build did not produce index.css"); |
| 146 | } |
| 147 | |
| 148 | // Copy over native / wasm modules // |
| 149 | process.chdir("../extensions/vscode"); |
| 150 | |
| 151 | fs.mkdirSync("bin", { recursive: true }); |
| 152 | |
| 153 | // onnxruntime-node |
| 154 | await new Promise((resolve, reject) => { |
| 155 | ncp( |
| 156 | path.join(__dirname, "../../../core/node_modules/onnxruntime-node/bin"), |
| 157 | path.join(__dirname, "../bin"), |
| 158 | { |
| 159 | dereference: true, |
| 160 | }, |
| 161 | (error) => { |
| 162 | if (error) { |
| 163 | console.warn("[info] Error copying onnxruntime-node files", error); |
| 164 | reject(error); |
| 165 | } |
| 166 | resolve(); |
| 167 | }, |
| 168 | ); |
| 169 | }); |
| 170 | if (target) { |
| 171 | // If building for production, only need the binaries for current platform |
| 172 | try { |
| 173 | if (!target.startsWith("darwin")) { |
| 174 | rimrafSync(path.join(__dirname, "../bin/napi-v3/darwin")); |
| 175 | } |
| 176 | if (!target.startsWith("linux")) { |
| 177 | rimrafSync(path.join(__dirname, "../bin/napi-v3/linux")); |
| 178 | } |
| 179 | if (!target.startsWith("win")) { |
| 180 | rimrafSync(path.join(__dirname, "../bin/napi-v3/win32")); |
| 181 | } |
| 182 | |
| 183 | // Also don't want to include cuda/shared/tensorrt binaries, they are too large |
| 184 | if (target.startsWith("linux")) { |
| 185 | const filesToRemove = [ |
| 186 | "libonnxruntime_providers_cuda.so", |
| 187 | "libonnxruntime_providers_shared.so", |
| 188 | "libonnxruntime_providers_tensorrt.so", |
| 189 | ]; |
| 190 | filesToRemove.forEach((file) => { |
| 191 | const filepath = path.join( |
| 192 | __dirname, |
| 193 | "../bin/napi-v3/linux/x64", |
| 194 | file, |
| 195 | ); |
| 196 | if (fs.existsSync(filepath)) { |
| 197 | fs.rmSync(filepath); |
| 198 | } |
| 199 | }); |
| 200 | } |
| 201 | } catch (e) { |
| 202 | console.warn("[info] Error removing unused binaries", e); |
| 203 | } |
| 204 | } |
| 205 | console.log("[info] Copied onnxruntime-node"); |
| 206 | |
| 207 | // tree-sitter-wasm |
| 208 | fs.mkdirSync("out", { recursive: true }); |
| 209 | |
| 210 | await new Promise((resolve, reject) => { |
| 211 | ncp( |
| 212 | path.join(__dirname, "../../../core/node_modules/tree-sitter-wasms/out"), |
| 213 | path.join(__dirname, "../out/tree-sitter-wasms"), |
| 214 | { dereference: true }, |
| 215 | (error) => { |
| 216 | if (error) { |
| 217 | console.warn("[error] Error copying tree-sitter-wasm files", error); |
| 218 | reject(error); |
| 219 | } else { |
| 220 | resolve(); |
| 221 | } |
| 222 | }, |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | const filesToCopy = [ |
| 227 | "../../../core/vendor/tree-sitter.wasm", |
| 228 | "../../../core/llm/llamaTokenizerWorkerPool.mjs", |
| 229 | "../../../core/llm/llamaTokenizer.mjs", |
| 230 | "../../../core/llm/tiktokenWorkerPool.mjs", |
| 231 | "../../../core/util/start_ollama.sh", |
| 232 | ]; |
| 233 | |
| 234 | for (const f of filesToCopy) { |
| 235 | fs.copyFileSync( |
| 236 | path.join(__dirname, f), |
| 237 | path.join(__dirname, "..", "out", path.basename(f)), |
| 238 | ); |
| 239 | console.log(`[info] Copied ${path.basename(f)}`); |
| 240 | } |
| 241 | |
| 242 | // tree-sitter tag query files |
| 243 | // ncp( |
| 244 | // path.join( |
| 245 | // __dirname, |
| 246 | // "../../../core/node_modules/llm-code-highlighter/dist/tag-qry", |
| 247 | // ), |
| 248 | // path.join(__dirname, "../out/tag-qry"), |
| 249 | // (error) => { |
| 250 | // if (error) |
| 251 | // console.warn("Error copying code-highlighter tag-qry files", error); |
| 252 | // }, |
| 253 | // ); |
| 254 | |
| 255 | // textmate-syntaxes |
| 256 | await new Promise((resolve, reject) => { |
| 257 | ncp( |
| 258 | path.join(__dirname, "../textmate-syntaxes"), |
| 259 | path.join(__dirname, "../gui/textmate-syntaxes"), |
| 260 | (error) => { |
| 261 | if (error) { |
| 262 | console.warn("[error] Error copying textmate-syntaxes", error); |
| 263 | reject(error); |
| 264 | } else { |
| 265 | resolve(); |
| 266 | } |
| 267 | }, |
| 268 | ); |
| 269 | }); |
| 270 | |
| 271 | if (!skipInstalls) { |
| 272 | // GitHub Actions doesn't support ARM, so we need to download pre-saved binaries |
| 273 | // 02/07/25 - the above comment is out of date, there is now support for ARM runners on GitHub Actions |
| 274 | if (isArmTarget) { |
| 275 | // lancedb binary |
| 276 | const packageToInstall = { |
| 277 | "darwin-arm64": "@lancedb/vectordb-darwin-arm64", |
| 278 | "linux-arm64": "@lancedb/vectordb-linux-arm64-gnu", |
| 279 | "win32-arm64": "@lancedb/vectordb-win32-arm64-msvc", |
| 280 | }[target]; |
| 281 | console.log( |
| 282 | "[info] Downloading pre-built lancedb binary: " + packageToInstall, |
| 283 | ); |
| 284 | |
| 285 | await Promise.all([ |
| 286 | copyEsbuild(target), |
| 287 | copySqlite(target), |
| 288 | installAndCopyNodeModules(packageToInstall, "@lancedb"), |
| 289 | ]); |
| 290 | } else { |
| 291 | // Download esbuild from npm in tmp and copy over |
| 292 | console.log("[info] npm installing esbuild binary"); |
| 293 | await installAndCopyNodeModules("esbuild@0.17.19", "@esbuild"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | console.log("[info] Copying sqlite node binding from core"); |
| 298 | await new Promise((resolve, reject) => { |
| 299 | ncp( |
| 300 | path.join(__dirname, "../../../core/node_modules/sqlite3/build"), |
| 301 | path.join(__dirname, "../out/build"), |
| 302 | { dereference: true }, |
| 303 | (error) => { |
| 304 | if (error) { |
| 305 | console.warn("[error] Error copying sqlite3 files", error); |
| 306 | reject(error); |
| 307 | } else { |
| 308 | resolve(); |
| 309 | } |
| 310 | }, |
| 311 | ); |
| 312 | }); |
| 313 | |
| 314 | // Copied here as well for the VS Code test suite |
| 315 | await new Promise((resolve, reject) => { |
| 316 | ncp( |
| 317 | path.join(__dirname, "../../../core/node_modules/sqlite3/build"), |
| 318 | path.join(__dirname, "../out"), |
| 319 | { dereference: true }, |
| 320 | (error) => { |
| 321 | if (error) { |
| 322 | console.warn("[error] Error copying sqlite3 files", error); |
| 323 | reject(error); |
| 324 | } else { |
| 325 | resolve(); |
| 326 | } |
| 327 | }, |
| 328 | ); |
| 329 | }); |
| 330 | |
| 331 | // Copy node_modules for pre-built binaries |
| 332 | const NODE_MODULES_TO_COPY = [ |
| 333 | "esbuild", |
| 334 | "@esbuild", |
| 335 | "@lancedb", |
| 336 | "@vscode/ripgrep", |
| 337 | "workerpool", |
| 338 | ]; |
| 339 | |
| 340 | fs.mkdirSync("out/node_modules", { recursive: true }); |
| 341 | |
| 342 | await Promise.all( |
| 343 | NODE_MODULES_TO_COPY.map( |
| 344 | (mod) => |
| 345 | new Promise((resolve, reject) => { |
| 346 | fs.mkdirSync(`out/node_modules/${mod}`, { recursive: true }); |
| 347 | ncp( |
| 348 | `node_modules/${mod}`, |
| 349 | `out/node_modules/${mod}`, |
| 350 | { dereference: true }, |
| 351 | function (error) { |
| 352 | if (error) { |
| 353 | console.error(`[error] Error copying ${mod}`, error); |
| 354 | reject(error); |
| 355 | } else { |
| 356 | console.log(`[info] Copied ${mod}`); |
| 357 | resolve(); |
| 358 | } |
| 359 | }, |
| 360 | ); |
| 361 | }), |
| 362 | ), |
| 363 | ); |
| 364 | |
| 365 | // delete esbuild/bin because platform-specific @esbuild is downloaded |
| 366 | fs.rmSync(`out/node_modules/esbuild/bin`, { recursive: true }); |
| 367 | |
| 368 | console.log(`[info] Copied ${NODE_MODULES_TO_COPY.join(", ")}`); |
| 369 | |
| 370 | // Copy over any worker files |
| 371 | fs.cpSync( |
| 372 | "node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", |
| 373 | "out/xhr-sync-worker.js", |
| 374 | ); |
| 375 | |
| 376 | // Validate the all of the necessary files are present |
| 377 | validateFilesPresent([ |
| 378 | // Queries used to create the index for @code context provider |
| 379 | "tree-sitter/code-snippet-queries/c_sharp.scm", |
| 380 | |
| 381 | // Queries used for @outline and @highlights context providers |
| 382 | "tag-qry/tree-sitter-c_sharp-tags.scm", |
| 383 | |
| 384 | // onnx runtime bindngs |
| 385 | `bin/napi-v3/${os}/${arch}/onnxruntime_binding.node`, |
| 386 | `bin/napi-v3/${os}/${arch}/${ |
| 387 | isMacTarget |
| 388 | ? "libonnxruntime.1.14.0.dylib" |
| 389 | : isLinuxTarget |
| 390 | ? "libonnxruntime.so.1.14.0" |
| 391 | : "onnxruntime.dll" |
| 392 | }`, |
| 393 | |
| 394 | // Code/styling for the sidebar |
| 395 | "gui/assets/index.js", |
| 396 | "gui/assets/index.css", |
| 397 | |
| 398 | // Tutorial |
| 399 | "media/move-chat-panel-right.md", |
| 400 | "continue_tutorial.py", |
| 401 | "config_schema.json", |
| 402 | |
| 403 | // Embeddings model |
| 404 | "models/all-MiniLM-L6-v2/config.json", |
| 405 | "models/all-MiniLM-L6-v2/special_tokens_map.json", |
| 406 | "models/all-MiniLM-L6-v2/tokenizer_config.json", |
| 407 | "models/all-MiniLM-L6-v2/tokenizer.json", |
| 408 | "models/all-MiniLM-L6-v2/vocab.txt", |
| 409 | "models/all-MiniLM-L6-v2/onnx/model_quantized.onnx", |
| 410 | |
| 411 | // node_modules (it's a bit confusing why this is necessary) |
| 412 | `node_modules/@vscode/ripgrep/bin/rg${exe}`, |
| 413 | |
| 414 | // out directory (where the extension.js lives) |
| 415 | // "out/extension.js", This is generated afterward by vsce |
| 416 | // web-tree-sitter |
| 417 | "out/tree-sitter.wasm", |
| 418 | // Worker required by jsdom |
| 419 | "out/xhr-sync-worker.js", |
| 420 | // SQLite3 Node native module |
| 421 | "out/build/Release/node_sqlite3.node", |
| 422 | |
| 423 | // out/node_modules (to be accessed by extension.js) |
| 424 | `out/node_modules/@vscode/ripgrep/bin/rg${exe}`, |
| 425 | `out/node_modules/@esbuild/${ |
| 426 | target === "win32-arm64" |
| 427 | ? "esbuild.exe" |
| 428 | : target === "win32-x64" |
| 429 | ? "win32-x64/esbuild.exe" |
| 430 | : `${target}/bin/esbuild` |
| 431 | }`, |
| 432 | `out/node_modules/@lancedb/vectordb-${target}${isWinTarget ? "-msvc" : ""}${isLinuxTarget ? "-gnu" : ""}/index.node`, |
| 433 | `out/node_modules/esbuild/lib/main.js`, |
| 434 | ]); |
| 435 | |
| 436 | process.exit(0); |
| 437 | })(); |