-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode-files.js
More file actions
530 lines (471 loc) · 17.4 KB
/
node-files.js
File metadata and controls
530 lines (471 loc) · 17.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// node-files.js
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { execSync } = require('child_process');
const {db} = require('./node-config');
const { get_project_settings, is_path_excluded } = require('./node-projects');
function resolve_path(relative_path, project_full_path) {
const full_path = path.resolve(project_full_path, relative_path);
if (!full_path.startsWith(project_full_path)) {
throw new Error("Invalid path traversal attempt.");
}
return full_path;
}
function calculate_checksum(data) {
return crypto.createHash('sha256').update(data).digest('hex');
}
function get_folders({ input_path, project_path }) {
const settings = get_project_settings(project_path);
const { allowed_extensions } = settings;
const full_path = resolve_path(input_path, project_path);
const folders = [];
const files = [];
const metadata_stmt = db.prepare('SELECT file_path, last_checksum FROM file_metadata WHERE project_path = ?');
const analyzed_files_map = new Map(metadata_stmt.all(project_path).map(r => [r.file_path, r.last_checksum]));
const modifiedGitFiles = getGitModifiedFiles(project_path);
try {
const items = fs.readdirSync(full_path);
for (const item of items) {
if (item === '.' || item === '..') continue;
const item_full_path = path.join(full_path, item);
let stats;
try {
stats = fs.statSync(item_full_path);
} catch (e) {
console.warn(`Skipping ${item_full_path}: ${e.message}`);
continue;
}
if (stats.isDirectory()) {
folders.push(item);
} else if (stats.isFile()) {
const ext = path.extname(item_full_path).slice(1);
let base = path.basename(item_full_path);
if (base.startsWith('.')) {
base = base.slice(1);
}
if (allowed_extensions.includes(ext) || (ext === '' && allowed_extensions.includes(base))) {
const relative_file_path = path.join(input_path, item).replace(/\\/g, '/');
const has_analysis = analyzed_files_map.has(relative_file_path);
let needs_reanalysis = false;
const has_git_diff = modifiedGitFiles.has(relative_file_path);
let file_content_buffer;
try {
file_content_buffer = fs.readFileSync(item_full_path);
} catch (read_error) {
console.warn(`Could not read file, skipping: ${item_full_path}`, read_error);
continue;
}
if (has_analysis) {
const stored_checksum = analyzed_files_map.get(relative_file_path);
if (stored_checksum) {
const current_checksum = calculate_checksum(file_content_buffer);
if (current_checksum !== stored_checksum) {
needs_reanalysis = true;
}
}
}
files.push({
name: item,
path: relative_file_path,
has_analysis: has_analysis,
needs_reanalysis: needs_reanalysis,
has_git_diff: has_git_diff,
size: stats.size
});
}
}
}
} catch (error) {
console.error(`Error reading directory ${full_path}:`, error);
return {folders: [], files: []};
}
folders.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }));
files.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
return {folders, files};
}
function get_file_content(input_path, project_path) {
const full_path = resolve_path(input_path, project_path);
try {
let file_contents = fs.readFileSync(full_path, 'utf8');
return {content: file_contents};
} catch (error) {
console.error(`Error reading file ${full_path}:`, error);
throw new Error(`Could not read file: ${input_path}`);
}
}
function save_file_content({ project_path, file_path, content }) {
const full_path = resolve_path(file_path, project_path);
try {
fs.writeFileSync(full_path, content, 'utf8');
const stats = fs.statSync(full_path);
return { success: true, mtimeMs: stats.mtimeMs };
} catch (error) {
console.error(`Error writing file ${full_path}:`, error);
throw new Error(`Could not save file: ${file_path}`);
}
}
function get_raw_file_content(input_path, project_path) {
const full_path = resolve_path(input_path, project_path);
return fs.readFileSync(full_path, 'utf8');
}
function isGitRepository(project_path) {
return fs.existsSync(path.join(project_path, '.git'));
}
function getGitHeadContent(relative_path, project_full_path) {
const sanitized_path = relative_path.replace(/\\/g, '/');
if (sanitized_path.includes('"') || sanitized_path.includes(';')) {
console.error(`Invalid characters in file path for git command: ${sanitized_path}`);
return null;
}
try {
const gitCommand = `git show HEAD:"${sanitized_path}"`;
const stdout = execSync(gitCommand, { cwd: project_full_path, encoding: 'utf8', timeout: 2000 });
return stdout;
} catch (error) {
return null;
}
}
function getGitModifiedFiles(project_full_path) {
if (!isGitRepository(project_full_path)) {
return new Set();
}
try {
const gitCommand = 'git diff --name-only HEAD -z';
const stdout = execSync(gitCommand, { cwd: project_full_path, encoding: 'utf8', timeout: 5000 });
const modifiedFiles = stdout.split('\0').filter(p => p.length > 0);
return new Set(modifiedFiles);
} catch (error) {
console.warn(`Could not get git diff for ${project_full_path}: ${error.message}`);
return new Set();
}
}
function get_file_for_editor({ project_path, file_path }) {
let currentContent;
let mtimeMs;
try {
const full_path = resolve_path(file_path, project_path);
const stats = fs.statSync(full_path);
mtimeMs = stats.mtimeMs;
currentContent = fs.readFileSync(full_path, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
console.warn(`File not found while opening for editor: ${file_path}`);
return { currentContent: null, originalContent: null, mtimeMs: null };
}
console.error(`Error reading file for editor ${file_path}:`, e);
return { currentContent: `/* ERROR: Could not read file: ${file_path}. Reason: ${e.message} */`, originalContent: null, mtimeMs: null };
}
let originalContent = null;
if (isGitRepository(project_path)) {
originalContent = getGitHeadContent(file_path, project_path);
}
if (originalContent && currentContent.replace(/\r/g, '') === originalContent.replace(/\r/g, '')) {
originalContent = null;
}
return { currentContent, originalContent, mtimeMs };
}
function get_file_mtime({ project_path, file_path }) {
try {
const full_path = resolve_path(file_path, project_path);
if (!fs.existsSync(full_path)) {
return { mtimeMs: null, exists: false };
}
const stats = fs.statSync(full_path);
return { mtimeMs: stats.mtimeMs, exists: true };
} catch (error) {
console.warn(`Could not get mtime for ${file_path}:`, error.message);
return { mtimeMs: null, exists: false };
}
}
function search_files({ start_path, search_term, project_path }) {
const settings = get_project_settings(project_path);
const { allowed_extensions } = settings;
const absolute_start_path = resolve_path(start_path, project_path);
const matching_files = [];
const search_regex = new RegExp(search_term.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi');
function search_in_directory(current_dir) {
let items;
try {
items = fs.readdirSync(current_dir);
} catch (err) {
console.warn(`Cannot read directory ${current_dir}: ${err.message}`);
return;
}
for (const item of items) {
if (item === '.' || item === '..') continue;
const item_full_path = path.join(current_dir, item);
let stats;
try {
stats = fs.statSync(item_full_path);
} catch (e) {
console.warn(`Skipping ${item_full_path}: ${e.message}`);
continue;
}
if (stats.isDirectory()) {
if (!is_path_excluded(path.relative(project_path, item_full_path), settings)) {
search_in_directory(item_full_path);
}
} else if (stats.isFile()) {
const ext = path.extname(item_full_path).slice(1);
if (allowed_extensions.includes(ext)) {
try {
const content = fs.readFileSync(item_full_path, 'utf8');
const matches = content.match(search_regex);
if (matches && matches.length > 0) {
const relative_path = path.relative(project_path, item_full_path).replace(/\\/g, '/');
matching_files.push({path: relative_path, match_count: matches.length});
}
} catch (err) {
console.warn(`Cannot read file ${item_full_path}: ${err.message}`);
}
}
}
}
}
search_in_directory(absolute_start_path);
return {matching_files};
}
function get_file_analysis({project_path, file_path}) {
const data = db.prepare('SELECT file_overview, functions_overview FROM file_metadata WHERE project_path = ? AND file_path = ?')
.get(project_path, file_path);
return data || {file_overview: null, functions_overview: null};
}
/**
* Recursively walks the project directory to get a set of all valid, non-excluded file paths.
* @param {string} project_path - The absolute path to the project root.
* @param {object} settings - The project's settings object.
* @returns {Set<string>} A set of relative file paths.
*/
function get_all_project_files(project_path, settings) {
const { allowed_extensions } = settings;
const all_files = new Set();
function walk(directory) {
let items;
try {
items = fs.readdirSync(directory);
} catch (e) {
console.warn(`Could not read directory, skipping: ${directory}`);
return;
}
for (const item of items) {
const full_path = path.join(directory, item);
const relative_path = path.relative(project_path, full_path).replace(/\\/g, '/');
if (is_path_excluded(relative_path, settings)) {
continue;
}
let stats;
try {
stats = fs.statSync(full_path);
} catch (e) {
continue;
}
if (stats.isDirectory()) {
walk(full_path);
} else if (stats.isFile()) {
const ext = path.extname(item).slice(1);
let base = path.basename(item);
if (base.startsWith('.')) base = base.slice(1);
if (allowed_extensions.includes(ext) || (ext === '' && allowed_extensions.includes(base))) {
all_files.add(relative_path);
}
}
}
}
walk(project_path);
return all_files;
}
/**
* Checks for file system changes by comparing the file system against a stored snapshot.
* This correctly identifies additions and deletions without being affected by UI state.
* @param {object} params - The parameters.
* @param {string} params.project_path - The full path of the project.
* @returns {object} An object containing arrays of added, deleted, and updated files.
*/
function check_folder_updates({ project_path }) {
// 1. Get the last known file list from the database snapshot.
const state_row = db.prepare('SELECT file_list_snapshot FROM project_states WHERE project_path = ?').get(project_path);
const previous_files = new Set(state_row?.file_list_snapshot ? JSON.parse(state_row.file_list_snapshot) : []);
// 2. Get the current list of all valid files from the filesystem.
const settings = get_project_settings(project_path);
const current_files_set = get_all_project_files(project_path, settings);
// 3. Compare the current list against the previous list to find true additions and deletions.
const added_files_paths = [...current_files_set].filter(p => !previous_files.has(p));
const deleted_files_paths = [...previous_files].filter(p => !current_files_set.has(p));
// 4. If there were any structural changes, update the snapshot in the database for the next poll.
if (added_files_paths.length > 0 || deleted_files_paths.length > 0) {
db.prepare('INSERT OR IGNORE INTO project_states (project_path) VALUES (?)').run(project_path);
db.prepare('UPDATE project_states SET file_list_snapshot = ? WHERE project_path = ?')
.run(JSON.stringify([...current_files_set]), project_path);
}
// 5. Check for modification status (reanalysis needed, git diff) on all current files.
const updates = [];
const db_files_stmt = db.prepare('SELECT file_path, last_checksum FROM file_metadata WHERE project_path = ?');
const db_files_map = new Map(db_files_stmt.all(project_path).map(r => [r.file_path, r.last_checksum]));
const modifiedGitFiles = getGitModifiedFiles(project_path);
for (const file_path of current_files_set) {
let needs_reanalysis = false;
const has_git_diff = modifiedGitFiles.has(file_path);
// Check for content modification only if the file has been analyzed before.
if (db_files_map.has(file_path)) {
try {
const full_path = resolve_path(file_path, project_path);
const file_content = fs.readFileSync(full_path);
const current_checksum = calculate_checksum(file_content);
const stored_checksum = db_files_map.get(file_path);
if (current_checksum !== stored_checksum) {
needs_reanalysis = true;
}
} catch (error) {
// File might have been deleted between scanning and reading, handle gracefully.
if (error.code !== 'ENOENT') {
console.error(`Error checking file for modification during poll: ${file_path}`, error);
}
}
}
// Always send the current status so the UI can add OR remove icons as needed.
updates.push({
file_path: file_path,
needs_reanalysis: needs_reanalysis,
has_git_diff: has_git_diff
});
}
// 6. Format added files with details needed by the frontend to render them.
const added_files_details = added_files_paths.map(p => ({
path: p,
name: path.basename(p),
parent_path: path.dirname(p).replace(/\\/g, '/') || '.'
}));
return {
updates: updates,
deleted: deleted_files_paths,
added: added_files_details
};
}
function check_for_modified_files({project_path}) {
const project_settings = get_project_settings(project_path);
const stmt = db.prepare('SELECT file_path, last_checksum FROM file_metadata WHERE project_path = ?');
const all_analyzed_files = stmt.all(project_path);
const analyzed_files = all_analyzed_files.filter(
file => !is_path_excluded(file.file_path, project_settings)
);
if (analyzed_files.length === 0) {
return {needs_reanalysis: false, count: 0};
}
let modified_count = 0;
for (const file of analyzed_files) {
try {
const full_path = resolve_path(file.file_path, project_path);
if (!fs.existsSync(full_path)) {
console.warn(`Analyzed file not found (likely deleted): ${full_path}`);
continue;
}
const file_content = fs.readFileSync(full_path);
const current_checksum = calculate_checksum(file_content);
if (current_checksum !== file.last_checksum) {
modified_count++;
}
} catch (error) {
console.error(`Error checking file for modification: ${file.file_path}`, error);
modified_count++;
}
}
return {
needs_reanalysis: modified_count > 0,
count: modified_count
};
}
function create_file({ project_path, file_path }) {
const full_path = resolve_path(file_path, project_path);
try {
if (fs.existsSync(full_path)) {
throw new Error(`File already exists: ${file_path}`);
}
fs.mkdirSync(path.dirname(full_path), { recursive: true });
fs.writeFileSync(full_path, '', 'utf8');
return { success: true };
} catch (error) {
console.error(`Error creating file ${full_path}:`, error);
throw error;
}
}
function create_folder({ project_path, folder_path }) {
const full_path = resolve_path(folder_path, project_path);
try {
if (fs.existsSync(full_path)) {
throw new Error(`Folder already exists: ${folder_path}`);
}
fs.mkdirSync(full_path, { recursive: true });
return { success: true };
} catch (error) {
console.error(`Error creating folder ${full_path}:`, error);
throw error;
}
}
function rename_path({ project_path, old_path, new_path }) {
const full_old_path = resolve_path(old_path, project_path);
const full_new_path = resolve_path(new_path, project_path);
try {
if (fs.existsSync(full_new_path)) {
throw new Error(`Destination path already exists: ${new_path}`);
}
fs.renameSync(full_old_path, full_new_path);
db.prepare('UPDATE file_metadata SET file_path = ? WHERE project_path = ? AND file_path = ?')
.run(new_path, project_path, old_path);
db.prepare('UPDATE project_open_tabs SET file_path = ? WHERE project_path = ? AND file_path = ?')
.run(new_path, project_path, old_path);
return { success: true };
} catch (error) {
console.error(`Error renaming ${old_path} to ${new_path}:`, error);
throw error;
}
}
function delete_path({ project_path, path_to_delete }) {
const full_path = resolve_path(path_to_delete, project_path);
try {
fs.rmSync(full_path, { recursive: true, force: true });
db.prepare('DELETE FROM file_metadata WHERE project_path = ? AND file_path LIKE ?')
.run(project_path, `${path_to_delete}%`);
db.prepare('DELETE FROM project_open_tabs WHERE project_path = ? AND file_path LIKE ?')
.run(project_path, `${path_to_delete}%`);
return { success: true };
} catch (error) {
console.error(`Error deleting ${path_to_delete}:`, error);
throw error;
}
}
function git_reset_file({ project_path, file_path }) {
if (!isGitRepository(project_path)) {
throw new Error('Project is not a Git repository.');
}
const sanitized_path = file_path.replace(/\\/g, '/');
if (sanitized_path.includes('"') || sanitized_path.includes(';')) {
throw new Error(`Invalid characters in file path for git command: ${sanitized_path}`);
}
try {
const gitCommand = `git checkout HEAD -- "${sanitized_path}"`;
execSync(gitCommand, { cwd: project_path, encoding: 'utf8', timeout: 2000 });
return { success: true };
} catch (error) {
console.error(`Error resetting file ${file_path}:`, error);
throw error;
}
}
module.exports = {
get_folders,
get_file_content,
save_file_content,
get_raw_file_content,
search_files,
get_file_analysis,
calculate_checksum,
check_folder_updates,
check_for_modified_files,
get_file_for_editor,
get_file_mtime,
create_file,
create_folder,
rename_path,
delete_path,
git_reset_file,
};