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/src/watch.js CHANGED
@@ -27,7 +27,13 @@
27
27
  import { watch as fsWatch, statSync, readFileSync } from "node:fs";
28
28
  import { resolve, relative } from "node:path";
29
29
  import { execSync } from "node:child_process";
30
- import indexRepo, { IGNORED } from "./indexer.js";
30
+ import indexRepo, {
31
+ IGNORED,
32
+ loadYatsEnv,
33
+ mergeRepoConfig,
34
+ resolveRepoConfig,
35
+ shouldSkipFile,
36
+ } from "./indexer.js";
31
37
 
32
38
  // ============================================================
33
39
  // Config
@@ -132,14 +138,31 @@ async function getRecordedCommit(baseUrl, repoName) {
132
138
  }
133
139
  }
134
140
 
135
- function shouldSkipPath(relPath) {
141
+ function shouldSkipPath(relPath, extraIgnoredDirs = []) {
136
142
  if (!relPath || relPath.startsWith(".")) return true;
137
143
  const segments = relPath.split("/");
138
- return segments.some((seg) => IGNORED.has(seg)) || SKIP_PATTERNS.some((p) => p.test(relPath));
144
+ return (
145
+ segments.some((seg) => IGNORED.has(seg) || extraIgnoredDirs.includes(seg)) ||
146
+ SKIP_PATTERNS.some((p) => p.test(relPath))
147
+ );
139
148
  }
140
149
 
141
- async function indexFile(baseUrl, repoPath, repoName, relPath) {
142
- if (shouldSkipPath(relPath)) return;
150
+ /**
151
+ * Build the per-file filter from the effective config (machine + repo).
152
+ * Doc/code filtering is delegated to indexer.shouldSkipFile (P4/P6) so
153
+ * `yats watch` and `yats index` behave identically.
154
+ */
155
+ function makeFileFilter(effective) {
156
+ return (relPath) =>
157
+ shouldSkipPath(relPath, effective.ignoredDirs ?? []) ||
158
+ shouldSkipFile(relPath, {
159
+ ...effective,
160
+ skipDocs: effective.indexDocs === false,
161
+ });
162
+ }
163
+
164
+ async function indexFile(baseUrl, repoPath, repoName, relPath, skipFn) {
165
+ if (skipFn(relPath)) return;
143
166
  const fullPath = resolve(repoPath, relPath);
144
167
  let content;
145
168
  try {
@@ -163,8 +186,8 @@ async function indexFile(baseUrl, repoPath, repoName, relPath) {
163
186
  }
164
187
  }
165
188
 
166
- async function removeFile(baseUrl, repoName, relPath) {
167
- if (shouldSkipPath(relPath)) return;
189
+ async function removeFile(baseUrl, repoName, relPath, skipFn) {
190
+ if (skipFn(relPath)) return;
168
191
  console.log(` ↻ Removing from index: ${relPath}`);
169
192
  try {
170
193
  const res = await post(baseUrl, "/index/remove", {
@@ -184,9 +207,9 @@ async function removeFile(baseUrl, repoName, relPath) {
184
207
 
185
208
  /**
186
209
  * Bring the index from `from` to `to` by streaming only the changed files.
187
- * Exported for tests (baseUrl is injectable).
210
+ * Exported for tests (baseUrl and the effective config are injectable).
188
211
  */
189
- export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_URL) {
212
+ export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_URL, effective = {}) {
190
213
  let output;
191
214
  try {
192
215
  output = git(repoPath, `diff --name-status ${from}..${to}`);
@@ -198,6 +221,7 @@ export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_
198
221
  return;
199
222
  }
200
223
 
224
+ const skipFn = makeFileFilter(effective);
201
225
  const changes = parseNameStatus(output);
202
226
  const total =
203
227
  changes.added.length + changes.modified.length +
@@ -208,13 +232,13 @@ export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_
208
232
  `⟲${changes.renamed.length}`,
209
233
  );
210
234
 
211
- for (const p of changes.deleted) await removeFile(baseUrl, repoName, p);
212
- for (const r of changes.renamed) await removeFile(baseUrl, repoName, r.from);
235
+ for (const p of changes.deleted) await removeFile(baseUrl, repoName, p, skipFn);
236
+ for (const r of changes.renamed) await removeFile(baseUrl, repoName, r.from, skipFn);
213
237
  for (const p of [...changes.added, ...changes.modified]) {
214
- await indexFile(baseUrl, repoPath, repoName, p);
238
+ await indexFile(baseUrl, repoPath, repoName, p, skipFn);
215
239
  }
216
240
  for (const r of changes.renamed) {
217
- await indexFile(baseUrl, repoPath, repoName, r.to);
241
+ await indexFile(baseUrl, repoPath, repoName, r.to, skipFn);
218
242
  }
219
243
 
220
244
  // Flush cross-file relationships and record the new commit. Both are
@@ -237,8 +261,9 @@ export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_
237
261
  // Live mode — save-based indexing (fs.watch + debounce)
238
262
  // ============================================================
239
263
 
240
- function startLiveWatch(repoPath, repoName, baseUrl) {
264
+ function startLiveWatch(repoPath, repoName, baseUrl, effective) {
241
265
  const pending = new Map();
266
+ const skipFn = makeFileFilter(effective);
242
267
 
243
268
  fsWatch(repoPath, { recursive: true }, (_eventType, filename) => {
244
269
  if (!filename || SKIP_PATTERNS.some((p) => p.test(filename))) return;
@@ -257,9 +282,9 @@ function startLiveWatch(repoPath, repoName, baseUrl) {
257
282
  pending.delete(fullPath);
258
283
  try {
259
284
  statSync(fullPath);
260
- await indexFile(baseUrl, repoPath, repoName, relPath);
285
+ await indexFile(baseUrl, repoPath, repoName, relPath, skipFn);
261
286
  } catch {
262
- await removeFile(baseUrl, repoName, relPath);
287
+ await removeFile(baseUrl, repoName, relPath, skipFn);
263
288
  }
264
289
  }, DEBOUNCE_MS),
265
290
  );
@@ -274,19 +299,21 @@ function startLiveWatch(repoPath, repoName, baseUrl) {
274
299
 
275
300
  export default async function watchRepo(args = []) {
276
301
  const live = args.includes("--live");
302
+ const noConfig = args.includes("--no-config");
277
303
  const baseUrl = process.env.YATS_URL ?? DEFAULT_URL;
278
- const cleanArgs = args.filter((a) => a !== "--live" && a !== "--repo");
304
+ const cleanArgs = args.filter((a) => a !== "--live" && a !== "--repo" && a !== "--no-config");
279
305
 
280
306
  const repoPath = resolve(cleanArgs[0] ?? process.cwd());
281
307
 
282
308
  if (!cleanArgs[0] || cleanArgs[0] === "--help" || cleanArgs[0] === "-h") {
283
- console.log("Usage: yats watch <path> [--live]");
309
+ console.log("Usage: yats watch <path> [--live] [--no-config]");
284
310
  console.log("");
285
311
  console.log("Keeps the YATS index in sync with the repository's commits.");
286
312
  console.log("Saving files without committing does NOT touch the index —");
287
313
  console.log("the graph always reflects the last commit.");
288
314
  console.log("");
289
- console.log(" --live also re-index on every file save (uncommitted code)");
315
+ console.log(" --live also re-index on every file save (uncommitted code)");
316
+ console.log(" --no-config ignore the repo's .yats/config.json");
290
317
  console.log("");
291
318
  console.log(" yats watch ~/my-project");
292
319
  console.log(" yats watch ~/my-project --live");
@@ -303,6 +330,11 @@ export default async function watchRepo(args = []) {
303
330
  process.exit(1);
304
331
  }
305
332
 
333
+ // P6 — same config resolution as `yats index`: a malformed
334
+ // .yats/config.json stops the run instead of watching with wrong filters.
335
+ const repoConfig = resolveRepoConfig(repoPath, noConfig);
336
+ const effective = mergeRepoConfig(loadYatsEnv(), repoConfig);
337
+
306
338
  console.log(`👀 Watching: ${repoPath}`);
307
339
  console.log(` Repo: ${repoName}`);
308
340
  console.log(` Server: ${baseUrl}`);
@@ -318,7 +350,7 @@ export default async function watchRepo(args = []) {
318
350
  } catch {
319
351
  /* non-fatal */
320
352
  }
321
- startLiveWatch(repoPath, repoName, baseUrl);
353
+ startLiveWatch(repoPath, repoName, baseUrl, effective);
322
354
  }
323
355
 
324
356
  if (!gitOk) {
@@ -358,7 +390,7 @@ export default async function watchRepo(args = []) {
358
390
  console.log(
359
391
  ` Indexed commit ${recorded.slice(0, 8)} ≠ HEAD ${head.slice(0, 8)} — syncing...`,
360
392
  );
361
- await applyDiff(repoPath, repoName, recorded, head, baseUrl);
393
+ await applyDiff(repoPath, repoName, recorded, head, baseUrl, effective);
362
394
  recorded = head;
363
395
  }
364
396
 
@@ -375,7 +407,7 @@ export default async function watchRepo(args = []) {
375
407
 
376
408
  console.log(`\n ⬆ HEAD moved ${recorded ? recorded.slice(0, 8) + " → " : ""}${head.slice(0, 8)}`);
377
409
  if (recorded) {
378
- await applyDiff(repoPath, repoName, recorded, head, baseUrl);
410
+ await applyDiff(repoPath, repoName, recorded, head, baseUrl, effective);
379
411
  } else {
380
412
  // First commit after an unborn HEAD — nothing to diff from: full index.
381
413
  await indexRepo([repoPath]);
package/src/watch.test.js CHANGED
@@ -15,6 +15,7 @@ import { execSync } from "node:child_process";
15
15
  import { tmpdir } from "node:os";
16
16
 
17
17
  import { parseNameStatus, applyDiff } from "./watch.js";
18
+ import { mergeRepoConfig } from "./indexer.js";
18
19
 
19
20
  function git(cwd, cmd) {
20
21
  return execSync(`git ${cmd}`, { cwd, encoding: "utf-8", stdio: "pipe" }).trim();
@@ -150,4 +151,47 @@ describe("applyDiff — commit-based sync (P1)", () => {
150
151
  // The commit is still recorded (idempotent sync)
151
152
  assert.equal(requests.filter((r) => r.url === "/index/commit").length, 1);
152
153
  });
154
+
155
+ it("applies the repo config: docs skipped when indexDocs=false (P6)", async () => {
156
+ // New commit adding a doc file and a code file
157
+ writeFileSync(join(workDir, "guide.md"), "# Guide\n");
158
+ writeFileSync(join(workDir, "extra.txt"), "plain text\n");
159
+ git(workDir, "add -A");
160
+ git(workDir, "commit -m 'v3: add docs + code'");
161
+ const v3 = git(workDir, "rev-parse HEAD");
162
+
163
+ requests = [];
164
+ const effective = mergeRepoConfig({}, { indexDocs: false, docExtensions: [".md"] });
165
+ await applyDiff(workDir, repoName, v2, v3, baseUrl, effective);
166
+
167
+ const sentPaths = requests
168
+ .filter((r) => r.url === "/index/file")
169
+ .map((r) => r.body.filePath)
170
+ .sort();
171
+ assert.deepEqual(
172
+ sentPaths,
173
+ ["extra.txt"],
174
+ "doc files (.md) are skipped, code/plain files are sent",
175
+ );
176
+ });
177
+
178
+ it("applies docPatterns: only whitelisted docs are sent (P6)", async () => {
179
+ const v3 = git(workDir, "rev-parse HEAD");
180
+ mkdirSync(join(workDir, "docs"), { recursive: true });
181
+ writeFileSync(join(workDir, "docs", "api.md"), "# API\n");
182
+ writeFileSync(join(workDir, "notes.md"), "# Notes\n");
183
+ git(workDir, "add -A");
184
+ git(workDir, "commit -m 'v4: more docs'");
185
+ const v4 = git(workDir, "rev-parse HEAD");
186
+
187
+ requests = [];
188
+ const effective = mergeRepoConfig({}, { docExtensions: [".md"], docPatterns: ["docs/"] });
189
+ await applyDiff(workDir, repoName, v3, v4, baseUrl, effective);
190
+
191
+ const sentPaths = requests
192
+ .filter((r) => r.url === "/index/file")
193
+ .map((r) => r.body.filePath)
194
+ .sort();
195
+ assert.deepEqual(sentPaths, ["docs/api.md"], "only docs matching the whitelist are sent");
196
+ });
153
197
  });