-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-index.ts
More file actions
166 lines (158 loc) · 4.33 KB
/
Copy pathrun-index.ts
File metadata and controls
166 lines (158 loc) · 4.33 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
import { createSchema, getAllFileHashes, setMeta } from "../db";
import type { CodemapDatabase } from "../db";
import {
collectFiles,
deleteFilesFromIndex,
fetchTableStats,
getChangedFiles,
getCurrentCommit,
indexFiles,
targetedReindex,
} from "./index-engine";
import type { IndexResult, IndexTableStats } from "./types";
function emptyStats(): IndexTableStats {
return {
files: 0,
symbols: 0,
imports: 0,
exports: 0,
components: 0,
dependencies: 0,
markers: 0,
css_vars: 0,
css_classes: 0,
css_keyframes: 0,
};
}
/**
* - `incremental` — git-based diff vs last indexed commit (default).
* - `full` — re-glob and re-index everything.
* - `files` — only `options.files` (paths relative to project root).
*/
export type IndexMode = "incremental" | "full" | "files";
export interface RunIndexOptions {
/**
* Defaults to `incremental`.
*/
mode?: IndexMode;
/**
* Paths relative to the project root; used only when `mode === "files"`.
* Non-indexable extensions are filtered out.
*/
files?: string[];
/**
* Suppresses progress logs; parse failures may still be printed. Defaults to `false`.
*/
quiet?: boolean;
}
/**
* Core indexing pipeline (CLI and `Codemap#index`).
*
* @param db - Open database; caller owns the connection lifecycle.
* @param options - Index mode, optional targeted paths, and logging.
* @returns Row counts and timing; see {@link IndexResult}.
*
* @remarks
* Call `initCodemap()` and `configureResolver()` for this project before invoking (same as CLI bootstrap).
*/
export async function runCodemapIndex(
db: CodemapDatabase,
options: RunIndexOptions = {},
): Promise<IndexResult> {
const quiet = options.quiet ?? false;
const mode: IndexMode = options.mode ?? "incremental";
if (mode === "full") {
if (!quiet) console.log(" Full rebuild requested...");
const files = collectFiles();
const run = await indexFiles(db, files, true, undefined, { quiet });
return {
mode: "full",
indexed: run.indexed,
skipped: run.skipped,
elapsedMs: run.elapsedMs,
stats: run.stats,
};
}
if (mode === "files") {
const targetFiles = options.files ?? [];
if (targetFiles.length === 0) {
return {
mode: "files",
indexed: 0,
skipped: 0,
elapsedMs: 0,
stats: emptyStats(),
idle: true,
};
}
const run = await targetedReindex(db, targetFiles, quiet);
return {
mode: "files",
indexed: run.indexed,
skipped: run.skipped,
elapsedMs: run.elapsedMs,
stats: run.stats,
};
}
// Incremental path reads `meta` via getChangedFiles — schema must exist first
// (indexFiles / targetedReindex call createSchema later; fresh DB had none).
createSchema(db);
const diff = getChangedFiles(db);
if (diff) {
if (!quiet) {
console.log(
` Incremental: ${diff.changed.length} changed, ${diff.deleted.length} deleted`,
);
}
deleteFilesFromIndex(db, diff.deleted, quiet);
if (diff.changed.length > 0) {
const indexedPaths = new Set(getAllFileHashes(db).keys());
for (const f of diff.changed) indexedPaths.add(f);
const run = await indexFiles(db, diff.changed, false, indexedPaths, {
quiet,
});
return {
mode: "incremental",
indexed: run.indexed,
skipped: run.skipped,
elapsedMs: run.elapsedMs,
stats: run.stats,
};
}
if (diff.deleted.length > 0) {
setMeta(db, "last_indexed_commit", getCurrentCommit());
if (!quiet) console.log(" Index updated (deletions only)");
return {
mode: "incremental",
indexed: 0,
skipped: 0,
elapsedMs: 0,
stats: fetchTableStats(db),
idle: true,
};
}
if (!quiet) console.log(" Index is up to date");
return {
mode: "incremental",
indexed: 0,
skipped: 0,
elapsedMs: 0,
stats: fetchTableStats(db),
idle: true,
};
}
if (!quiet) {
console.log(
" No previous index or incompatible history, doing full rebuild...",
);
}
const files = collectFiles();
const run = await indexFiles(db, files, true, undefined, { quiet });
return {
mode: "full",
indexed: run.indexed,
skipped: run.skipped,
elapsedMs: run.elapsedMs,
stats: run.stats,
};
}