11const esbuild = require ( "esbuild" ) ;
2- const { execSync } = require ( "child_process" ) ;
32const fs = require ( "fs" ) ;
43const path = require ( "path" ) ;
54const ncp = require ( "ncp" ) . ncp ;
65const { rimrafSync } = require ( "rimraf" ) ;
6+ const { validateFilesPresent, execCmdSync } = require ( "../scripts/util" ) ;
77
8- function execCmdSync ( cmd ) {
9- try {
10- execSync ( cmd ) ;
11- } catch ( err ) {
12- console . error ( `Error executing command ' ${ cmd } ': ` , err . output . toString ( ) ) ;
13- process . exit ( 1 ) ;
14- }
15- }
8+ // Clean slate
9+ const bin = path . join ( __dirname , "bin" ) ;
10+ const out = path . join ( __dirname , "out" ) ;
11+ rimrafSync ( bin ) ;
12+ rimrafSync ( out ) ;
13+ rimrafSync ( path . join ( __dirname , "tmp" ) ) ;
14+ fs . mkdirSync ( bin ) ;
15+ fs . mkdirSync ( out ) ;
1616
1717const esbuildOutputFile = "out/index.js" ;
18- const targets = [
18+ let targets = [
1919 "darwin-x64" ,
2020 "darwin-arm64" ,
2121 "linux-x64" ,
@@ -44,38 +44,43 @@ const targetToLanceDb = {
4444 "linux-arm64" : "@lancedb/vectordb-linux-arm64-gnu" ,
4545 "linux-x64" : "@lancedb/vectordb-linux-x64-gnu" ,
4646 "win32-x64" : "@lancedb/vectordb-win32-x64-msvc" ,
47+ "win32-arm64" : "@lancedb/vectordb-win32-x64-msvc" , // they don't have a win32-arm64 build
4748} ;
4849
49- async function installNodeModuleInTempDirAndCopyToCurrent ( package , toCopy ) {
50- console . log ( `Copying ${ package } to ${ toCopy } ` ) ;
50+ async function installNodeModuleInTempDirAndCopyToCurrent ( packageName , toCopy ) {
51+ console . log ( `Copying ${ packageName } to ${ toCopy } ` ) ;
5152 // This is a way to install only one package without npm trying to install all the dependencies
5253 // Create a temporary directory for installing the package
53- const adjustedName = toCopy . replace ( / ^ @ / , "" ) . replace ( "/" , "-" ) ;
54+ const adjustedName = packageName . replace ( / ^ @ / , "" ) . replace ( "/" , "-" ) ;
5455 const tempDir = path . join (
5556 __dirname ,
5657 "tmp" ,
5758 `continue-node_modules-${ adjustedName } ` ,
5859 ) ;
5960 const currentDir = process . cwd ( ) ;
6061
61- // Remove the dir we will be copying to
62- rimrafSync ( `node_modules/${ toCopy } ` ) ;
62+ // // Remove the dir we will be copying to
63+ // rimrafSync(`node_modules/${toCopy}`);
6364
64- // Ensure the temporary directory exists
65+ // // Ensure the temporary directory exists
6566 fs . mkdirSync ( tempDir , { recursive : true } ) ;
6667
6768 try {
6869 // Move to the temporary directory
6970 process . chdir ( tempDir ) ;
7071
7172 // Initialize a new package.json and install the package
72- execCmdSync ( `npm init -y && npm i -f ${ package } --no-save` ) ;
73+ execCmdSync ( `npm init -y && npm i -f ${ packageName } --no-save` ) ;
7374
7475 console . log (
75- `Contents of: ${ package } ` ,
76+ `Contents of: ${ packageName } ` ,
7677 fs . readdirSync ( path . join ( tempDir , "node_modules" , toCopy ) ) ,
7778 ) ;
7879
80+ // Without this it seems the file isn't completely written to disk
81+ // Ideally we validate file integrity in the validation at the end
82+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
83+
7984 // Copy the installed package back to the current directory
8085 await new Promise ( ( resolve , reject ) => {
8186 ncp (
@@ -84,7 +89,10 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) {
8489 { dereference : true } ,
8590 ( error ) => {
8691 if ( error ) {
87- console . error ( `[error] Error copying ${ package } package` , error ) ;
92+ console . error (
93+ `[error] Error copying ${ packageName } package` ,
94+ error ,
95+ ) ;
8896 reject ( error ) ;
8997 } else {
9098 resolve ( ) ;
@@ -102,78 +110,66 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) {
102110}
103111
104112( async ( ) => {
105- // console.log("[info] Building with ncc...");
106- // execCmdSync(`npx ncc build src/index.ts -o out`);
107-
108- // Copy node_modules for pre-built binaries
109- const DYNAMIC_IMPORTS = [
110- // "esbuild",
111- // "@esbuild",
112- // // "@lancedb",
113- // "posthog-node",
114- // "@octokit",
115- ] ;
116113 fs . mkdirSync ( "out/node_modules" , { recursive : true } ) ;
117114 fs . mkdirSync ( "bin/node_modules" , { recursive : true } ) ;
118115
119- await Promise . all (
120- DYNAMIC_IMPORTS . map (
121- ( mod ) =>
122- new Promise ( ( resolve , reject ) => {
123- ncp (
124- `node_modules/${ mod } ` ,
125- `out/node_modules/${ mod } ` ,
126- function ( error ) {
127- if ( error ) {
128- console . error ( `[error] Error copying ${ mod } ` , error ) ;
129- reject ( error ) ;
130- } else {
131- resolve ( ) ;
132- }
133- } ,
134- ) ;
135- ncp (
136- `node_modules/${ mod } ` ,
137- `bin/node_modules/${ mod } ` ,
138- function ( error ) {
139- if ( error ) {
140- console . error ( `[error] Error copying ${ mod } ` , error ) ;
141- reject ( error ) ;
142- } else {
143- resolve ( ) ;
144- }
145- } ,
146- ) ;
147- } ) ,
148- ) ,
149- ) ;
150- console . log ( `[info] Copied ${ DYNAMIC_IMPORTS . join ( ", " ) } ` ) ;
151-
152116 console . log ( "[info] Downloading prebuilt lancedb..." ) ;
153117 for ( const target of targets ) {
154118 if ( targetToLanceDb [ target ] ) {
155- console . log ( `[info] Downloading ${ target } ...` ) ;
119+ console . log ( `[info] Downloading for ${ target } ...` ) ;
156120 await installNodeModuleInTempDirAndCopyToCurrent (
157121 targetToLanceDb [ target ] ,
158122 "@lancedb" ,
159123 ) ;
160124 }
161125 }
162126
127+ // tree-sitter-wasm
128+ const treeSitterWasmsDir = path . join ( out , "tree-sitter-wasms" ) ;
129+ fs . mkdirSync ( treeSitterWasmsDir ) ;
130+ await new Promise ( ( resolve , reject ) => {
131+ ncp (
132+ path . join (
133+ __dirname ,
134+ ".." ,
135+ "core" ,
136+ "node_modules" ,
137+ "tree-sitter-wasms" ,
138+ "out" ,
139+ ) ,
140+ treeSitterWasmsDir ,
141+ { dereference : true } ,
142+ ( error ) => {
143+ if ( error ) {
144+ console . warn ( "[error] Error copying tree-sitter-wasm files" , error ) ;
145+ reject ( error ) ;
146+ } else {
147+ resolve ( ) ;
148+ }
149+ } ,
150+ ) ;
151+ } ) ;
152+
153+ fs . copyFileSync (
154+ path . join ( __dirname , "../core/vendor/tree-sitter.wasm" ) ,
155+ path . join ( __dirname , "out/tree-sitter.wasm" ) ,
156+ ) ;
157+ console . log ( "[info] Copied tree-sitter wasms" ) ;
158+
163159 console . log ( "[info] Cleaning up artifacts from previous builds..." ) ;
164160
165161 // delete asset backups generated by previous pkg invocations, if present
166162 for ( const assetPath of assetBackups ) {
167163 fs . rmSync ( assetPath , { force : true } ) ;
168164 }
169165
170- console . log ( "[info] Building with esbuild..." ) ;
171166 // Bundles the extension into one file
167+ console . log ( "[info] Building with esbuild..." ) ;
172168 await esbuild . build ( {
173169 entryPoints : [ "src/index.ts" ] ,
174170 bundle : true ,
175171 outfile : esbuildOutputFile ,
176- external : [ "esbuild" , ... DYNAMIC_IMPORTS , "./xhr-sync-worker.js" , "vscode" ] ,
172+ external : [ "esbuild" , "./xhr-sync-worker.js" , "vscode" ] ,
177173 format : "cjs" ,
178174 platform : "node" ,
179175 sourcemap : true ,
@@ -208,6 +204,7 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) {
208204 ) ;
209205
210206 // Download and unzip prebuilt sqlite3 binary for the target
207+ console . log ( "[info] Downloading node-sqlite3" ) ;
211208 const downloadUrl = `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${
212209 target === "win32-arm64" ? "win32-ia32" : target
213210 } .tar.gz`;
@@ -240,9 +237,30 @@ async function installNodeModuleInTempDirAndCopyToCurrent(package, toCopy) {
240237 force : true ,
241238 recursive : true ,
242239 } ) ;
240+
241+ // copy @lancedb to bin folders
242+ console . log ( "[info] Copying @lancedb files to bin" ) ;
243+ fs . copyFileSync (
244+ `node_modules/${ targetToLanceDb [ target ] } /index.node` ,
245+ `${ targetDir } /index.node` ,
246+ ) ;
243247 }
244248 // execCmdSync(
245249 // `npx pkg out/index.js --target node18-darwin-arm64 --no-bytecode --public-packages "*" --public -o bin/pkg`
246250 // );
251+
252+ const pathsToVerify = [ ] ;
253+ for ( target of targets ) {
254+ const exe = target . startsWith ( "win" ) ? ".exe" : "" ;
255+ const targetDir = `bin/${ target } ` ;
256+ pathsToVerify . push (
257+ `${ targetDir } /continue-binary${ exe } ` ,
258+ `${ targetDir } /esbuild${ exe } ` ,
259+ `${ targetDir } /index.node` , // @lancedb
260+ `${ targetDir } /node_sqlite3.node` ,
261+ ) ;
262+ }
263+ validateFilesPresent ( pathsToVerify ) ;
264+
247265 console . log ( "[info] Done!" ) ;
248266} ) ( ) ;
0 commit comments