| 1 | /** |
| 2 | * This is an experimental copy of `prepackage.js` that will attempt to build the extension in a fully cross-platform way. |
| 3 | * This is not what we use for real builds. |
| 4 | * It is also not complete. Current status is that it is just beginning to be refactored. |
| 5 | */ |
| 6 | |
| 7 | const fs = require("fs"); |
| 8 | const path = require("path"); |
| 9 | const { rimrafSync } = require("rimraf"); |
| 10 | const { |
| 11 | validateFilesPresent, |
| 12 | autodetectPlatformAndArch, |
| 13 | } = require("../../../scripts/util/index"); |
| 14 | const { |
| 15 | copyConfigSchema, |
| 16 | installNodeModules, |
| 17 | buildGui, |
| 18 | copyOnnxRuntimeFromNodeModules, |
| 19 | copyTreeSitterWasms, |
| 20 | copyTreeSitterTagQryFiles, |
| 21 | copyNodeModules, |
| 22 | downloadEsbuildBinary, |
| 23 | downloadRipgrepBinary, |
| 24 | copySqliteBinary, |
| 25 | installNodeModuleInTempDirAndCopyToCurrent, |
| 26 | downloadSqliteBinary, |
| 27 | copyTokenizers, |
| 28 | copyScripts, |
| 29 | } = require("./utils"); |
| 30 | |
| 31 | // Clear folders that will be packaged to ensure clean slate |
| 32 | rimrafSync(path.join(__dirname, "..", "bin")); |
| 33 | rimrafSync(path.join(__dirname, "..", "out")); |
| 34 | fs.mkdirSync(path.join(__dirname, "..", "out", "node_modules"), { |
| 35 | recursive: true, |
| 36 | }); |
| 37 | const guiDist = path.join(__dirname, "..", "..", "..", "gui", "dist"); |
| 38 | if (!fs.existsSync(guiDist)) { |
| 39 | fs.mkdirSync(guiDist, { recursive: true }); |
| 40 | } |
| 41 | |
| 42 | // Get the target to package for |
| 43 | let target = undefined; |
| 44 | const args = process.argv; |
| 45 | if (args[2] === "--target") { |
| 46 | target = args[3]; |
| 47 | } |
| 48 | |
| 49 | let os; |
| 50 | let arch; |
| 51 | if (!target) { |
| 52 | [os, arch] = autodetectPlatformAndArch(); |
| 53 | } else { |
| 54 | [os, arch] = target.split("-"); |
| 55 | } |
| 56 | |
| 57 | if (os === "alpine") { |
| 58 | os = "linux"; |
| 59 | } |
| 60 | if (arch === "armhf") { |
| 61 | arch = "arm64"; |
| 62 | } |
| 63 | target = `${os}-${arch}`; |
| 64 | console.log("[info] Using target: ", target); |
| 65 | |
| 66 | const exe = os === "win32" ? ".exe" : ""; |
| 67 | |
| 68 | console.log("[info] Using target: ", target); |
| 69 | |
| 70 | function ghAction() { |
| 71 | return !!process.env.GITHUB_ACTIONS; |
| 72 | } |
| 73 | |
| 74 | function isArm() { |
| 75 | return ( |
| 76 | target === "darwin-arm64" || |
| 77 | target === "linux-arm64" || |
| 78 | target === "win32-arm64" |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | function isWin() { |
| 83 | return target?.startsWith("win"); |
| 84 | } |
| 85 | |
| 86 | async function package(target, os, arch, exe) { |
| 87 | console.log("[info] Packaging extension for target ", target); |
| 88 | |
| 89 | // Copy config_schema to intellij |
| 90 | copyConfigSchema(); |
| 91 | |
| 92 | // Install node_modules |
| 93 | installNodeModules(); |
| 94 | |
| 95 | // Build gui and copy to extensions |
| 96 | await buildGui(ghAction()); |
| 97 | |
| 98 | // Assets |
| 99 | // Copy tree-sitter-wasm files |
| 100 | await copyTreeSitterWasms(); |
| 101 | |
| 102 | // Copy tree-sitter tag query files |
| 103 | await copyTreeSitterTagQryFiles(); |
| 104 | |
| 105 | // Install and copy over native modules |
| 106 | // *** onnxruntime-node *** |
| 107 | await copyOnnxRuntimeFromNodeModules(target); |
| 108 | |
| 109 | // copy llama tokenizers to out |
| 110 | copyTokenizers(); |
| 111 | |
| 112 | // Copy Linux scripts |
| 113 | await copyScripts(); |
| 114 | |
| 115 | // *** Install @lancedb binary *** |
| 116 | const lancePackageToInstall = { |
| 117 | "darwin-arm64": "@lancedb/vectordb-darwin-arm64", |
| 118 | "darwin-x64": "@lancedb/vectordb-darwin-x64", |
| 119 | "linux-arm64": "@lancedb/vectordb-linux-arm64-gnu", |
| 120 | "linux-x64": "@lancedb/vectordb-linux-x64-gnu", |
| 121 | "win32-x64": "@lancedb/vectordb-win32-x64-msvc", |
| 122 | "win32-arm64": "@lancedb/vectordb-win32-x64-msvc", // they don't have a win32-arm64 build |
| 123 | }[target]; |
| 124 | await installNodeModuleInTempDirAndCopyToCurrent( |
| 125 | lancePackageToInstall, |
| 126 | "@lancedb", |
| 127 | ); |
| 128 | // *** esbuild *** |
| 129 | // await installNodeModuleInTempDirAndCopyToCurrent( |
| 130 | // "esbuild@0.17.19", |
| 131 | // "@esbuild", |
| 132 | // ); |
| 133 | await downloadEsbuildBinary(target); |
| 134 | |
| 135 | // *** sqlite *** |
| 136 | await downloadSqliteBinary(target); |
| 137 | await copySqliteBinary(); |
| 138 | |
| 139 | await downloadRipgrepBinary(target); |
| 140 | |
| 141 | // copy node_modules to out/node_modules |
| 142 | await copyNodeModules(); |
| 143 | |
| 144 | // Copy over any worker files |
| 145 | fs.cpSync( |
| 146 | "node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", |
| 147 | "out/xhr-sync-worker.js", |
| 148 | ); |
| 149 | |
| 150 | // Validate the all of the necessary files are present |
| 151 | validateFilesPresent([ |
| 152 | // Queries used to create the index for @code context provider |
| 153 | "tree-sitter/code-snippet-queries/c_sharp.scm", |
| 154 | |
| 155 | // Queries used for @outline and @highlights context providers |
| 156 | "tag-qry/tree-sitter-c_sharp-tags.scm", |
| 157 | |
| 158 | // onnx runtime bindngs |
| 159 | `bin/napi-v3/${os}/${arch}/onnxruntime_binding.node`, |
| 160 | `bin/napi-v3/${os}/${arch}/${ |
| 161 | os === "darwin" |
| 162 | ? "libonnxruntime.1.14.0.dylib" |
| 163 | : os === "linux" |
| 164 | ? "libonnxruntime.so.1.14.0" |
| 165 | : "onnxruntime.dll" |
| 166 | }`, |
| 167 | |
| 168 | // Code/styling for the sidebar |
| 169 | "gui/assets/index.js", |
| 170 | "gui/assets/index.css", |
| 171 | |
| 172 | // Tutorial |
| 173 | "media/move-chat-panel-right.md", |
| 174 | "continue_tutorial.py", |
| 175 | "config_schema.json", |
| 176 | |
| 177 | // Embeddings model |
| 178 | "models/all-MiniLM-L6-v2/config.json", |
| 179 | "models/all-MiniLM-L6-v2/special_tokens_map.json", |
| 180 | "models/all-MiniLM-L6-v2/tokenizer_config.json", |
| 181 | "models/all-MiniLM-L6-v2/tokenizer.json", |
| 182 | "models/all-MiniLM-L6-v2/vocab.txt", |
| 183 | "models/all-MiniLM-L6-v2/onnx/model_quantized.onnx", |
| 184 | |
| 185 | // node_modules (it's a bit confusing why this is necessary) |
| 186 | `node_modules/@vscode/ripgrep/bin/rg${exe}`, |
| 187 | |
| 188 | // out directory (where the extension.js lives) |
| 189 | // "out/extension.js", This is generated afterward by vsce |
| 190 | // web-tree-sitter |
| 191 | "out/tree-sitter.wasm", |
| 192 | // Worker required by jsdom |
| 193 | "out/xhr-sync-worker.js", |
| 194 | // SQLite3 Node native module |
| 195 | "out/build/Release/node_sqlite3.node", |
| 196 | |
| 197 | // out/node_modules (to be accessed by extension.js) |
| 198 | `out/node_modules/@vscode/ripgrep/bin/rg${exe}`, |
| 199 | `out/node_modules/@esbuild/${ |
| 200 | target === "win32-arm64" |
| 201 | ? "esbuild.exe" |
| 202 | : target === "win32-x64" |
| 203 | ? "win32-x64/esbuild.exe" |
| 204 | : `${target}/bin/esbuild` |
| 205 | }`, |
| 206 | `out/node_modules/@lancedb/vectordb-${ |
| 207 | os === "win32" |
| 208 | ? "win32-x64-msvc" |
| 209 | : `${target}${os === "linux" ? "-gnu" : ""}` |
| 210 | }/index.node`, |
| 211 | `out/node_modules/esbuild/lib/main.js`, |
| 212 | ]); |
| 213 | } |
| 214 | |
| 215 | void (async () => { |
| 216 | await package(target, os, arch, exe); |
| 217 | })(); |