@codebolt/codeboltjs 1.1.34 → 1.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -4
- package/modules/codeutils.d.ts +1 -1
- package/modules/codeutils.js +85 -8
- package/package.json +3 -1
- package/src/modules/codeutils.ts +67 -10
package/index.d.ts
CHANGED
|
@@ -131,10 +131,7 @@ declare class Codebolt {
|
|
|
131
131
|
executeCommandWithStream(command: string): EventEmitter<[never]>;
|
|
132
132
|
};
|
|
133
133
|
codeutils: {
|
|
134
|
-
getJsTree: (filePath
|
|
135
|
-
* @constructor
|
|
136
|
-
* @description Initializes the websocket connection.
|
|
137
|
-
*/
|
|
134
|
+
getJsTree: (filePath?: string | undefined) => Promise<import("@codebolt/types").GetJsTreeResponse>;
|
|
138
135
|
getAllFilesAsMarkDown: () => Promise<string>;
|
|
139
136
|
performMatch: (matcherDefinition: object, problemPatterns: any[], problems: any[]) => Promise<import("@codebolt/types").MatchProblemResponse>;
|
|
140
137
|
getMatcherList: () => Promise<import("@codebolt/types").GetMatcherListTreeResponse>;
|
package/modules/codeutils.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare const cbcodeutils: {
|
|
|
8
8
|
* @param {string} filePath - The path of the file to retrieve the JS tree for.
|
|
9
9
|
* @returns {Promise<GetJsTreeResponse>} A promise that resolves with the JS tree response.
|
|
10
10
|
*/
|
|
11
|
-
getJsTree: (filePath
|
|
11
|
+
getJsTree: (filePath?: string) => Promise<GetJsTreeResponse>;
|
|
12
12
|
/**
|
|
13
13
|
* Retrieves all files as Markdown.
|
|
14
14
|
* @returns {Promise<string>} A promise that resolves with the Markdown content of all files.
|
package/modules/codeutils.js
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
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,20 +41,70 @@ 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((resolve, reject) => {
|
|
44
|
+
return new Promise(async (resolve, reject) => {
|
|
18
45
|
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
19
|
-
"type": "
|
|
20
|
-
"action": "
|
|
21
|
-
payload: {
|
|
22
|
-
filePath
|
|
23
|
-
}
|
|
46
|
+
"type": "settingEvent",
|
|
47
|
+
"action": "getProjectPath"
|
|
24
48
|
}));
|
|
25
49
|
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
26
50
|
const response = JSON.parse(data);
|
|
27
|
-
if (response.type === "
|
|
28
|
-
resolve(response);
|
|
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
|
+
const tree = parser.parse(code);
|
|
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
|
+
trees.push(tree);
|
|
86
|
+
}
|
|
87
|
+
return trees; // Return an array of abstract syntax trees (ASTs)
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error('An error occurred:', error);
|
|
91
|
+
return null; // Return null in case of error
|
|
92
|
+
}
|
|
29
93
|
}
|
|
30
94
|
});
|
|
95
|
+
// cbws.getWebsocket.send(JSON.stringify({
|
|
96
|
+
// "type": "codeEvent",
|
|
97
|
+
// "action":"getJsTree",
|
|
98
|
+
// payload:{
|
|
99
|
+
// filePath
|
|
100
|
+
// }
|
|
101
|
+
// }));
|
|
102
|
+
// cbws.getWebsocket.on('message', (data: string) => {
|
|
103
|
+
// const response = JSON.parse(data);
|
|
104
|
+
// if (response.type === "getJsTreeResponse") {
|
|
105
|
+
// resolve(response); // Resolve the Promise with the response data
|
|
106
|
+
// }
|
|
107
|
+
// });
|
|
31
108
|
});
|
|
32
109
|
},
|
|
33
110
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebolt/codeboltjs",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.35",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "",
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
"homepage": "https://codeboltai.github.io",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@codebolt/types": "^1.0.9",
|
|
22
|
+
"tree-sitter": "^0.21.1",
|
|
23
|
+
"tree-sitter-javascript": "^0.21.2",
|
|
22
24
|
"typedoc-plugin-missing-exports": "^2.2.0",
|
|
23
25
|
"ws": "^8.17.0"
|
|
24
26
|
},
|
package/src/modules/codeutils.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
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
|
+
|
|
2
7
|
import { GetJsTreeResponse, MatchProblemResponse, GetMatcherListTreeResponse, getMatchDetail } from '@codebolt/types';
|
|
3
8
|
|
|
4
9
|
/**
|
|
@@ -11,21 +16,73 @@ const cbcodeutils = {
|
|
|
11
16
|
* @param {string} filePath - The path of the file to retrieve the JS tree for.
|
|
12
17
|
* @returns {Promise<GetJsTreeResponse>} A promise that resolves with the JS tree response.
|
|
13
18
|
*/
|
|
14
|
-
getJsTree: (filePath
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
19
|
+
getJsTree: (filePath?: string): Promise<GetJsTreeResponse> => {
|
|
20
|
+
return new Promise( async (resolve, reject) => {
|
|
16
21
|
cbws.getWebsocket.send(JSON.stringify({
|
|
17
|
-
"type": "
|
|
18
|
-
"action":"
|
|
19
|
-
payload:{
|
|
20
|
-
filePath
|
|
21
|
-
}
|
|
22
|
+
"type": "settingEvent",
|
|
23
|
+
"action": "getProjectPath"
|
|
22
24
|
}));
|
|
23
25
|
cbws.getWebsocket.on('message', (data: string) => {
|
|
24
26
|
const response = JSON.parse(data);
|
|
25
|
-
if (response.type === "
|
|
26
|
-
resolve(response);
|
|
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
|
+
}
|
|
28
71
|
});
|
|
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
|
+
// });
|
|
29
86
|
});
|
|
30
87
|
},
|
|
31
88
|
|