1
1
"use strict" ;
2
+ var __createBinding = ( this && this . __createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
3
+ if ( k2 === undefined ) k2 = k ;
4
+ var desc = Object . getOwnPropertyDescriptor ( m , k ) ;
5
+ if ( ! desc || ( "get" in desc ? ! m . __esModule : desc . writable || desc . configurable ) ) {
6
+ desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
7
+ }
8
+ Object . defineProperty ( o , k2 , desc ) ;
9
+ } ) : ( function ( o , m , k , k2 ) {
10
+ if ( k2 === undefined ) k2 = k ;
11
+ o [ k2 ] = m [ k ] ;
12
+ } ) ) ;
13
+ var __setModuleDefault = ( this && this . __setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
14
+ Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
15
+ } ) : function ( o , v ) {
16
+ o [ "default" ] = v ;
17
+ } ) ;
18
+ var __importStar = ( this && this . __importStar ) || function ( mod ) {
19
+ if ( mod && mod . __esModule ) return mod ;
20
+ var result = { } ;
21
+ if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . prototype . hasOwnProperty . call ( mod , k ) ) __createBinding ( result , mod , k ) ;
22
+ __setModuleDefault ( result , mod ) ;
23
+ return result ;
24
+ } ;
2
25
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3
26
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4
27
} ;
5
28
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
29
const websocket_1 = __importDefault ( require ( "./websocket" ) ) ;
30
+ const fs = __importStar ( require ( "fs" ) ) ;
31
+ const path_1 = __importDefault ( require ( "path" ) ) ;
32
+ const tree_sitter_1 = __importDefault ( require ( "tree-sitter" ) ) ;
33
+ const tree_sitter_javascript_1 = __importDefault ( require ( "tree-sitter-javascript" ) ) ;
7
34
/**
8
35
* A utility module for working with code.
9
36
*/
@@ -14,72 +41,74 @@ const cbcodeutils = {
14
41
* @returns {Promise<GetJsTreeResponse> } A promise that resolves with the JS tree response.
15
42
*/
16
43
getJsTree : ( filePath ) => {
17
- // return new Promise( async (resolve, reject) => {
18
- // cbws.getWebsocket.send(JSON.stringify({
19
- // "type": "settingEvent",
20
- // "action": "getProjectPath"
21
- // }));
22
- // cbws.getWebsocket.on('message', (data: string) => {
23
- // const response = JSON.parse(data);
24
- // if (response.type === "getProjectPathResponse") {
25
- // // resolve(response);
26
- // try {
27
- // let pathInput= response.projectPath;
28
- // let parser= new Parser();
29
- // // Initialize the parser with the JavaScript language
30
- // parser.setLanguage(JavaScript);
31
- // const trees = [];
32
- // const functionNodes = [];
33
- // const processDirectory = (directory:any) => {
34
- // console.log("isdir")
35
- // // Read all files in the directory
36
- // const files = fs.readdirSync(directory, { withFileTypes: true });
37
- // files.forEach(file => {
38
- // if (file.isDirectory()) {
39
- // if (file.name !== 'node_modules') { // Ignore node_modules directory
40
- // processDirectory(path.join(directory, file.name)); // Recursive call for subdirectories
41
- // }
42
- // } else if (path.extname(file.name) === '.js') {
43
- // const code = fs.readFileSync(path.join(directory, file.name), 'utf-8');
44
- // console.log(code);
45
- // let tree:any = parser.parse(code);
46
- // tree.rootNode.path = path.join(directory, file.name); // Set file path for t
47
- // trees.push(tree);
48
- // }
49
- // });
50
- // };
51
- // if (fs.lstatSync(pathInput).isDirectory()) {
52
- // processDirectory(pathInput);
53
- // } else if (path.extname(pathInput) === '.js') {
54
- // // Read a single JavaScript file
55
- // const code = fs.readFileSync(pathInput, 'utf-8');
56
- // let tree:any = parser.parse(code);
57
- // tree.rootNode.path = pathInput; // Set file path for t
58
- // trees.push(tree);
59
- // }
60
- // resolve({ event: 'GetJsTreeResponse',payload:trees}); // Return an array of abstract syntax trees (ASTs)
61
- // } catch (error) {
62
- // console.error('An error occurred:', error);
63
- // return { event: 'GetJsTreeResponse',payload:null}; // Return null in case of error
64
- // }
65
- // }
66
- // });
67
- // });
68
44
return new Promise ( async ( resolve , reject ) => {
69
45
websocket_1 . default . getWebsocket . send ( JSON . stringify ( {
70
- "type" : "codeEvent" ,
71
- "action" : "getJsTree" ,
72
- payload : {
73
- filePath
74
- }
46
+ "type" : "settingEvent" ,
47
+ "action" : "getProjectPath"
75
48
} ) ) ;
76
49
websocket_1 . default . getWebsocket . on ( 'message' , ( data ) => {
77
50
const response = JSON . parse ( data ) ;
78
- if ( response . type === "getJsTreeResponse" ) {
79
- resolve ( response ) ; // Resolve the Promise with the response data
51
+ if ( response . type === "getProjectPathResponse" ) {
52
+ // resolve(response);
53
+ try {
54
+ let pathInput = filePath || response . projectPath ;
55
+ let parser = new tree_sitter_1 . default ( ) ;
56
+ // Initialize the parser with the JavaScript language
57
+ parser . setLanguage ( tree_sitter_javascript_1 . default ) ;
58
+ const trees = [ ] ;
59
+ const functionNodes = [ ] ;
60
+ const processDirectory = ( directory ) => {
61
+ // Read all files in the directory
62
+ const files = fs . readdirSync ( directory , { withFileTypes : true } ) ;
63
+ files . forEach ( file => {
64
+ if ( file . isDirectory ( ) ) {
65
+ if ( file . name !== 'node_modules' ) { // Ignore node_modules directory
66
+ processDirectory ( path_1 . default . join ( directory , file . name ) ) ; // Recursive call for subdirectories
67
+ }
68
+ }
69
+ else if ( path_1 . default . extname ( file . name ) === '.js' ) {
70
+ const code = fs . readFileSync ( path_1 . default . join ( directory , file . name ) , 'utf-8' ) ;
71
+ console . log ( code ) ;
72
+ let tree = parser . parse ( code ) ;
73
+ tree . rootNode . path = path_1 . default . join ( directory , file . name ) ; // Set file path for t
74
+ trees . push ( tree ) ;
75
+ }
76
+ } ) ;
77
+ } ;
78
+ if ( fs . lstatSync ( pathInput ) . isDirectory ( ) ) {
79
+ processDirectory ( pathInput ) ;
80
+ }
81
+ else if ( path_1 . default . extname ( pathInput ) === '.js' ) {
82
+ // Read a single JavaScript file
83
+ const code = fs . readFileSync ( pathInput , 'utf-8' ) ;
84
+ let tree = parser . parse ( code ) ;
85
+ tree . rootNode . path = pathInput ; // Set file path for t
86
+ trees . push ( tree ) ;
87
+ }
88
+ resolve ( { event : 'GetJsTreeResponse' , payload : trees } ) ; // Return an array of abstract syntax trees (ASTs)
89
+ }
90
+ catch ( error ) {
91
+ console . error ( 'An error occurred:' , error ) ;
92
+ return { event : 'GetJsTreeResponse' , payload : null } ; // Return null in case of error
93
+ }
80
94
}
81
95
} ) ;
82
96
} ) ;
97
+ // return new Promise(async (resolve, reject) => {
98
+ // cbws.getWebsocket.send(JSON.stringify({
99
+ // "type": "codeEvent",
100
+ // "action": "getJsTree",
101
+ // payload: {
102
+ // filePath
103
+ // }
104
+ // }));
105
+ // cbws.getWebsocket.on('message', (data: string) => {
106
+ // const response = JSON.parse(data);
107
+ // if (response.type === "getJsTreeResponse") {
108
+ // resolve(response); // Resolve the Promise with the response data
109
+ // }
110
+ // });
111
+ // });
83
112
} ,
84
113
/**
85
114
* Retrieves all files as Markdown.
0 commit comments