Skip to content

Commit ab566d5

Browse files
changes in codebolt
1 parent e0851d3 commit ab566d5

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codebolt/codeboltjs",
3-
"version": "1.1.34",
3+
"version": "1.1.35",
44
"description": "",
55
"keywords": [],
66
"author": "",
@@ -19,6 +19,8 @@
1919
"homepage": "https://codeboltai.github.io",
2020
"dependencies": {
2121
"@codebolt/types": "^1.0.9",
22+
"tree-sitter": "^0.21.1",
23+
"tree-sitter-javascript": "^0.21.2",
2224
"typedoc-plugin-missing-exports": "^2.2.0",
2325
"ws": "^8.17.0"
2426
},

src/modules/codeutils.ts

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import 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+
27
import { 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

Comments
 (0)