11"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+ } ;
225var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
326 return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
427} ;
528Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
629const 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" ) ) ;
734/**
835 * A utility module for working with code.
936 */
@@ -16,19 +43,73 @@ const cbcodeutils = {
1643 getJsTree : ( filePath ) => {
1744 return new Promise ( async ( resolve , reject ) => {
1845 websocket_1 . default . getWebsocket . send ( JSON . stringify ( {
19- "type" : "codeEvent" ,
20- "action" : "getJsTree" ,
21- payload : {
22- filePath
23- }
46+ "type" : "settingEvent" ,
47+ "action" : "getProjectPath"
2448 } ) ) ;
2549 websocket_1 . default . getWebsocket . on ( 'message' , ( data ) => {
2650 const response = JSON . parse ( data ) ;
27- if ( response . type === "getJsTreeResponse" ) {
28- resolve ( response ) ; // Resolve the Promise with the response data
51+ if ( response . type === "getProjectPathResponse" ) {
52+ // resolve(response);
53+ try {
54+ let pathInput = 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+ console . log ( "isdir" ) ;
62+ // Read all files in the directory
63+ const files = fs . readdirSync ( directory , { withFileTypes : true } ) ;
64+ files . forEach ( file => {
65+ if ( file . isDirectory ( ) ) {
66+ if ( file . name !== 'node_modules' ) { // Ignore node_modules directory
67+ processDirectory ( path_1 . default . join ( directory , file . name ) ) ; // Recursive call for subdirectories
68+ }
69+ }
70+ else if ( path_1 . default . extname ( file . name ) === '.js' ) {
71+ const code = fs . readFileSync ( path_1 . default . join ( directory , file . name ) , 'utf-8' ) ;
72+ console . log ( code ) ;
73+ let tree = parser . parse ( code ) ;
74+ tree . rootNode . path = path_1 . default . join ( directory , file . name ) ; // Set file path for t
75+ trees . push ( tree ) ;
76+ }
77+ } ) ;
78+ } ;
79+ if ( fs . lstatSync ( pathInput ) . isDirectory ( ) ) {
80+ processDirectory ( pathInput ) ;
81+ }
82+ else if ( path_1 . default . extname ( pathInput ) === '.js' ) {
83+ // Read a single JavaScript file
84+ const code = fs . readFileSync ( pathInput , 'utf-8' ) ;
85+ let tree = parser . parse ( code ) ;
86+ tree . rootNode . path = pathInput ; // Set file path for t
87+ trees . push ( tree ) ;
88+ }
89+ resolve ( { event : 'GetJsTreeResponse' , payload : trees } ) ; // Return an array of abstract syntax trees (ASTs)
90+ }
91+ catch ( error ) {
92+ console . error ( 'An error occurred:' , error ) ;
93+ return { event : 'GetJsTreeResponse' , payload : null } ; // Return null in case of error
94+ }
2995 }
3096 } ) ;
3197 } ) ;
98+ // return new Promise(async (resolve, reject) => {
99+ // cbws.getWebsocket.send(JSON.stringify({
100+ // "type": "codeEvent",
101+ // "action": "getJsTree",
102+ // payload: {
103+ // filePath
104+ // }
105+ // }));
106+ // cbws.getWebsocket.on('message', (data: string) => {
107+ // const response = JSON.parse(data);
108+ // if (response.type === "getJsTreeResponse") {
109+ // resolve(response); // Resolve the Promise with the response data
110+ // }
111+ // });
112+ // });
32113 } ,
33114 /**
34115 * Retrieves all files as Markdown.
0 commit comments