Elffuss BraidHub

kikocis/continue public

main · 6.5 KiBRawPermalink
1const esbuild = require("esbuild");
2const fs = require("fs");
3const path = require("path");
4const ncp = require("ncp").ncp;
5const { rimrafSync } = require("rimraf");
6const { validateFilesPresent } = require("../scripts/util");
7const { ALL_TARGETS, TARGET_TO_LANCEDB } = require("./utils/targets");
8const { fork } = require("child_process");
9const {
10 installAndCopyNodeModules,
11} = require("../extensions/vscode/scripts/install-copy-nodemodule");
12const { bundleBinary } = require("./utils/bundle-binary");
13
14const bin = path.join(__dirname, "bin");
15const out = path.join(__dirname, "out");
16const build = path.join(__dirname, "build");
17
18function cleanSlate() {
19 // Clean slate
20 rimrafSync(bin);
21 rimrafSync(out);
22 rimrafSync(build);
23 rimrafSync(path.join(__dirname, "tmp"));
24 rimrafSync(path.join(__dirname, "tree-sitter"));
25 fs.mkdirSync(bin);
26 fs.mkdirSync(out);
27 fs.mkdirSync(build);
28}
29
30const esbuildOutputFile = "out/index.js";
31let targets = [...ALL_TARGETS];
32
33const assetBackups = [
34 "node_modules/win-ca/lib/crypt32-ia32.node.bak",
35 "node_modules/win-ca/lib/crypt32-x64.node.bak",
36];
37
38let esbuildOnly = false;
39for (let i = 2; i < process.argv.length; i++) {
40 if (process.argv[i] === "--esbuild-only") {
41 esbuildOnly = true;
42 }
43 if (process.argv[i - 1] === "--target") {
44 targets = [process.argv[i]];
45 }
46}
47
48// Bundles the extension into one file
49async function buildWithEsbuild() {
50 console.log("[info] Building with esbuild...");
51 await esbuild.build({
52 entryPoints: ["src/index.ts"],
53 bundle: true,
54 outfile: esbuildOutputFile,
55 external: [
56 "esbuild",
57 "./xhr-sync-worker.js",
58 "llamaTokenizerWorkerPool.mjs",
59 "tiktokenWorkerPool.mjs",
60 "vscode",
61 "./index.node",
62 ],
63 format: "cjs",
64 platform: "node",
65 sourcemap: true,
66 minify: !esbuildOnly,
67 treeShaking: true,
68 loader: {
69 // eslint-disable-next-line @typescript-eslint/naming-convention
70 ".node": "file",
71 },
72
73 // To allow import.meta.path for transformers.js
74 // https://github.com/evanw/esbuild/issues/1492#issuecomment-893144483
75 inject: ["./importMetaUrl.js"],
76 define: { "import.meta.url": "importMetaUrl" },
77 });
78}
79
80(async () => {
81 if (esbuildOnly) {
82 await buildWithEsbuild();
83 return;
84 }
85
86 cleanSlate();
87
88 // Informs of where to look for node_sqlite3.node https://www.npmjs.com/package/bindings#:~:text=The%20searching%20for,file%20is%20found
89 // This is only needed for our `pkg` command at build time
90 fs.writeFileSync(
91 "out/package.json",
92 JSON.stringify(
93 {
94 name: "binary",
95 version: "1.0.0",
96 author: "Continue Dev, Inc",
97 license: "Apache-2.0",
98 },
99 undefined,
100 2,
101 ),
102 );
103
104 const copyLanceDBPromises = [];
105 for (const target of targets) {
106 if (!TARGET_TO_LANCEDB[target]) {
107 continue;
108 }
109 console.log(`[info] Downloading for ${target}...`);
110 copyLanceDBPromises.push(
111 installAndCopyNodeModules(TARGET_TO_LANCEDB[target], "@lancedb"),
112 );
113 }
114 await Promise.all(copyLanceDBPromises).catch(() => {
115 console.error("[error] Failed to copy LanceDB");
116 process.exit(1);
117 });
118 console.log("[info] Copied all LanceDB");
119
120 // tree-sitter-wasm
121 const treeSitterWasmsDir = path.join(out, "tree-sitter-wasms");
122 fs.mkdirSync(treeSitterWasmsDir);
123 await new Promise((resolve, reject) => {
124 ncp(
125 path.join(
126 __dirname,
127 "..",
128 "core",
129 "node_modules",
130 "tree-sitter-wasms",
131 "out",
132 ),
133 treeSitterWasmsDir,
134 { dereference: true },
135 (error) => {
136 if (error) {
137 console.warn("[error] Error copying tree-sitter-wasm files", error);
138 reject(error);
139 } else {
140 resolve();
141 }
142 },
143 );
144 });
145
146 // copy tree-sitter colder to binary folder to make it available when running in intellij debug mode
147 const treeSitterDir = path.join(__dirname, "tree-sitter");
148 fs.mkdirSync(treeSitterDir);
149 await new Promise((resolve, reject) => {
150 ncp(
151 path.join(__dirname, "..", "extensions", "vscode", "tree-sitter"),
152 treeSitterDir,
153 { dereference: true },
154 (error) => {
155 if (error) {
156 console.warn("[error] Error copying tree-sitter files", error);
157 reject(error);
158 } else {
159 resolve();
160 }
161 },
162 );
163 });
164
165 const filesToCopy = [
166 "../core/vendor/tree-sitter.wasm",
167 "../core/llm/llamaTokenizerWorkerPool.mjs",
168 "../core/llm/llamaTokenizer.mjs",
169 "../core/llm/tiktokenWorkerPool.mjs",
170 ];
171 for (const f of filesToCopy) {
172 fs.copyFileSync(
173 path.join(__dirname, f),
174 path.join(__dirname, "out", path.basename(f)),
175 );
176 console.log(`[info] Copied ${path.basename(f)}`);
177 }
178
179 console.log("[info] Cleaning up artifacts from previous builds...");
180
181 // delete asset backups generated by previous pkg invocations, if present
182 for (const assetPath of assetBackups) {
183 fs.rmSync(assetPath, { force: true });
184 }
185
186 await buildWithEsbuild();
187
188 // Copy over any worker files
189 fs.cpSync(
190 "../core/node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js",
191 "out/xhr-sync-worker.js",
192 );
193 fs.cpSync("../core/llm/tiktokenWorkerPool.mjs", "out/tiktokenWorkerPool.mjs");
194 fs.cpSync(
195 "../core/llm/llamaTokenizerWorkerPool.mjs",
196 "out/llamaTokenizerWorkerPool.mjs",
197 );
198
199 const buildBinaryPromises = [];
200 console.log("[info] Building binaries with pkg...");
201 for (const target of targets) {
202 buildBinaryPromises.push(bundleBinary(target));
203 }
204 await Promise.all(buildBinaryPromises).catch(() => {
205 console.error("[error] Failed to build binaries");
206 process.exit(1);
207 });
208 console.log("[info] All binaries built");
209
210 // Cleanup - this is needed when running locally
211 fs.rmSync("out/package.json");
212
213 const pathsToVerify = [];
214 for (const target of targets) {
215 const exe = target.startsWith("win") ? ".exe" : "";
216 const targetDir = `bin/${target}`;
217 pathsToVerify.push(
218 `${targetDir}/continue-binary${exe}`,
219 `${targetDir}/index.node`, // @lancedb
220 `${targetDir}/build/Release/node_sqlite3.node`,
221 `${targetDir}/rg${exe}`, // ripgrep binary
222 );
223 }
224
225 // Note that this doesn't verify they actually made it into the binary, just that they were in the expected folder before it was built
226 pathsToVerify.push("out/index.js");
227 pathsToVerify.push("out/llamaTokenizerWorkerPool.mjs");
228 pathsToVerify.push("out/tiktokenWorkerPool.mjs");
229 pathsToVerify.push("out/xhr-sync-worker.js");
230 pathsToVerify.push("out/tree-sitter.wasm");
231
232 validateFilesPresent(pathsToVerify);
233
234 console.log("[info] Done!");
235 process.exit(0);
236})();