-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
210 lines (179 loc) · 7.13 KB
/
Copy pathserver.js
File metadata and controls
210 lines (179 loc) · 7.13 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const express = require('express');
const fs = require('fs');
const path = require('path');
const { get_encoding } = require('tiktoken');
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.static('public')); // Serves our frontend
let encoder = null;
try {
encoder = get_encoding("cl100k_base");
} catch (e) {
console.log("Tiktoken encoding failed to load. Will estimate tokens based on character count.");
}
function countTokens(text) {
if (encoder) return encoder.encode(text).length;
return Math.floor(text.length / 3.5);
}
// Recursively scan the directory
function scanDirectory(dirPath, exts, ignores) {
const result = { name: path.basename(dirPath), path: dirPath, type: 'dir', children:[] };
const items = fs.readdirSync(dirPath, { withFileTypes: true });
for (let item of items) {
if (ignores.includes(item.name)) continue; // Skip global ignores (e.g. node_modules)
const fullPath = path.join(dirPath, item.name);
if (item.isDirectory()) {
const childNode = scanDirectory(fullPath, exts, ignores);
// Only add directory if it contains something matching our criteria
if (childNode.children.length > 0) result.children.push(childNode);
} else {
const ext = path.extname(item.name).toLowerCase();
if (exts.includes(ext) || exts.length === 0) {
result.children.push({ name: item.name, path: fullPath, type: 'file' });
}
}
}
return result;
}
// API: Get folder structure
app.post('/api/scan', (req, res) => {
const { rootPath, extensions, ignores } = req.body;
if (!fs.existsSync(rootPath)) return res.status(400).json({ error: "Invalid path" });
try {
const tree = scanDirectory(rootPath, extensions, ignores);
res.json(tree);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// API: Detect all unique extensions in a directory
app.post('/api/detect-extensions', (req, res) => {
const { rootPath, ignores } = req.body;
if (!fs.existsSync(rootPath)) return res.status(400).json({ error: "Invalid path" });
const extensions = new Set();
function walk(dir) {
const items = fs.readdirSync(dir, { withFileTypes: true });
for (let item of items) {
if (ignores.includes(item.name)) continue;
const fullPath = path.join(dir, item.name);
if (item.isDirectory()) {
walk(fullPath);
} else {
const ext = path.extname(item.name).toLowerCase();
if (ext) extensions.add(ext);
}
}
}
try {
walk(rootPath);
res.json({ extensions: Array.from(extensions).sort() });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
function generateVisualTree(files, rootPath) {
const tree = {};
// Build object tree
files.forEach(file => {
const relPath = path.relative(rootPath, file);
const parts = relPath.split(path.sep);
let current = tree;
parts.forEach(part => {
if (!current[part]) current[part] = {};
current = current[part];
});
});
let output = path.basename(rootPath) + "/\n";
function buildString(node, prefix = "") {
const keys = Object.keys(node).sort();
keys.forEach((key, index) => {
const isLast = index === keys.length - 1;
const connector = isLast ? "└── " : "├── ";
output += prefix + connector + key + (Object.keys(node[key]).length > 0 ? "/" : "") + "\n";
if (Object.keys(node[key]).length > 0) {
const newPrefix = prefix + (isLast ? " " : "│ ");
buildString(node[key], newPrefix);
}
});
}
buildString(tree);
return output;
}
// API: Generate Context
app.post('/api/generate', (req, res) => {
const { files, limit, rootPath } = req.body;
let part = 1;
let currentTokens = 0;
let fileCount = 0;
const structureText = generateVisualTree(files, rootPath);
fs.writeFileSync(path.join(rootPath, `structure.txt`), structureText);
let fContext = fs.openSync(path.join(rootPath, `full_context_part${part}.txt`), 'w');
try {
files.forEach(file => {
const relPath = path.relative(rootPath, file);
const content = fs.readFileSync(file, 'utf-8');
const header = `\n${'='.repeat(60)}\nFILE: ${relPath}\n${'='.repeat(60)}\n\n`;
const fileText = header + content + "\n";
const tks = countTokens(fileText);
if (currentTokens + tks > limit) {
fs.closeSync(fContext);
part++;
currentTokens = 0;
fContext = fs.openSync(path.join(rootPath, `full_context_part${part}.txt`), 'w');
}
fs.writeSync(fContext, fileText);
currentTokens += tks;
fileCount++;
});
fs.closeSync(fContext);
res.json({ message: `Successfully saved ${fileCount} files into ${part} part(s). Tree structure saved to structure.txt` });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// API: Get list of all saved project names
app.get('/api/list-projects', (req, res) => {
try {
if (!fs.existsSync('projects.json')) return res.json([]);
const data = JSON.parse(fs.readFileSync('projects.json', 'utf-8'));
res.json(Object.keys(data)); // Return just the names
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// API: Save a specific project
app.post('/api/save-project', (req, res) => {
const { projectName, config } = req.body;
if (!projectName) return res.status(400).json({ error: "Project name is required" });
try {
let allProjects = {};
if (fs.existsSync('projects.json')) {
allProjects = JSON.parse(fs.readFileSync('projects.json', 'utf-8'));
}
allProjects[projectName] = config;
fs.writeFileSync('projects.json', JSON.stringify(allProjects, null, 2));
res.json({ message: `Project '${projectName}' saved successfully!` });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// API: Load a specific project
app.get('/api/load-project/:name', (req, res) => {
try {
if (!fs.existsSync('projects.json')) return res.status(404).json({ error: "No projects found" });
const allProjects = JSON.parse(fs.readFileSync('projects.json', 'utf-8'));
const config = allProjects[req.params.name];
if (!config) return res.status(404).json({ error: "Project not found" });
res.json(config);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
const { exec } = require('child_process');
app.listen(3000, () => {
console.log('Server running at http://localhost:3000');
// Automatically open the browser
const url = 'http://localhost:3000';
const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
exec(`${start} ${url}`);
});