-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
181 lines (155 loc) · 5.17 KB
/
Copy pathindex.ts
File metadata and controls
181 lines (155 loc) · 5.17 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
import * as path from 'path';
import {
createDirectory,
isInitialized,
removeDirectory,
getDatabasePath,
} from './directory';
import { DatabaseConnection } from './db';
import { QueryBuilder } from './db/queries';
import { indexProject, resolveIndexerPlugins } from './indexer';
import type {
AssetKind,
AssetRecord,
GraphStats,
IndexProgress,
IndexResult,
SearchResult,
} from './types';
import {
buildBaselineReport,
applyUpdates,
formatApplyResult as formatApplyResultText,
traverseAssets,
type AgentUpdatePayload,
type ApplyOptions,
type BaselineOptions,
type ApplyResult,
type TraverseOptions,
type TraverseResult,
} from './enrich';
import {
ensureGraphVersion,
readGraphVersion,
setGraphVersion as writeGraphVersionValue,
} from './version';
import { ensurePluginsScaffold } from './indexer/external-plugins';
export * from './types';
export { ASSET_KINDS } from './types';
export {
getCodeGraphPlusDir,
isInitialized,
findNearestCodeGraphPlusRoot,
getDatabasePath,
} from './directory';
export { getIndexerPlugins, resolveIndexerPlugins, BUILTIN_PLUGINS, INDEXER_SLOTS } from './indexer';
export type { IndexerPlugin, IndexersConfig, IndexerSlot } from './indexer';
export { MCPServer } from './mcp';
export type { AgentUpdatePayload, ApplyOptions, BaselineOptions, ApplyResult, TraverseOptions, TraverseResult } from './enrich';
export { INITIAL_GRAPH_VERSION, GraphVersionError, validateGraphVersion } from './version';
export class CodeGraphPlus {
private db: DatabaseConnection;
private queries: QueryBuilder;
private projectRoot: string;
private constructor(projectRoot: string, db: DatabaseConnection) {
this.projectRoot = path.resolve(projectRoot);
this.db = db;
this.queries = new QueryBuilder(db.getDb());
ensureGraphVersion(this.queries, this.projectRoot);
ensurePluginsScaffold(this.projectRoot);
}
static init(projectRoot: string, options?: { index?: boolean; onProgress?: (p: IndexProgress) => void }): CodeGraphPlus {
const root = path.resolve(projectRoot);
if (!isInitialized(root)) {
createDirectory(root);
const db = DatabaseConnection.initialize(getDatabasePath(root));
db.getDb().prepare(
'INSERT OR IGNORE INTO metadata (key, value) VALUES (?, ?)',
).run('index_status', 'stub');
db.getDb().prepare(
'INSERT OR IGNORE INTO metadata (key, value) VALUES (?, ?)',
).run('graph_version', '0000.0000.0000.0000');
db.close();
}
const graph = CodeGraphPlus.openSync(root);
if (options?.index) {
graph.index(options.onProgress);
}
return graph;
}
static openSync(projectRoot: string): CodeGraphPlus {
const root = path.resolve(projectRoot);
if (!isInitialized(root)) {
throw new Error(`CodeGraphPlus not initialized in ${root}. Run: codegraphplus init -i`);
}
const db = DatabaseConnection.forProject(root);
return new CodeGraphPlus(root, db);
}
static async open(projectRoot: string): Promise<CodeGraphPlus> {
return CodeGraphPlus.openSync(projectRoot);
}
static uninit(projectRoot: string): void {
removeDirectory(path.resolve(projectRoot));
}
getProjectRoot(): string {
return this.projectRoot;
}
index(onProgress?: (p: IndexProgress) => void): IndexResult {
return indexProject(this.queries, this.projectRoot, onProgress);
}
search(query: string, limit = 10, kind?: AssetKind): SearchResult[] {
return this.queries.search(query, limit, kind);
}
getAsset(id: number): AssetRecord | null {
return this.queries.getAssetById(id);
}
findAssets(kind: AssetKind, nameOrQualified: string, limit = 20): AssetRecord[] {
return this.queries.findAssets(kind, nameOrQualified, limit);
}
getDependencies(assetId: number) {
return this.queries.getDependencies(assetId);
}
getConsumers(assetId: number) {
return this.queries.getConsumers(assetId);
}
getImpact(assetId: number, depth = 2): AssetRecord[] {
return this.queries.getImpact(assetId, depth);
}
listFiles() {
return this.queries.listFiles();
}
getStats(): GraphStats {
return this.queries.getStats();
}
getBaseline(opts?: BaselineOptions): string {
return buildBaselineReport(this.queries, { ...opts, projectRoot: this.projectRoot }).text;
}
getGraphVersion(): string {
return readGraphVersion(this.queries, this.projectRoot);
}
setGraphVersion(version: string): string {
return writeGraphVersionValue(this.queries, this.projectRoot, version);
}
traverse(opts: TraverseOptions): TraverseResult {
return traverseAssets(this.queries, opts);
}
applyAgentUpdates(
payload: AgentUpdatePayload,
options?: Omit<ApplyOptions, 'projectRoot'> | boolean,
): ApplyResult {
const opts: ApplyOptions = typeof options === 'boolean'
? { dryRun: options, projectRoot: this.projectRoot }
: { projectRoot: this.projectRoot, ...(options ?? {}) };
return applyUpdates(this.queries, payload, opts);
}
formatApplyResult(result: ApplyResult): string {
return formatApplyResultText(result);
}
listIndexerPlugins() {
return resolveIndexerPlugins(this.projectRoot);
}
close(): void {
this.db.close();
}
}
export default CodeGraphPlus;