@eventcatalog/sdk 2.14.1 → 2.14.3

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/dist/index.mjs CHANGED
@@ -435,54 +435,64 @@ import { join as join3 } from "path";
435
435
  import { globSync as globSync2 } from "glob";
436
436
  import fsSync from "fs";
437
437
  import { copy } from "fs-extra";
438
- import { join, dirname, normalize, resolve, relative } from "path";
438
+ import { join, dirname, normalize, sep as pathSeparator, resolve, relative } from "path";
439
439
  import matter from "gray-matter";
440
440
  import { satisfies, validRange } from "semver";
441
441
  var _fileIndexCache = null;
442
442
  var _fileIndexCatalogDir = null;
443
443
  var _matterCache = null;
444
+ var _filePathToIdCache = null;
444
445
  var _fileIndexMtimeMs = 0;
446
+ function toCanonicalPath(inputPath) {
447
+ return normalize(resolve(inputPath));
448
+ }
445
449
  function buildFileCache(catalogDir) {
450
+ const canonicalCatalogDir = toCanonicalPath(catalogDir);
446
451
  const files = globSync2("**/index.{md,mdx}", {
447
- cwd: catalogDir,
452
+ cwd: canonicalCatalogDir,
448
453
  ignore: ["node_modules/**"],
449
454
  absolute: true,
450
455
  nodir: true
451
456
  }).map(normalize);
452
457
  const index = /* @__PURE__ */ new Map();
453
458
  const matterResults = /* @__PURE__ */ new Map();
459
+ const pathToId = /* @__PURE__ */ new Map();
454
460
  for (const file of files) {
455
461
  const content = fsSync.readFileSync(file, "utf-8");
456
462
  const parsed = matter(content);
457
463
  matterResults.set(file, parsed);
458
464
  const id = parsed.data.id;
459
465
  if (!id) continue;
466
+ const resourceId = String(id);
460
467
  const version = parsed.data.version || "";
461
468
  const isVersioned = file.includes("versioned");
462
- const entry = { path: file, id, version: String(version), isVersioned };
463
- const existing = index.get(id);
469
+ const entry = { path: file, id: resourceId, version: String(version), isVersioned };
470
+ pathToId.set(file, resourceId);
471
+ const existing = index.get(resourceId);
464
472
  if (existing) {
465
473
  existing.push(entry);
466
474
  } else {
467
- index.set(id, [entry]);
475
+ index.set(resourceId, [entry]);
468
476
  }
469
477
  }
470
478
  _fileIndexCache = index;
471
- _fileIndexCatalogDir = catalogDir;
479
+ _fileIndexCatalogDir = canonicalCatalogDir;
472
480
  _matterCache = matterResults;
481
+ _filePathToIdCache = pathToId;
473
482
  try {
474
- _fileIndexMtimeMs = fsSync.statSync(catalogDir).mtimeMs;
483
+ _fileIndexMtimeMs = fsSync.statSync(canonicalCatalogDir).mtimeMs;
475
484
  } catch {
476
485
  _fileIndexMtimeMs = 0;
477
486
  }
478
487
  }
479
488
  function ensureFileCache(catalogDir) {
480
- if (!_fileIndexCache || _fileIndexCatalogDir !== catalogDir) {
489
+ const canonicalCatalogDir = toCanonicalPath(catalogDir);
490
+ if (!_fileIndexCache || _fileIndexCatalogDir !== canonicalCatalogDir) {
481
491
  buildFileCache(catalogDir);
482
492
  return;
483
493
  }
484
494
  try {
485
- const currentMtime = fsSync.statSync(catalogDir).mtimeMs;
495
+ const currentMtime = fsSync.statSync(canonicalCatalogDir).mtimeMs;
486
496
  if (currentMtime !== _fileIndexMtimeMs) {
487
497
  buildFileCache(catalogDir);
488
498
  }
@@ -494,6 +504,47 @@ function invalidateFileCache() {
494
504
  _fileIndexCache = null;
495
505
  _fileIndexCatalogDir = null;
496
506
  _matterCache = null;
507
+ _filePathToIdCache = null;
508
+ }
509
+ function upsertFileCacheEntry(catalogDir, filePath, rawContent) {
510
+ const canonicalCatalogDir = toCanonicalPath(catalogDir);
511
+ if (!_fileIndexCache || !_matterCache || !_filePathToIdCache || _fileIndexCatalogDir !== canonicalCatalogDir) {
512
+ return;
513
+ }
514
+ const normalizedPath = toCanonicalPath(filePath);
515
+ const parsed = matter(rawContent);
516
+ const previousId = _filePathToIdCache.get(normalizedPath);
517
+ if (previousId) {
518
+ const previousEntries = _fileIndexCache.get(previousId) || [];
519
+ const nextEntries = previousEntries.filter((entry2) => entry2.path !== normalizedPath);
520
+ if (nextEntries.length === 0) {
521
+ _fileIndexCache.delete(previousId);
522
+ } else {
523
+ _fileIndexCache.set(previousId, nextEntries);
524
+ }
525
+ _filePathToIdCache.delete(normalizedPath);
526
+ }
527
+ _matterCache.set(normalizedPath, parsed);
528
+ const id = parsed.data.id;
529
+ if (!id) {
530
+ _filePathToIdCache.delete(normalizedPath);
531
+ return;
532
+ }
533
+ const resourceId = String(id);
534
+ const entry = {
535
+ path: normalizedPath,
536
+ id: resourceId,
537
+ version: String(parsed.data.version || ""),
538
+ isVersioned: normalizedPath.includes(`${pathSeparator}versioned${pathSeparator}`)
539
+ };
540
+ const entries = _fileIndexCache.get(resourceId) || [];
541
+ entries.push(entry);
542
+ _fileIndexCache.set(resourceId, entries);
543
+ _filePathToIdCache.set(normalizedPath, resourceId);
544
+ try {
545
+ _fileIndexMtimeMs = fsSync.statSync(canonicalCatalogDir).mtimeMs;
546
+ } catch {
547
+ }
497
548
  }
498
549
  function cachedMatterRead(filePath) {
499
550
  if (_matterCache) {
@@ -683,7 +734,7 @@ var writeResource = async (catalogDir, resource, options = {
683
734
  }
684
735
  const document = matter2.stringify(markdown.trim(), frontmatter);
685
736
  fsSync2.writeFileSync(lockPath, document);
686
- invalidateFileCache();
737
+ upsertFileCacheEntry(catalogDir, lockPath, document);
687
738
  } finally {
688
739
  await unlock(lockPath).catch(() => {
689
740
  });