yats-toolkit 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/setup.js +15 -6
- package/package.json +2 -2
- package/src/indexer.js +326 -40
- package/src/indexer.test.js +243 -1
- package/src/setup.js +163 -36
- package/src/setup.test.js +89 -0
- package/src/watch.js +54 -22
- package/src/watch.test.js +44 -0
package/bin/setup.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Usage:
|
|
6
6
|
* yats setup # One-time setup wizard
|
|
7
|
-
* yats index <path> # Index a
|
|
7
|
+
* yats index <path> # Index a repo (--skip-docs, --no-config, --force)
|
|
8
8
|
* yats search <query> # Search indexed code
|
|
9
9
|
* yats list # List indexed repositories
|
|
10
10
|
* yats summary <repo> # Show repository summary
|
|
@@ -31,12 +31,15 @@ function parseFlags(arr) {
|
|
|
31
31
|
const a = arr[i];
|
|
32
32
|
if (a === "--provider" && arr[i + 1]) flags.provider = arr[++i];
|
|
33
33
|
else if (a === "--api-key" && arr[i + 1]) flags.apiKey = arr[++i];
|
|
34
|
+
else if (a === "--endpoint" && arr[i + 1]) flags.endpoint = arr[++i];
|
|
35
|
+
else if (a === "--model" && arr[i + 1]) flags.model = arr[++i];
|
|
34
36
|
else if (a === "--port" && arr[i + 1]) flags.port = arr[++i];
|
|
35
37
|
else if (a === "--batch" && arr[i + 1]) flags.batch = arr[++i];
|
|
36
38
|
else if (a === "--no-docs") flags.noDocs = true;
|
|
37
39
|
else if (a === "--yes") flags.skipConfirm = true;
|
|
38
40
|
}
|
|
39
|
-
|
|
41
|
+
// Azure additionally requires an endpoint URL to run unattended.
|
|
42
|
+
flags.nonInteractive = !!(flags.provider && flags.apiKey && (flags.provider !== "azure" || flags.endpoint));
|
|
40
43
|
return flags;
|
|
41
44
|
}
|
|
42
45
|
|
|
@@ -47,8 +50,14 @@ switch (cmd) {
|
|
|
47
50
|
case "index":
|
|
48
51
|
case "add": {
|
|
49
52
|
const skipDocs = args.includes("--skip-docs");
|
|
50
|
-
const
|
|
51
|
-
|
|
53
|
+
const noConfig = args.includes("--no-config");
|
|
54
|
+
const force = args.includes("--force");
|
|
55
|
+
const cleanArgs = args.filter(
|
|
56
|
+
(a) => a !== "--skip-docs" && a !== "--no-config" && a !== "--force",
|
|
57
|
+
);
|
|
58
|
+
import("../src/indexer.js").then((m) =>
|
|
59
|
+
m.default(cleanArgs, { skipDocs, noConfig, force }),
|
|
60
|
+
);
|
|
52
61
|
break;
|
|
53
62
|
}
|
|
54
63
|
case "reindex":
|
|
@@ -110,7 +119,7 @@ switch (cmd) {
|
|
|
110
119
|
console.log(`YATS Toolkit — Code intelligence for AI agents`);
|
|
111
120
|
console.log(``);
|
|
112
121
|
console.log(` yats setup Setup wizard`);
|
|
113
|
-
console.log(` yats index <path> Index a
|
|
122
|
+
console.log(` yats index <path> Index a repo (--skip-docs, --no-config)`);
|
|
114
123
|
console.log(` yats search <query> Search indexed code`);
|
|
115
124
|
console.log(` yats list List indexed repositories`);
|
|
116
125
|
console.log(` yats summary <repo> Show repository summary`);
|
|
@@ -123,7 +132,7 @@ switch (cmd) {
|
|
|
123
132
|
console.log(` yats update-base Update Docker images`);
|
|
124
133
|
console.log(` yats bridge Stdio proxy for Copilot/Claude`);
|
|
125
134
|
console.log(` yats benchmark AI agent token comparison`);
|
|
126
|
-
console.log(` yats watch <path> Sync index with git commits (--live
|
|
135
|
+
console.log(` yats watch <path> Sync index with git commits (--live, --no-config)`);
|
|
127
136
|
console.log(` yats connect [agent] Show agent setup config`);
|
|
128
137
|
break;
|
|
129
138
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yats-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "YATS — Yet Another Token Saver. One-command MCP server that indexes your codebase into a knowledge graph so AI agents (Claude, Cursor, Copilot) can search and navigate it without burning tokens.",
|
|
5
5
|
"author": "Franco Vinciarelli <vinciarellifranco@gmail.com>",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
],
|
|
50
50
|
"scripts": {
|
|
51
51
|
"start": "node bin/setup.js",
|
|
52
|
-
"test": "node --test src/*.test.js"
|
|
52
|
+
"test": "node --test --test-concurrency=1 src/*.test.js"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {},
|
|
55
55
|
"engines": {
|
package/src/indexer.js
CHANGED
|
@@ -1,16 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* yats index <path> [--skip-docs] — Index a repository by
|
|
3
|
-
* Reads each file from the host and POSTs it
|
|
2
|
+
* yats index <path> [--skip-docs] [--no-config] — Index a repository by
|
|
3
|
+
* sending files to YATS server. Reads each file from the host and POSTs it
|
|
4
|
+
* via HTTP.
|
|
4
5
|
*
|
|
5
|
-
* --skip-docs: skip documentation files (DOC_EXTENSIONS, default .md/.mdx
|
|
6
|
-
* .txt/.adoc/.org/.wiki/.readme) instead of only .md files.
|
|
6
|
+
* --skip-docs: skip documentation files (DOC_EXTENSIONS, default .md/.mdx/
|
|
7
|
+
* .rst/.txt/.adoc/.org/.wiki/.readme) instead of only .md files.
|
|
8
|
+
* --no-config: ignore the repo's .yats/config.json and use machine defaults.
|
|
9
|
+
*
|
|
10
|
+
* Per-repo config (`.yats/config.json`, all keys optional):
|
|
11
|
+
*
|
|
12
|
+
* {
|
|
13
|
+
* "indexDocs": false, // skip ALL docs for this repo
|
|
14
|
+
* "docExtensions": [".md"], // REPLACES the machine's DOC_EXTENSIONS
|
|
15
|
+
* "docPatterns": ["docs/", "README.md"], // whitelist: only these doc files are sent
|
|
16
|
+
* "ignoredDirs": ["sandbox"], // ADDED to the machine's IGNORED_DIRS
|
|
17
|
+
* "skipExtensions": [".snap"] // ADDED to the machine's SKIP_EXTENSIONS
|
|
18
|
+
* }
|
|
7
19
|
*/
|
|
8
|
-
import { readFileSync, statSync, readdirSync } from "node:fs";
|
|
20
|
+
import { readFileSync, statSync, readdirSync, readSync } from "node:fs";
|
|
9
21
|
import { join, relative } from "node:path";
|
|
10
22
|
import { homedir } from "node:os";
|
|
23
|
+
import { spawnSync } from "node:child_process";
|
|
24
|
+
import { createHash } from "node:crypto";
|
|
11
25
|
|
|
12
26
|
const YATS_URL = process.env.YATS_URL || "http://localhost:5555";
|
|
13
27
|
|
|
28
|
+
const CONFIG_SCHEMA_HINT = `{
|
|
29
|
+
"indexDocs": false,
|
|
30
|
+
"docExtensions": [".md"],
|
|
31
|
+
"docPatterns": ["docs/", "README.md"],
|
|
32
|
+
"ignoredDirs": ["sandbox"],
|
|
33
|
+
"skipExtensions": [".snap"]
|
|
34
|
+
}`;
|
|
35
|
+
|
|
14
36
|
export const IGNORED = new Set([
|
|
15
37
|
"node_modules", ".git", "dist", "build", ".next", "__pycache__",
|
|
16
38
|
"vendor", "target", "bin", "obj", ".venv", "venv", ".yarn", ".pnpm",
|
|
@@ -18,6 +40,45 @@ export const IGNORED = new Set([
|
|
|
18
40
|
|
|
19
41
|
export const DEFAULT_DOC_EXTENSIONS = ".md,.mdx,.rst,.txt,.adoc,.org,.wiki,.readme";
|
|
20
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Diff plan for one index run.
|
|
45
|
+
*
|
|
46
|
+
* @param walkedFiles [{path, hash}] — files found on disk with their content hash
|
|
47
|
+
* @param state server state from GET /index/state: {files, reanalyzeAll}
|
|
48
|
+
* @param options.force re-analyze everything (skips the state diff)
|
|
49
|
+
*
|
|
50
|
+
* send: files to POST /index/file (changed, new, or forced)
|
|
51
|
+
* remove: paths present in the server state but gone from disk
|
|
52
|
+
* unchanged: files skipped because hash + analyzer version match
|
|
53
|
+
*/
|
|
54
|
+
export function planIndex(walkedFiles, state, { force = false } = {}) {
|
|
55
|
+
const files = state?.files ?? {};
|
|
56
|
+
const reanalyzeAll = !!state?.reanalyzeAll;
|
|
57
|
+
const send = [];
|
|
58
|
+
const remove = [];
|
|
59
|
+
let unchanged = 0;
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
|
|
62
|
+
for (const f of walkedFiles) {
|
|
63
|
+
seen.add(f.path);
|
|
64
|
+
if (force || reanalyzeAll || files[f.path] !== f.hash) {
|
|
65
|
+
send.push(f.path);
|
|
66
|
+
} else {
|
|
67
|
+
unchanged++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Deletion detection: state entries not on disk anymore. Skipped on forced
|
|
72
|
+
// runs (state is not fetched) — the next normal run cleans them up.
|
|
73
|
+
if (!force) {
|
|
74
|
+
for (const p of Object.keys(files)) {
|
|
75
|
+
if (!seen.has(p)) remove.push(p);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { send, remove, unchanged };
|
|
80
|
+
}
|
|
81
|
+
|
|
21
82
|
/**
|
|
22
83
|
* Read the YATS config written by `yats setup` (~/.yats/.env), so the CLI
|
|
23
84
|
* filters files consistently with the server (DOC_EXTENSIONS, SKIP_EXTENSIONS,
|
|
@@ -43,14 +104,171 @@ export function loadYatsEnv(envPath = null) {
|
|
|
43
104
|
* Pure helper (exported for tests):
|
|
44
105
|
* - skipDocs: drop files whose extension is in docExtensions
|
|
45
106
|
* - skipExtensions: drop files ending with any of these suffixes
|
|
107
|
+
* - docPatterns (prefix whitelist): when set, doc files are only sent if
|
|
108
|
+
* their path starts with one of the prefixes (code files always sent)
|
|
46
109
|
*/
|
|
47
|
-
export function shouldSkipFile(
|
|
110
|
+
export function shouldSkipFile(
|
|
111
|
+
relPath,
|
|
112
|
+
{ skipDocs = false, docExtensions = [], skipExtensions = [], docPatterns = null } = {},
|
|
113
|
+
) {
|
|
48
114
|
const lower = relPath.toLowerCase();
|
|
49
|
-
|
|
115
|
+
const isDoc = docExtensions.some((e) => lower.endsWith(e));
|
|
116
|
+
if (isDoc) {
|
|
117
|
+
if (skipDocs) return true;
|
|
118
|
+
if (docPatterns && !docPatterns.some((p) => relPath.startsWith(p))) return true;
|
|
119
|
+
}
|
|
50
120
|
if (skipExtensions.some((e) => lower.endsWith(e))) return true;
|
|
51
121
|
return false;
|
|
52
122
|
}
|
|
53
123
|
|
|
124
|
+
// ============================================================
|
|
125
|
+
// Per-repo config — .yats/config.json (P6)
|
|
126
|
+
// ============================================================
|
|
127
|
+
|
|
128
|
+
/** Validate a parsed repo config. Returns {ok:false,error} with a human
|
|
129
|
+
* (and AI-agent) readable explanation of what is wrong. */
|
|
130
|
+
export function validateRepoConfig(raw) {
|
|
131
|
+
let data;
|
|
132
|
+
try {
|
|
133
|
+
data = JSON.parse(raw);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
return { ok: false, error: `not valid JSON: ${err.message}` };
|
|
136
|
+
}
|
|
137
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
138
|
+
return { ok: false, error: "must be a JSON object (not an array or a plain value)" };
|
|
139
|
+
}
|
|
140
|
+
const errors = [];
|
|
141
|
+
if (data.indexDocs !== undefined && typeof data.indexDocs !== "boolean") {
|
|
142
|
+
errors.push('"indexDocs" must be true or false');
|
|
143
|
+
}
|
|
144
|
+
for (const key of ["docExtensions", "docPatterns", "ignoredDirs", "skipExtensions"]) {
|
|
145
|
+
if (data[key] !== undefined && (!Array.isArray(data[key]) || !data[key].every((v) => typeof v === "string"))) {
|
|
146
|
+
errors.push(`"${key}" must be an array of strings`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (errors.length > 0) return { ok: false, error: errors.join("; ") };
|
|
150
|
+
return { ok: true, config: data };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Load and validate the repo's .yats/config.json.
|
|
155
|
+
* Returns { ok:true, config, path, exists } or { ok:false, error, path, exists }.
|
|
156
|
+
* A missing file is NOT an error — machine defaults apply.
|
|
157
|
+
*/
|
|
158
|
+
export function loadRepoConfig(repoPath) {
|
|
159
|
+
const configPath = join(repoPath, ".yats", "config.json");
|
|
160
|
+
let raw;
|
|
161
|
+
try {
|
|
162
|
+
raw = readFileSync(configPath, "utf-8");
|
|
163
|
+
} catch {
|
|
164
|
+
return { ok: true, config: {}, path: configPath, exists: false };
|
|
165
|
+
}
|
|
166
|
+
const result = validateRepoConfig(raw);
|
|
167
|
+
return { ...result, path: configPath, exists: true };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Merge the machine config (~/.yats/.env) with the repo config.
|
|
172
|
+
* Rule: a repo can only NARROW what the machine allows — ignoredDirs and
|
|
173
|
+
* skipExtensions are UNIONs, docExtensions REPLACES the global list, and
|
|
174
|
+
* indexDocs=false can only turn docs off (never on, if the machine forbids).
|
|
175
|
+
*/
|
|
176
|
+
export function mergeRepoConfig(globalEnv, repoConfig) {
|
|
177
|
+
const toList = (v) => v.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
178
|
+
|
|
179
|
+
const docExtensions = Array.isArray(repoConfig.docExtensions)
|
|
180
|
+
? repoConfig.docExtensions.map((e) => e.trim().toLowerCase()).filter(Boolean)
|
|
181
|
+
: toList(globalEnv.DOC_EXTENSIONS ?? DEFAULT_DOC_EXTENSIONS);
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
docExtensions,
|
|
185
|
+
skipExtensions: [
|
|
186
|
+
...toList(globalEnv.SKIP_EXTENSIONS ?? ""),
|
|
187
|
+
...(Array.isArray(repoConfig.skipExtensions) ? repoConfig.skipExtensions : []),
|
|
188
|
+
],
|
|
189
|
+
ignoredDirs: [
|
|
190
|
+
...toList(globalEnv.IGNORED_DIRS ?? ""),
|
|
191
|
+
...(Array.isArray(repoConfig.ignoredDirs) ? repoConfig.ignoredDirs : []),
|
|
192
|
+
],
|
|
193
|
+
docPatterns: Array.isArray(repoConfig.docPatterns) ? repoConfig.docPatterns : null,
|
|
194
|
+
indexDocs: repoConfig.indexDocs !== false,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The message printed when .yats/config.json is invalid. Written for BOTH
|
|
200
|
+
* humans and AI agents running `yats index`: it explains what is wrong, why
|
|
201
|
+
* indexing stopped (a broken filter could index something huge), and the
|
|
202
|
+
* three ways out (fix + re-run, delete the file, or --no-config).
|
|
203
|
+
*/
|
|
204
|
+
export function configErrorText(repoPath, configPath, error) {
|
|
205
|
+
return [
|
|
206
|
+
`✗ Invalid repo config: ${configPath}`,
|
|
207
|
+
``,
|
|
208
|
+
` ${error}`,
|
|
209
|
+
``,
|
|
210
|
+
` Indexing stopped before reading any file. A broken config could mean`,
|
|
211
|
+
` wrong filters (e.g. indexing a huge folder that was meant to be ignored).`,
|
|
212
|
+
``,
|
|
213
|
+
` How to fix — pick ONE:`,
|
|
214
|
+
``,
|
|
215
|
+
` 1. Correct ${configPath} (valid JSON, schema below) and re-run:`,
|
|
216
|
+
``,
|
|
217
|
+
` yats index ${repoPath}`,
|
|
218
|
+
``,
|
|
219
|
+
` 2. Delete the file to fall back to the machine defaults (~/.yats/.env).`,
|
|
220
|
+
``,
|
|
221
|
+
` 3. Re-run with --no-config to ignore the file for this run only:`,
|
|
222
|
+
``,
|
|
223
|
+
` yats index ${repoPath} --no-config`,
|
|
224
|
+
``,
|
|
225
|
+
` Valid schema (every key is optional):`,
|
|
226
|
+
``,
|
|
227
|
+
...CONFIG_SCHEMA_HINT.split("\n").map((l) => ` ${l}`),
|
|
228
|
+
``,
|
|
229
|
+
` Semantics: ignoredDirs/skipExtensions ADD to the machine config;`,
|
|
230
|
+
` docExtensions REPLACES it; docPatterns whitelists which doc files are`,
|
|
231
|
+
` sent (prefix match); indexDocs=false skips all docs for this repo.`,
|
|
232
|
+
].join("\n");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function promptYesNo(question) {
|
|
236
|
+
process.stdout.write(question);
|
|
237
|
+
try {
|
|
238
|
+
const buf = Buffer.alloc(16);
|
|
239
|
+
const n = readSync(0, buf, 0, 16);
|
|
240
|
+
return buf.toString("utf-8", 0, n).trim().toLowerCase() === "y";
|
|
241
|
+
} catch {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Resolve the repo config, stopping the run on a malformed file.
|
|
248
|
+
* - Non-TTY (the normal case — an AI agent runs yats): exit 1 with the
|
|
249
|
+
* configErrorText so the agent can fix the file and re-run.
|
|
250
|
+
* - TTY (a human): ask whether to open $EDITOR to fix it, re-validate and
|
|
251
|
+
* retry in a loop until valid or declined.
|
|
252
|
+
*/
|
|
253
|
+
export function resolveRepoConfig(repoPath, noConfig = false) {
|
|
254
|
+
if (noConfig) return {};
|
|
255
|
+
for (;;) {
|
|
256
|
+
const res = loadRepoConfig(repoPath);
|
|
257
|
+
if (res.ok) return res.config;
|
|
258
|
+
|
|
259
|
+
console.error(configErrorText(repoPath, res.path, res.error));
|
|
260
|
+
|
|
261
|
+
if (!process.stdin.isTTY) process.exit(1);
|
|
262
|
+
|
|
263
|
+
const editor = process.env.EDITOR || process.env.VISUAL || "vi";
|
|
264
|
+
const answer = promptYesNo(`\n Open ${editor} to fix it and retry? [y/N] `);
|
|
265
|
+
if (!answer) process.exit(1);
|
|
266
|
+
spawnSync(editor, [res.path], { stdio: "inherit" });
|
|
267
|
+
console.log(" Re-reading the config...");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
|
|
54
272
|
async function walk(dir, ignored) {
|
|
55
273
|
const files = [];
|
|
56
274
|
const entries = readdirSync(dir, { withFileTypes: true });
|
|
@@ -67,10 +285,12 @@ async function walk(dir, ignored) {
|
|
|
67
285
|
}
|
|
68
286
|
|
|
69
287
|
export default async function indexRepo(args, options = {}) {
|
|
70
|
-
const
|
|
288
|
+
const skipDocsFlag = options.skipDocs || false;
|
|
289
|
+
const noConfig = options.noConfig || false;
|
|
290
|
+
const force = options.force === true;
|
|
71
291
|
const repoPath = args[0];
|
|
72
292
|
if (!repoPath) {
|
|
73
|
-
console.error("Usage: npx yats index <path> [--skip-docs]");
|
|
293
|
+
console.error("Usage: npx yats index <path> [--skip-docs] [--no-config]");
|
|
74
294
|
process.exit(1);
|
|
75
295
|
}
|
|
76
296
|
|
|
@@ -89,18 +309,13 @@ export default async function indexRepo(args, options = {}) {
|
|
|
89
309
|
process.exit(1);
|
|
90
310
|
}
|
|
91
311
|
|
|
92
|
-
// P4 —
|
|
93
|
-
//
|
|
312
|
+
// P4/P6 — machine config (~/.yats/.env) + repo config (.yats/config.json).
|
|
313
|
+
// A malformed repo config STOPS the run (never index with broken filters).
|
|
94
314
|
const yatsEnv = loadYatsEnv();
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
const docExtensions = (yatsEnv.DOC_EXTENSIONS ?? DEFAULT_DOC_EXTENSIONS)
|
|
101
|
-
.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
102
|
-
const skipExtensions = (yatsEnv.SKIP_EXTENSIONS ?? "")
|
|
103
|
-
.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
315
|
+
const repoConfig = resolveRepoConfig(repoPath, noConfig);
|
|
316
|
+
const effective = mergeRepoConfig(yatsEnv, repoConfig);
|
|
317
|
+
const skipDocs = skipDocsFlag || effective.indexDocs === false;
|
|
318
|
+
const ignoredDirs = new Set([...IGNORED, ...effective.ignoredDirs]);
|
|
104
319
|
|
|
105
320
|
// Register repo
|
|
106
321
|
try {
|
|
@@ -114,48 +329,119 @@ export default async function indexRepo(args, options = {}) {
|
|
|
114
329
|
process.exit(1);
|
|
115
330
|
}
|
|
116
331
|
|
|
117
|
-
// Walk and
|
|
332
|
+
// Walk the tree and hash every file up front — reading + sha256 is cheap;
|
|
333
|
+
// it lets us diff against the server state instead of re-sending everything.
|
|
118
334
|
console.log(`Indexing ${repoPath}...`);
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
const CONCURRENCY = 10;
|
|
122
|
-
const batch = [];
|
|
123
|
-
|
|
124
|
-
for (const file of files) {
|
|
335
|
+
const walked = [];
|
|
336
|
+
for (const file of await walk(repoPath, ignoredDirs)) {
|
|
125
337
|
const relPath = relative(repoPath, file);
|
|
126
|
-
if (shouldSkipFile(relPath, {
|
|
127
|
-
|
|
338
|
+
if (shouldSkipFile(relPath, {
|
|
339
|
+
skipDocs,
|
|
340
|
+
docExtensions: effective.docExtensions,
|
|
341
|
+
skipExtensions: effective.skipExtensions,
|
|
342
|
+
docPatterns: effective.docPatterns,
|
|
343
|
+
})) continue;
|
|
344
|
+
try {
|
|
345
|
+
const content = readFileSync(file, "utf-8");
|
|
346
|
+
if (content.includes("\0") || content.length > 1_000_000) continue;
|
|
347
|
+
walked.push({
|
|
348
|
+
file,
|
|
349
|
+
relPath,
|
|
350
|
+
content,
|
|
351
|
+
hash: createHash("sha256").update(content).digest("hex"),
|
|
352
|
+
});
|
|
353
|
+
} catch {
|
|
354
|
+
// Unreadable file — skip it.
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Server state: which files were analyzed with which content hash, and
|
|
359
|
+
// whether the analyzer version forces a full re-analysis pass.
|
|
360
|
+
let state = { files: {}, reanalyzeAll: true };
|
|
361
|
+
if (!force) {
|
|
362
|
+
try {
|
|
363
|
+
const res = await fetch(
|
|
364
|
+
`${YATS_URL}/index/state?repository=${encodeURIComponent(repoName)}`,
|
|
365
|
+
);
|
|
366
|
+
if (res.ok) state = await res.json();
|
|
367
|
+
} catch {
|
|
368
|
+
// Server unreachable or endpoint missing — full pass.
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const plan = planIndex(
|
|
373
|
+
walked.map((w) => ({ path: w.relPath, hash: w.hash })),
|
|
374
|
+
state,
|
|
375
|
+
{ force },
|
|
376
|
+
);
|
|
377
|
+
const byPath = new Map(walked.map((w) => [w.relPath, w]));
|
|
378
|
+
|
|
379
|
+
// Remove files that disappeared since the last index
|
|
380
|
+
let removed = 0;
|
|
381
|
+
for (const relPath of plan.remove) {
|
|
382
|
+
try {
|
|
383
|
+
const res = await fetch(`${YATS_URL}/index/remove`, {
|
|
384
|
+
method: "POST",
|
|
385
|
+
headers: { "Content-Type": "application/json" },
|
|
386
|
+
body: JSON.stringify({ repository: repoName, path: relPath }),
|
|
387
|
+
});
|
|
388
|
+
if (res.ok) removed++;
|
|
389
|
+
} catch {
|
|
390
|
+
// Non-fatal — the next run retries.
|
|
391
|
+
}
|
|
128
392
|
}
|
|
129
393
|
|
|
130
394
|
let sent = 0;
|
|
131
395
|
let errors = 0;
|
|
396
|
+
let serverSkipped = 0;
|
|
397
|
+
const failedPaths = [];
|
|
398
|
+
const CONCURRENCY = 10;
|
|
399
|
+
const batch = plan.send.map((p) => byPath.get(p));
|
|
132
400
|
const total = batch.length;
|
|
133
401
|
|
|
134
402
|
for (let i = 0; i < batch.length; i += CONCURRENCY) {
|
|
135
403
|
const chunk = batch.slice(i, i + CONCURRENCY);
|
|
136
|
-
const results = await Promise.all(chunk.map(async (
|
|
137
|
-
const relPath =
|
|
404
|
+
const results = await Promise.all(chunk.map(async (entry) => {
|
|
405
|
+
const relPath = entry.relPath;
|
|
138
406
|
try {
|
|
139
|
-
const content = readFileSync(file, "utf-8");
|
|
140
|
-
if (content.includes("\0") || content.length > 1_000_000) return { ok: true, skipped: true };
|
|
141
407
|
const res = await fetch(`${YATS_URL}/index/file`, {
|
|
142
408
|
method: "POST",
|
|
143
409
|
headers: { "Content-Type": "application/json" },
|
|
144
|
-
body: JSON.stringify({
|
|
410
|
+
body: JSON.stringify({
|
|
411
|
+
repoName,
|
|
412
|
+
filePath: relPath,
|
|
413
|
+
content: entry.content,
|
|
414
|
+
contentHash: entry.hash,
|
|
415
|
+
}),
|
|
145
416
|
});
|
|
146
|
-
|
|
417
|
+
const data = await res.json().catch(() => ({}));
|
|
418
|
+
return { ok: res.ok, serverSkipped: data.skipped === true };
|
|
147
419
|
} catch {
|
|
148
|
-
return { ok: false,
|
|
420
|
+
return { ok: false, serverSkipped: false };
|
|
149
421
|
}
|
|
150
422
|
}));
|
|
151
|
-
|
|
423
|
+
|
|
152
424
|
for (const r of results) {
|
|
153
|
-
if (r.ok) sent++;
|
|
154
|
-
else if (
|
|
425
|
+
if (r.ok && !r.serverSkipped) sent++;
|
|
426
|
+
else if (r.ok) serverSkipped++;
|
|
427
|
+
else errors++;
|
|
428
|
+
}
|
|
429
|
+
for (const entry of chunk) {
|
|
430
|
+
const r = results[chunk.indexOf(entry)];
|
|
431
|
+
if (r && !r.ok) failedPaths.push(entry.relPath);
|
|
155
432
|
}
|
|
156
433
|
process.stdout.write(`\r ${sent + errors}/${total} files`);
|
|
157
434
|
}
|
|
158
|
-
console.log(
|
|
435
|
+
console.log(
|
|
436
|
+
`\r ✓ ${sent} files indexed` +
|
|
437
|
+
`${serverSkipped > 0 ? `, ${serverSkipped} skipped by server` : ""}` +
|
|
438
|
+
`${plan.unchanged > 0 ? `, ${plan.unchanged} unchanged` : ""}` +
|
|
439
|
+
`${removed > 0 ? `, ${removed} removed` : ""}` +
|
|
440
|
+
`${errors > 0 ? `, ${errors} failed` : ""}`,
|
|
441
|
+
);
|
|
442
|
+
if (failedPaths.length > 0) {
|
|
443
|
+
console.log(` ! failed: ${failedPaths.slice(0, 5).join(", ")}${failedPaths.length > 5 ? ` (+${failedPaths.length - 5} more)` : ""}`);
|
|
444
|
+
}
|
|
159
445
|
|
|
160
446
|
// Finalize: resolve cross-file references and store relationships.
|
|
161
447
|
// The server also flushes automatically after a quiet period, but an
|