@eventcatalog/sdk 2.27.0 → 2.27.2

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.d.mts CHANGED
@@ -1349,7 +1349,7 @@ type Cache = {
1349
1349
  };
1350
1350
  type HydrateOptions = {
1351
1351
  outDir: string;
1352
- localSource: string;
1352
+ localSource?: string;
1353
1353
  fetch: Fetcher;
1354
1354
  modes?: Record<string, 'hydrate' | 'reference'>;
1355
1355
  cache?: Cache;
@@ -3519,10 +3519,11 @@ declare const _default: (path: string) => {
3519
3519
  * ```
3520
3520
  */
3521
3521
  createSnapshot: (options?: SnapshotOptions) => Promise<SnapshotResult>;
3522
- buildIndex: ({ source, commit, hashContent }: {
3522
+ buildIndex: ({ source, commit, hashContent, includeFederated }: {
3523
3523
  source: string;
3524
3524
  commit: string;
3525
3525
  hashContent?: boolean;
3526
+ includeFederated?: boolean;
3526
3527
  }) => Promise<Index>;
3527
3528
  diffSnapshots: (snapshotAPath: string, snapshotBPath: string) => Promise<SnapshotDiff>;
3528
3529
  listSnapshots: () => Promise<SnapshotMeta[]>;
package/dist/index.d.ts CHANGED
@@ -1349,7 +1349,7 @@ type Cache = {
1349
1349
  };
1350
1350
  type HydrateOptions = {
1351
1351
  outDir: string;
1352
- localSource: string;
1352
+ localSource?: string;
1353
1353
  fetch: Fetcher;
1354
1354
  modes?: Record<string, 'hydrate' | 'reference'>;
1355
1355
  cache?: Cache;
@@ -3519,10 +3519,11 @@ declare const _default: (path: string) => {
3519
3519
  * ```
3520
3520
  */
3521
3521
  createSnapshot: (options?: SnapshotOptions) => Promise<SnapshotResult>;
3522
- buildIndex: ({ source, commit, hashContent }: {
3522
+ buildIndex: ({ source, commit, hashContent, includeFederated }: {
3523
3523
  source: string;
3524
3524
  commit: string;
3525
3525
  hashContent?: boolean;
3526
+ includeFederated?: boolean;
3526
3527
  }) => Promise<Index>;
3527
3528
  diffSnapshots: (snapshotAPath: string, snapshotBPath: string) => Promise<SnapshotDiff>;
3528
3529
  listSnapshots: () => Promise<SnapshotMeta[]>;
package/dist/index.js CHANGED
@@ -901,6 +901,8 @@ var import_semver4 = require("semver");
901
901
  var import_proper_lockfile = require("proper-lockfile");
902
902
  var import_node_path2 = require("path");
903
903
  var import_node_path3 = __toESM(require("path"));
904
+ var resourceSourcePaths = /* @__PURE__ */ new WeakMap();
905
+ var getResourceSourcePath = (resource) => resourceSourcePaths.get(resource);
904
906
  var versionResource = async (catalogDir, id) => {
905
907
  const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
906
908
  const matchedFiles = await searchFilesForId(files, id);
@@ -1046,10 +1048,12 @@ var getResources = async (catalogDir, {
1046
1048
  }
1047
1049
  }
1048
1050
  }
1049
- return {
1051
+ const resource = {
1050
1052
  ...data,
1051
1053
  markdown: content.trim()
1052
1054
  };
1055
+ resourceSourcePaths.set(resource, file);
1056
+ return resource;
1053
1057
  });
1054
1058
  };
1055
1059
  var rmResourceById = async (catalogDir, id, version, options) => {
@@ -3805,6 +3809,7 @@ var listSnapshots = (directory) => async () => {
3805
3809
  var import_node_crypto2 = require("crypto");
3806
3810
  var import_promises18 = __toESM(require("fs/promises"));
3807
3811
  var import_node_path26 = __toESM(require("path"));
3812
+ var import_gray_matter8 = __toESM(require("gray-matter"));
3808
3813
  var import_ignore = __toESM(require("ignore"));
3809
3814
  var import_semver8 = require("semver");
3810
3815
 
@@ -3867,6 +3872,7 @@ var compareVersions = (left, right) => {
3867
3872
  };
3868
3873
  var compareIndexResources = (left, right) => compareText(left.type, right.type) || compareText(left.id, right.id) || compareVersions(left.version, right.version);
3869
3874
  var toCatalogPath = (directory, filePath) => import_node_path26.default.relative(directory, filePath).split(import_node_path26.default.sep).join("/");
3875
+ var isFederatedPath = (directory, filePath) => toCatalogPath(directory, filePath).startsWith("federated/");
3870
3876
  var loadIgnoreRules = async (directory) => {
3871
3877
  try {
3872
3878
  return (0, import_ignore.default)().add(await import_promises18.default.readFile(import_node_path26.default.join(directory, ".eventcatalogignore"), "utf8"));
@@ -3875,6 +3881,17 @@ var loadIgnoreRules = async (directory) => {
3875
3881
  throw error;
3876
3882
  }
3877
3883
  };
3884
+ var getFederatedDirectoryResources = async (directory, type, resourceDirectory, ignoreRules) => {
3885
+ const files = await getFiles(import_node_path26.default.join(directory, "federated", "**", resourceDirectory, "*.{md,mdx}"));
3886
+ const resourcePathPattern = new RegExp(`^(?:federated/[^/]+/)+${resourceDirectory}/[^/]+\\.mdx?$`, "i");
3887
+ return files.filter((file) => {
3888
+ const catalogPath = toCatalogPath(directory, file);
3889
+ return resourcePathPattern.test(catalogPath) && !ignoreRules?.ignores(catalogPath);
3890
+ }).map((sourcePath) => {
3891
+ const { data } = import_gray_matter8.default.read(sourcePath);
3892
+ return { type, resource: data, sourcePath };
3893
+ });
3894
+ };
3878
3895
  var normalizeAssets = async (directory, hashContent, ignoreRules) => {
3879
3896
  const collectFiles = async (currentDirectory) => {
3880
3897
  let entries;
@@ -4032,7 +4049,8 @@ var toIndexResource = async (directory, type, resource, hashContent, sourcePath,
4032
4049
  ...resource.members === void 0 ? {} : { members: resource.members }
4033
4050
  };
4034
4051
  };
4035
- var buildIndex = (directory) => async ({ source, commit, hashContent = true }) => {
4052
+ var buildIndex = (directory) => async ({ source, commit, hashContent = true, includeFederated = true }) => {
4053
+ const ignoreRules = await loadIgnoreRules(directory);
4036
4054
  const [
4037
4055
  domains,
4038
4056
  channels,
@@ -4068,6 +4086,10 @@ var buildIndex = (directory) => async ({ source, commit, hashContent = true }) =
4068
4086
  getUsers(directory)(),
4069
4087
  getDiagrams(directory)()
4070
4088
  ]);
4089
+ const federatedDirectoryResources = includeFederated ? await Promise.all([
4090
+ getFederatedDirectoryResources(directory, "team", "teams", ignoreRules),
4091
+ getFederatedDirectoryResources(directory, "user", "users", ignoreRules)
4092
+ ]) : [];
4071
4093
  const indexableResources = [
4072
4094
  ...(domains ?? []).map((resource) => ({ type: "domain", resource })),
4073
4095
  ...(channels ?? []).map((resource) => ({ type: "channel", resource })),
@@ -4092,19 +4114,20 @@ var buildIndex = (directory) => async ({ source, commit, hashContent = true }) =
4092
4114
  resource,
4093
4115
  sourcePath: import_node_path26.default.join(directory, "users", `${resource.id}.mdx`)
4094
4116
  })),
4117
+ ...federatedDirectoryResources.flat(),
4095
4118
  ...(diagrams ?? []).map((resource) => ({ type: "diagram", resource }))
4096
4119
  ];
4097
- const locatedResources = await Promise.all(
4120
+ const locatedResources = (await Promise.all(
4098
4121
  indexableResources.map(async (entry) => {
4099
- const sourcePath = entry.sourcePath ?? (await getResourcePath(directory, entry.resource.id, entry.resource.version))?.fullPath;
4122
+ const sourcePath = entry.sourcePath ?? (includeFederated ? void 0 : getResourceSourcePath(entry.resource)) ?? (await getResourcePath(directory, entry.resource.id, entry.resource.version))?.fullPath;
4100
4123
  if (!sourcePath) throw new Error(`Cannot find ${entry.type} ${entry.resource.id} (${entry.resource.version})`);
4124
+ if (!includeFederated && isFederatedPath(directory, sourcePath)) return void 0;
4101
4125
  return { ...entry, sourcePath };
4102
4126
  })
4103
- );
4127
+ )).filter((entry) => entry !== void 0);
4104
4128
  const resourceDirectories = new Set(
4105
4129
  locatedResources.filter(({ sourcePath }) => /^index\.mdx?$/i.test(import_node_path26.default.basename(sourcePath))).map(({ sourcePath }) => import_node_path26.default.resolve(import_node_path26.default.dirname(sourcePath)))
4106
4130
  );
4107
- const ignoreRules = await loadIgnoreRules(directory);
4108
4131
  const resources = await Promise.all(
4109
4132
  locatedResources.map(
4110
4133
  ({ type, resource, sourcePath }) => toIndexResource(directory, type, resource, hashContent, sourcePath, resourceDirectories, ignoreRules)
@@ -4124,7 +4147,7 @@ var buildIndex = (directory) => async ({ source, commit, hashContent = true }) =
4124
4147
  var import_node_path27 = __toESM(require("path"));
4125
4148
  var import_node_fs10 = __toESM(require("fs"));
4126
4149
  var import_promises19 = __toESM(require("fs/promises"));
4127
- var import_gray_matter8 = __toESM(require("gray-matter"));
4150
+ var import_gray_matter9 = __toESM(require("gray-matter"));
4128
4151
  var writeChangelog = (catalogDir) => async (id, changelog, options = {}) => {
4129
4152
  const { version, format = "mdx" } = options;
4130
4153
  const resourceFile = await findFileById(catalogDir, id, version);
@@ -4142,7 +4165,7 @@ var writeChangelog = (catalogDir) => async (id, changelog, options = {}) => {
4142
4165
  delete fm.badges;
4143
4166
  }
4144
4167
  import_node_fs10.default.mkdirSync(resourceDir, { recursive: true });
4145
- const document = import_gray_matter8.default.stringify(markdown.trim(), fm);
4168
+ const document = import_gray_matter9.default.stringify(markdown.trim(), fm);
4146
4169
  import_node_fs10.default.writeFileSync(changelogPath, document);
4147
4170
  invalidateFileCache();
4148
4171
  };
@@ -4159,7 +4182,7 @@ var appendChangelog = (catalogDir) => async (id, changelog, options = {}) => {
4159
4182
  if (!existingPath) {
4160
4183
  return writeChangelog(catalogDir)(id, changelog, options);
4161
4184
  }
4162
- const existing = import_gray_matter8.default.read(existingPath);
4185
+ const existing = import_gray_matter9.default.read(existingPath);
4163
4186
  const existingContent = existing.content.trim();
4164
4187
  const newEntry = changelog.markdown.trim();
4165
4188
  const combined = `${newEntry}
@@ -4174,7 +4197,7 @@ ${existingContent}`;
4174
4197
  } else {
4175
4198
  delete fm.badges;
4176
4199
  }
4177
- const document = import_gray_matter8.default.stringify(combined, fm);
4200
+ const document = import_gray_matter9.default.stringify(combined, fm);
4178
4201
  import_node_fs10.default.writeFileSync(existingPath, document);
4179
4202
  invalidateFileCache();
4180
4203
  };
@@ -4187,7 +4210,7 @@ var getChangelog = (catalogDir) => async (id, options = {}) => {
4187
4210
  const mdPath = import_node_path27.default.join(resourceDir, "changelog.md");
4188
4211
  const changelogPath = import_node_fs10.default.existsSync(mdxPath) ? mdxPath : import_node_fs10.default.existsSync(mdPath) ? mdPath : void 0;
4189
4212
  if (!changelogPath) return void 0;
4190
- const { data, content } = import_gray_matter8.default.read(changelogPath);
4213
+ const { data, content } = import_gray_matter9.default.read(changelogPath);
4191
4214
  return { ...data, markdown: content.trim() };
4192
4215
  };
4193
4216
  var rmChangelog = (catalogDir) => async (id, options = {}) => {
@@ -4274,10 +4297,7 @@ var resolve2 = (indexes) => {
4274
4297
  const assets = [];
4275
4298
  const warnings = [];
4276
4299
  for (const [assetPath, candidates] of assetCandidatesByPath) {
4277
- candidates.sort(
4278
- (left, right) => compareText2(left.resolvedFrom.source, right.resolvedFrom.source) || compareText2(left.resolvedFrom.commit, right.resolvedFrom.commit)
4279
- );
4280
- const [winner] = candidates;
4300
+ const winner = candidates[candidates.length - 1];
4281
4301
  const sources = [...new Set(candidates.map((candidate) => candidate.resolvedFrom.source))].sort(compareText2);
4282
4302
  const hashes = new Set(candidates.map((candidate) => candidate.hash));
4283
4303
  const hashesCanBeCompared = candidates.every((candidate) => candidate.hash !== void 0);
@@ -4427,6 +4447,7 @@ var getSourceDirectory = (source) => {
4427
4447
  return `${slug}--${digest}`;
4428
4448
  };
4429
4449
  var getContentHash = (content) => `sha256:${(0, import_node_crypto3.createHash)("sha256").update(content).digest("hex")}`;
4450
+ var getHydrationTarget = (artifactPath) => artifactPath.replace(/^(?:federated\/[^/]+\/)+/, "");
4430
4451
  var getArtifactOutputPath = (artifact, outDir) => {
4431
4452
  const allowedDirectory = import_node_path28.default.resolve(artifact.shared ? outDir : import_node_path28.default.join(outDir, getSourceDirectory(artifact.source)));
4432
4453
  const portableTarget = artifact.target.replaceAll("\\", "/");
@@ -4466,7 +4487,7 @@ async function hydrate(graph, options) {
4466
4487
  const artifacts = [];
4467
4488
  for (const entity of graph.entities) {
4468
4489
  const { source, commit } = entity.resolvedFrom;
4469
- if (source === options.localSource) continue;
4490
+ if (options.localSource !== void 0 && source === options.localSource) continue;
4470
4491
  if (options.modes?.[source] === "reference") {
4471
4492
  result.referenced++;
4472
4493
  continue;
@@ -4477,31 +4498,44 @@ async function hydrate(graph, options) {
4477
4498
  commit,
4478
4499
  path: entity.contentPath,
4479
4500
  hash: entity.contentHash,
4480
- target: entity.contentPath
4501
+ target: getHydrationTarget(entity.contentPath)
4481
4502
  });
4482
4503
  const entityDirectory = import_node_path28.default.posix.dirname(entity.contentPath);
4483
4504
  for (const schema of entity.schemas ?? []) {
4484
4505
  if (!schema.path) continue;
4485
4506
  const artifactPath = import_node_path28.default.posix.join(entityDirectory, schema.path);
4486
- artifacts.push({ source, commit, path: artifactPath, hash: schema.hash, target: artifactPath });
4507
+ artifacts.push({ source, commit, path: artifactPath, hash: schema.hash, target: getHydrationTarget(artifactPath) });
4487
4508
  }
4488
4509
  for (const specification of entity.specifications ?? []) {
4489
4510
  const artifactPath = import_node_path28.default.posix.join(entityDirectory, specification.path);
4490
- artifacts.push({ source, commit, path: artifactPath, hash: specification.hash, target: artifactPath });
4511
+ artifacts.push({
4512
+ source,
4513
+ commit,
4514
+ path: artifactPath,
4515
+ hash: specification.hash,
4516
+ target: getHydrationTarget(artifactPath)
4517
+ });
4491
4518
  }
4492
4519
  for (const sidecar of entity.sidecars ?? []) {
4493
- artifacts.push({ source, commit, path: sidecar.path, hash: sidecar.hash, target: sidecar.path });
4520
+ artifacts.push({
4521
+ source,
4522
+ commit,
4523
+ path: sidecar.path,
4524
+ hash: sidecar.hash,
4525
+ target: getHydrationTarget(sidecar.path)
4526
+ });
4494
4527
  }
4495
4528
  }
4496
4529
  for (const asset of graph.assets) {
4497
4530
  const { source, commit } = asset.resolvedFrom;
4498
- if (source === options.localSource || options.modes?.[source] === "reference") continue;
4531
+ if (options.localSource !== void 0 && source === options.localSource || options.modes?.[source] === "reference")
4532
+ continue;
4499
4533
  artifacts.push({
4500
4534
  source,
4501
4535
  commit,
4502
4536
  path: asset.path,
4503
4537
  hash: asset.hash,
4504
- target: asset.path,
4538
+ target: getHydrationTarget(asset.path),
4505
4539
  shared: true
4506
4540
  });
4507
4541
  }