11import cbws from './websocket' ;
2+ import * as fs from 'fs' ;
3+ import path from 'path' ;
4+ import Parser from 'tree-sitter' ;
5+ import JavaScript from 'tree-sitter-javascript' ;
6+
27import { GetJsTreeResponse , MatchProblemResponse , GetMatcherListTreeResponse , getMatchDetail } from '@codebolt/types' ;
38
49/**
@@ -11,21 +16,73 @@ const cbcodeutils = {
1116 * @param {string } filePath - The path of the file to retrieve the JS tree for.
1217 * @returns {Promise<GetJsTreeResponse> } A promise that resolves with the JS tree response.
1318 */
14- getJsTree : ( filePath : string ) : Promise < GetJsTreeResponse > => {
15- return new Promise ( ( resolve , reject ) => {
19+ getJsTree : ( filePath ? : string ) : Promise < GetJsTreeResponse > => {
20+ return new Promise ( async ( resolve , reject ) => {
1621 cbws . getWebsocket . send ( JSON . stringify ( {
17- "type" : "codeEvent" ,
18- "action" :"getJsTree" ,
19- payload :{
20- filePath
21- }
22+ "type" : "settingEvent" ,
23+ "action" : "getProjectPath"
2224 } ) ) ;
2325 cbws . getWebsocket . on ( 'message' , ( data : string ) => {
2426 const response = JSON . parse ( data ) ;
25- if ( response . type === "getJsTreeResponse" ) {
26- resolve ( response ) ; // Resolve the Promise with the response data
27- }
27+ if ( response . type === "getProjectPathResponse" ) {
28+ // resolve(response);
29+ try {
30+ let pathInput = response . projectPath ;
31+ let parser = new Parser ( ) ;
32+ // Initialize the parser with the JavaScript language
33+ parser . setLanguage ( JavaScript ) ;
34+ const trees = [ ] ;
35+ const functionNodes = [ ] ;
36+ const processDirectory = ( directory :any ) => {
37+ console . log ( "isdir" )
38+ // Read all files in the directory
39+ const files = fs . readdirSync ( directory , { withFileTypes : true } ) ;
40+
41+ files . forEach ( file => {
42+ if ( file . isDirectory ( ) ) {
43+ if ( file . name !== 'node_modules' ) { // Ignore node_modules directory
44+ processDirectory ( path . join ( directory , file . name ) ) ; // Recursive call for subdirectories
45+ }
46+ } else if ( path . extname ( file . name ) === '.js' ) {
47+ const code = fs . readFileSync ( path . join ( directory , file . name ) , 'utf-8' ) ;
48+ console . log ( code ) ;
49+ const tree = parser . parse ( code ) ;
50+ trees . push ( tree ) ;
51+ }
52+ } ) ;
53+ } ;
54+
55+ if ( fs . lstatSync ( pathInput ) . isDirectory ( ) ) {
56+ processDirectory ( pathInput ) ;
57+ } else if ( path . extname ( pathInput ) === '.js' ) {
58+ // Read a single JavaScript file
59+ const code = fs . readFileSync ( pathInput , 'utf-8' ) ;
60+ let tree = parser . parse ( code ) ;
61+
62+ trees . push ( tree ) ;
63+ }
64+
65+ return trees ; // Return an array of abstract syntax trees (ASTs)
66+ } catch ( error ) {
67+ console . error ( 'An error occurred:' , error ) ;
68+ return null ; // Return null in case of error
69+ }
70+ }
2871 } ) ;
72+
73+ // cbws.getWebsocket.send(JSON.stringify({
74+ // "type": "codeEvent",
75+ // "action":"getJsTree",
76+ // payload:{
77+ // filePath
78+ // }
79+ // }));
80+ // cbws.getWebsocket.on('message', (data: string) => {
81+ // const response = JSON.parse(data);
82+ // if (response.type === "getJsTreeResponse") {
83+ // resolve(response); // Resolve the Promise with the response data
84+ // }
85+ // });
2986 } ) ;
3087 } ,
3188
0 commit comments