-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwatcher.js
More file actions
267 lines (233 loc) · 8.34 KB
/
Copy pathwatcher.js
File metadata and controls
267 lines (233 loc) · 8.34 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
import fs from 'node:fs';
import path from 'node:path';
import { readFileSafe } from './builder.js';
import { EXTENSIONS, IGNORE_DIRS, normalizePath } from './constants.js';
import { closeDb, initSchema, openDb } from './db.js';
import { appendJournalEntries } from './journal.js';
import { info, warn } from './logger.js';
import { createParseTreeCache, getActiveEngine, parseFileIncremental } from './parser.js';
import { resolveImportPath } from './resolve.js';
function shouldIgnore(filePath) {
const parts = filePath.split(path.sep);
return parts.some((p) => IGNORE_DIRS.has(p));
}
function isTrackedExt(filePath) {
return EXTENSIONS.has(path.extname(filePath));
}
/**
* Parse a single file and update the database incrementally.
*/
async function updateFile(_db, rootDir, filePath, stmts, engineOpts, cache) {
const relPath = normalizePath(path.relative(rootDir, filePath));
const oldNodes = stmts.countNodes.get(relPath)?.c || 0;
const _oldEdges = stmts.countEdgesForFile.get(relPath)?.c || 0;
stmts.deleteEdgesForFile.run(relPath);
stmts.deleteNodes.run(relPath);
if (!fs.existsSync(filePath)) {
if (cache) cache.remove(filePath);
return { file: relPath, nodesAdded: 0, nodesRemoved: oldNodes, edgesAdded: 0, deleted: true };
}
let code;
try {
code = readFileSafe(filePath);
} catch (err) {
warn(`Cannot read ${relPath}: ${err.message}`);
return null;
}
const symbols = await parseFileIncremental(cache, filePath, code, engineOpts);
if (!symbols) return null;
stmts.insertNode.run(relPath, 'file', relPath, 0, null);
for (const def of symbols.definitions) {
stmts.insertNode.run(def.name, def.kind, relPath, def.line, def.endLine || null);
}
for (const exp of symbols.exports) {
stmts.insertNode.run(exp.name, exp.kind, relPath, exp.line, null);
}
const newNodes = stmts.countNodes.get(relPath)?.c || 0;
let edgesAdded = 0;
const fileNodeRow = stmts.getNodeId.get(relPath, 'file', relPath, 0);
if (!fileNodeRow)
return { file: relPath, nodesAdded: newNodes, nodesRemoved: oldNodes, edgesAdded: 0 };
const fileNodeId = fileNodeRow.id;
// Load aliases for full import resolution
const aliases = { baseUrl: null, paths: {} };
for (const imp of symbols.imports) {
const resolvedPath = resolveImportPath(
path.join(rootDir, relPath),
imp.source,
rootDir,
aliases,
);
const targetRow = stmts.getNodeId.get(resolvedPath, 'file', resolvedPath, 0);
if (targetRow) {
const edgeKind = imp.reexport ? 'reexports' : imp.typeOnly ? 'imports-type' : 'imports';
stmts.insertEdge.run(fileNodeId, targetRow.id, edgeKind, 1.0, 0);
edgesAdded++;
}
}
const importedNames = new Map();
for (const imp of symbols.imports) {
const resolvedPath = resolveImportPath(
path.join(rootDir, relPath),
imp.source,
rootDir,
aliases,
);
for (const name of imp.names) {
importedNames.set(name.replace(/^\*\s+as\s+/, ''), resolvedPath);
}
}
for (const call of symbols.calls) {
let caller = null;
for (const def of symbols.definitions) {
if (def.line <= call.line) {
const row = stmts.getNodeId.get(def.name, def.kind, relPath, def.line);
if (row) caller = row;
}
}
if (!caller) caller = fileNodeRow;
const importedFrom = importedNames.get(call.name);
let targets;
if (importedFrom) {
targets = stmts.findNodeInFile.all(call.name, importedFrom);
}
if (!targets || targets.length === 0) {
targets = stmts.findNodeInFile.all(call.name, relPath);
if (targets.length === 0) {
targets = stmts.findNodeByName.all(call.name);
}
}
for (const t of targets) {
if (t.id !== caller.id) {
stmts.insertEdge.run(
caller.id,
t.id,
'calls',
importedFrom ? 1.0 : 0.5,
call.dynamic ? 1 : 0,
);
edgesAdded++;
}
}
}
return {
file: relPath,
nodesAdded: newNodes,
nodesRemoved: oldNodes,
edgesAdded,
deleted: false,
};
}
export async function watchProject(rootDir, opts = {}) {
const dbPath = path.join(rootDir, '.codegraph', 'graph.db');
if (!fs.existsSync(dbPath)) {
console.error('No graph.db found. Run `codegraph build` first.');
process.exit(1);
}
const db = openDb(dbPath);
initSchema(db);
const engineOpts = { engine: opts.engine || 'auto' };
const { name: engineName, version: engineVersion } = getActiveEngine(engineOpts);
console.log(
`Watch mode using ${engineName} engine${engineVersion ? ` (v${engineVersion})` : ''}`,
);
const cache = createParseTreeCache();
console.log(
cache
? 'Incremental parsing enabled (native tree cache)'
: 'Incremental parsing unavailable (full re-parse)',
);
const stmts = {
insertNode: db.prepare(
'INSERT OR IGNORE INTO nodes (name, kind, file, line, end_line) VALUES (?, ?, ?, ?, ?)',
),
getNodeId: db.prepare(
'SELECT id FROM nodes WHERE name = ? AND kind = ? AND file = ? AND line = ?',
),
insertEdge: db.prepare(
'INSERT INTO edges (source_id, target_id, kind, confidence, dynamic) VALUES (?, ?, ?, ?, ?)',
),
deleteNodes: db.prepare('DELETE FROM nodes WHERE file = ?'),
deleteEdgesForFile: null,
countNodes: db.prepare('SELECT COUNT(*) as c FROM nodes WHERE file = ?'),
countEdgesForFile: null,
findNodeInFile: db.prepare(
"SELECT id, file FROM nodes WHERE name = ? AND kind IN ('function', 'method', 'class', 'interface', 'type', 'struct', 'enum', 'trait', 'record', 'module') AND file = ?",
),
findNodeByName: db.prepare(
"SELECT id, file FROM nodes WHERE name = ? AND kind IN ('function', 'method', 'class', 'interface', 'type', 'struct', 'enum', 'trait', 'record', 'module')",
),
};
// Use named params for statements needing the same value twice
const origDeleteEdges = db.prepare(
`DELETE FROM edges WHERE source_id IN (SELECT id FROM nodes WHERE file = @f) OR target_id IN (SELECT id FROM nodes WHERE file = @f)`,
);
const origCountEdges = db.prepare(
`SELECT COUNT(*) as c FROM edges WHERE source_id IN (SELECT id FROM nodes WHERE file = @f) OR target_id IN (SELECT id FROM nodes WHERE file = @f)`,
);
stmts.deleteEdgesForFile = { run: (f) => origDeleteEdges.run({ f }) };
stmts.countEdgesForFile = { get: (f) => origCountEdges.get({ f }) };
const pending = new Set();
let timer = null;
const DEBOUNCE_MS = 300;
async function processPending() {
const files = [...pending];
pending.clear();
const results = [];
for (const filePath of files) {
const result = await updateFile(db, rootDir, filePath, stmts, engineOpts, cache);
if (result) results.push(result);
}
const updates = results;
// Append processed files to journal for Tier 0 detection on next build
if (updates.length > 0) {
const entries = updates.map((r) => ({
file: r.file,
deleted: r.deleted || false,
}));
try {
appendJournalEntries(rootDir, entries);
} catch {
/* journal write failure is non-fatal */
}
}
for (const r of updates) {
const nodeDelta = r.nodesAdded - r.nodesRemoved;
const nodeStr = nodeDelta >= 0 ? `+${nodeDelta}` : `${nodeDelta}`;
if (r.deleted) {
info(`Removed: ${r.file} (-${r.nodesRemoved} nodes)`);
} else {
info(`Updated: ${r.file} (${nodeStr} nodes, +${r.edgesAdded} edges)`);
}
}
}
console.log(`Watching ${rootDir} for changes...`);
console.log('Press Ctrl+C to stop.\n');
const watcher = fs.watch(rootDir, { recursive: true }, (_eventType, filename) => {
if (!filename) return;
if (shouldIgnore(filename)) return;
if (!isTrackedExt(filename)) return;
const fullPath = path.join(rootDir, filename);
pending.add(fullPath);
if (timer) clearTimeout(timer);
timer = setTimeout(processPending, DEBOUNCE_MS);
});
process.on('SIGINT', () => {
console.log('\nStopping watcher...');
watcher.close();
// Flush any pending file paths to journal before exit
if (pending.size > 0) {
const entries = [...pending].map((filePath) => ({
file: normalizePath(path.relative(rootDir, filePath)),
}));
try {
appendJournalEntries(rootDir, entries);
} catch {
/* best-effort */
}
}
if (cache) cache.clear();
closeDb(db);
process.exit(0);
});
}