1
1
const { exec } = require ( 'child_process' ) ;
2
- const fs = require ( 'fs' ) ;
3
2
const path = require ( 'path' ) ;
4
- const tar = require ( 'tar' ) ;
5
3
const fsExtra = require ( 'fs-extra' )
4
+ const os = require ( 'os' ) ;
5
+ const { rimrafSync } = require ( 'rimraf' )
6
6
7
-
8
- function buildLib ( { command, cwd, destination} ) {
7
+ async function buildWasm ( { languageName} ) {
8
+ const command = 'npx tree-sitter build-wasm .'
9
+ const cwd = path . join ( __dirname , `./node_modules/tree-sitter-${ languageName } ` )
10
+ const destination = path . join ( __dirname , './src/assets/wasm' )
9
11
return new Promise ( ( resolve , reject ) => {
10
12
exec ( command , { cwd } , ( error , stdout , stderr ) => {
11
13
if ( error ) {
12
14
return reject ( `执行的命令出错: ${ error } ` )
13
15
}
14
16
15
- console . log ( `stdout: ${ stdout } ` ) ;
16
- console . error ( `stderr: ${ stderr } ` ) ;
17
-
18
- // 找到生成的压缩包
19
- const prebuildsDir = path . join ( cwd , 'prebuilds' ) ;
20
- fs . readdir ( prebuildsDir , ( err , files ) => {
21
- if ( err ) {
22
- return reject ( `读取目录出错: ${ err } ` )
23
- }
24
-
25
- // 找到 .tar.gz 文件
26
- const tarFile = files . find ( file => file . endsWith ( '.tar.gz' ) ) ;
27
- if ( ! tarFile ) {
28
- return reject ( '没有找到 .tar.gz 文件' )
29
- }
17
+ console . log ( `stdout: tree-sitter-${ languageName } generate success` ) ;
30
18
31
- // 解压缩文件
32
- tar . x ( {
33
- file : path . join ( prebuildsDir , tarFile ) ,
34
- C : prebuildsDir ,
35
- } , err => {
36
- if ( err ) {
37
- return reject ( `解压缩文件出错: ${ err } ` )
38
- }
39
- fsExtra . copy (
40
- path . join ( prebuildsDir , 'build' , 'Release' ) ,
41
- destination
42
- ) . then ( resolve )
43
- } ) ;
44
- } ) ;
19
+ const wasmFile = `tree-sitter-${ languageName } .wasm`
20
+ fsExtra . copy (
21
+ path . join ( path . join ( cwd , wasmFile ) ) ,
22
+ path . join ( destination , wasmFile )
23
+ ) . then ( resolve )
45
24
} ) ;
46
25
} )
47
26
}
@@ -54,47 +33,43 @@ async function serialPrommise(taskFunc = []){
54
33
} , Promise . resolve ( ) )
55
34
}
56
35
57
- const buildNodeModulesLib = ( ) => {
58
- return [ 'javascript' , 'cpp' , 'go' , 'java' , 'php' , 'python' , 'ruby' , 'rust' , 'scala' , 'markdown' , 'html' , 'solidity' , 'kotlin' ] . map ( language => {
59
- return buildLib ( {
60
- command : 'npx prebuild -r electron -t 26.0.0 --strip' ,
61
- cwd : path . join ( __dirname , 'node_modules' , `tree-sitter-${ language } ` ) ,
62
- destination : path . join ( __dirname , 'src' , 'assets' , 'node' , `tree-sitter-${ language } ` )
63
- } )
64
- } )
65
- }
66
-
67
- const copyNodeTypes = ( ) => {
68
- return [ 'javascript' , 'cpp' , 'go' , 'java' , 'php' , 'python' , 'ruby' , 'rust' , 'scala' , 'markdown' , 'html' , 'solidity' , 'kotlin' ] . map ( language => {
36
+ const buildFaiss = async function ( ) {
37
+ const copyFaiss = async ( ) => {
69
38
return fsExtra . copy (
70
- path . join ( __dirname , 'node_modules' , `tree-sitter- ${ language } ` , 'src ', 'node-types.json ' ) ,
71
- path . join ( __dirname , 'src' , 'assets' , 'node' , `tree-sitter- ${ language } ` , 'node-types.json ' )
39
+ path . join ( __dirname , 'node_modules' , 'faiss-node' , 'build ', 'Release ' ) ,
40
+ path . join ( __dirname , 'src' , 'assets' , 'node' , 'faiss-node' , 'build ' )
72
41
)
73
- } )
42
+ }
43
+ if ( os . platform ( ) === 'darwin' ) {
44
+ return new Promise ( ( resolve , reject ) => {
45
+ rimrafSync (
46
+ path . join ( __dirname , 'node_modules' , 'faiss-node' , 'deps' , 'faiss' )
47
+ )
48
+ const command = 'git clone -b v1.7.4 --depth 1 https://github.com/facebookresearch/faiss.git deps/faiss && npm i cmake-js && npm run build'
49
+ exec ( command , { cwd : path . join ( __dirname , 'node_modules' , 'faiss-node' ) } , ( error , stdout , stderr ) => {
50
+ if ( error ) {
51
+ return reject ( `执行的命令出错: ${ error } ` )
52
+ }
53
+ copyFaiss ( ) . then ( resolve )
54
+ } ) ;
55
+ } )
56
+ }
57
+ else {
58
+ await copyFaiss ( )
59
+ }
74
60
}
75
61
76
- const taskFunc = [
77
- ( ) => {
78
- return Promise . all ( [
79
- buildLib ( {
80
- command : 'npx prebuild -r electron -t 26.0.0 --strip' ,
81
- cwd : path . join ( __dirname , 'src' , 'deps' , 'tree-sitter' ) ,
82
- destination : path . join ( __dirname , 'src' , 'assets' , 'node' , 'tree-sitter' )
83
- } ) ,
84
- ...buildNodeModulesLib ( )
85
- ] )
86
- } ,
87
- ( ) => {
88
- return Promise . all ( [
89
- fsExtra . copy (
90
- path . join ( __dirname , 'node_modules' , 'faiss-node' , 'build' , 'Release' ) ,
91
- path . join ( __dirname , 'src' , 'assets' , 'node' , 'faiss-node' , 'build' )
92
- ) ,
93
- ...copyNodeTypes ( )
94
- ] )
95
- }
96
- ]
62
+ // const taskFunc = [
63
+ // buildFaiss,
64
+ // ()=>{
65
+ // return Promise.all(
66
+ // ['javascript','cpp','go','java','php','python','ruby','rust','scala','markdown','html','solidity','kotlin'].map(language=> buildWasm({languageName: language}))
67
+ // )
68
+ // }
69
+ // ]
70
+ //
71
+ // serialPrommise(taskFunc).then().catch(err=>{
72
+ // console.log(err);
73
+ // })
97
74
98
- serialPrommise ( taskFunc ) . then ( ) . catch ( err => {
99
- console . log ( err ) ;
100
- } )
75
+ buildFaiss ( ) . then ( )
0 commit comments