forked from linuxmobile/commitia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stageScrapper.ts
70 lines (61 loc) · 1.93 KB
/
stageScrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { $ } from "bun";
import { RULES } from "./utils/PROMPT_CONTEXT_RULES.ts";
let globalFilesContent = "";
async function getCurrentDirectory(): Promise<string> {
try {
const result = await $`pwd`.text();
return result.trim();
} catch (error) {
console.error("Error al obtener el directorio actual:", error);
return "";
}
}
async function readFileContent(fullPath: string): Promise<string | null> {
try {
const extension = fullPath.split('.').pop();
const fileName = fullPath.split('/').pop();
let extractFunction: (context: string) => Promise<{ functions?: string[], constants?: string[], keys?: string[] }>;
switch (extension) {
case 'ts':
extractFunction = RULES.TYPESCRIPT;
break;
case 'js':
extractFunction = RULES.JAVASCRIPT;
break;
case 'nix':
extractFunction = RULES.NIX;
break;
case 'json':
extractFunction = RULES.JSON_KEYS;
break;
default:
console.log(`No specific extraction function for .${extension} files, skipping.`);
return null;
}
const content = await Bun.file(fullPath).text();
const result = await extractFunction(content);
const output = {
fileName,
functions: result.functions || [],
constants: result.constants || [],
keys: result.keys || []
};
return JSON.stringify(output, null, 2);
} catch (error) {
console.error("Error al leer el archivo:", error);
return null;
}
}
async function updateGlobalFilesContent(selectedFiles: string[]): Promise<string> {
globalFilesContent = "";
const currentDirectory = await getCurrentDirectory();
for (const file of selectedFiles) {
const fullPath = `${currentDirectory}/${file}`;
const content = await readFileContent(fullPath);
if (content !== null) {
globalFilesContent += content + '\n';
}
}
return globalFilesContent;
}
export { updateGlobalFilesContent };