@eventcatalog/sdk 1.4.6 → 1.4.8

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
@@ -2,12 +2,11 @@
2
2
  import { join as join11 } from "node:path";
3
3
 
4
4
  // src/events.ts
5
- import fs3 from "node:fs/promises";
5
+ import fs2 from "node:fs/promises";
6
6
  import { join as join3 } from "node:path";
7
7
 
8
8
  // src/internal/utils.ts
9
- import { glob } from "glob";
10
- import fs from "node:fs/promises";
9
+ import { globSync } from "glob";
11
10
  import fsSync from "node:fs";
12
11
  import { copy } from "fs-extra";
13
12
  import { join } from "node:path";
@@ -45,7 +44,7 @@ var findFileById = async (catalogDir, id, version) => {
45
44
  var getFiles = async (pattern, ignore = "") => {
46
45
  try {
47
46
  const ignoreList = Array.isArray(ignore) ? ignore : [ignore];
48
- const files = await glob(pattern, { ignore: ["node_modules/**", ...ignoreList] });
47
+ const files = globSync(pattern, { ignore: ["node_modules/**", ...ignoreList] });
49
48
  return files;
50
49
  } catch (error) {
51
50
  throw new Error(`Error finding files: ${error}`);
@@ -54,18 +53,16 @@ var getFiles = async (pattern, ignore = "") => {
54
53
  var searchFilesForId = async (files, id, version) => {
55
54
  const idRegex = new RegExp(`^id:\\s*(['"]|>-)?\\s*${id}['"]?\\s*$`, "m");
56
55
  const versionRegex = new RegExp(`^version:\\s*['"]?${version}['"]?\\s*$`, "m");
57
- const matches = await Promise.all(
58
- files.map(async (file) => {
59
- const content = await fs.readFile(file, "utf-8");
60
- const hasIdMatch = content.match(idRegex);
61
- if (version && !content.match(versionRegex)) {
62
- return void 0;
63
- }
64
- if (hasIdMatch) {
65
- return file;
66
- }
67
- })
68
- );
56
+ const matches = files.map((file) => {
57
+ const content = fsSync.readFileSync(file, "utf-8");
58
+ const hasIdMatch = content.match(idRegex);
59
+ if (version && !content.match(versionRegex)) {
60
+ return void 0;
61
+ }
62
+ if (hasIdMatch) {
63
+ return file;
64
+ }
65
+ });
69
66
  return matches.filter(Boolean).filter((file) => file !== void 0);
70
67
  };
71
68
  var copyDir = async (catalogDir, source, target, filter) => {
@@ -96,9 +93,10 @@ var uniqueVersions = (messages) => {
96
93
  // src/internal/resources.ts
97
94
  import { dirname, join as join2 } from "path";
98
95
  import matter2 from "gray-matter";
99
- import fs2 from "node:fs/promises";
96
+ import fs from "node:fs/promises";
100
97
  import fsSync2 from "node:fs";
101
98
  import { satisfies as satisfies2 } from "semver";
99
+ import { lock, unlock } from "proper-lockfile";
102
100
  var versionResource = async (catalogDir, id) => {
103
101
  const files = await getFiles(`${catalogDir}/**/index.md`);
104
102
  const matchedFiles = await searchFilesForId(files, id);
@@ -113,7 +111,7 @@ var versionResource = async (catalogDir, id) => {
113
111
  await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
114
112
  return !src.includes("versioned");
115
113
  });
116
- await fs2.readdir(sourceDirectory).then(async (resourceFiles) => {
114
+ await fs.readdir(sourceDirectory).then(async (resourceFiles) => {
117
115
  await Promise.all(
118
116
  resourceFiles.map(async (file2) => {
119
117
  if (file2 !== "versioned") {
@@ -130,24 +128,39 @@ var writeResource = async (catalogDir, resource, options = {
130
128
  versionExistingContent: false
131
129
  }) => {
132
130
  const path = options.path || `/${resource.id}`;
133
- const exists = await versionExists(catalogDir, resource.id, resource.version);
134
- if (exists && !options.override) {
135
- throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
131
+ const fullPath = join2(catalogDir, path);
132
+ fsSync2.mkdirSync(fullPath, { recursive: true });
133
+ const lockPath = join2(fullPath, "index.md");
134
+ if (!fsSync2.existsSync(lockPath)) {
135
+ fsSync2.writeFileSync(lockPath, "");
136
136
  }
137
- const { markdown, ...frontmatter } = resource;
138
- if (options.versionExistingContent && !exists) {
139
- const currentResource = await getResource(catalogDir, resource.id);
140
- if (currentResource) {
141
- if (satisfies2(resource.version, `>${currentResource.version}`)) {
142
- await versionResource(catalogDir, resource.id);
143
- } else {
144
- throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
137
+ try {
138
+ await lock(lockPath, {
139
+ retries: 5,
140
+ stale: 1e4
141
+ // 10 seconds
142
+ });
143
+ const exists = await versionExists(catalogDir, resource.id, resource.version);
144
+ if (exists && !options.override) {
145
+ throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
146
+ }
147
+ const { markdown, ...frontmatter } = resource;
148
+ if (options.versionExistingContent && !exists) {
149
+ const currentResource = await getResource(catalogDir, resource.id);
150
+ if (currentResource) {
151
+ if (satisfies2(resource.version, `>${currentResource.version}`)) {
152
+ await versionResource(catalogDir, resource.id);
153
+ } else {
154
+ throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
155
+ }
145
156
  }
146
157
  }
158
+ const document = matter2.stringify(markdown.trim(), frontmatter);
159
+ fsSync2.writeFileSync(lockPath, document);
160
+ } finally {
161
+ await unlock(lockPath).catch(() => {
162
+ });
147
163
  }
148
- const document = matter2.stringify(markdown.trim(), frontmatter);
149
- fsSync2.mkdirSync(join2(catalogDir, path), { recursive: true });
150
- fsSync2.writeFileSync(join2(catalogDir, path, "index.md"), document);
151
164
  };
152
165
  var getResource = async (catalogDir, id, version, options) => {
153
166
  const file = await findFileById(catalogDir, id, version);
@@ -179,14 +192,14 @@ var rmResourceById = async (catalogDir, id, version, options) => {
179
192
  if (options?.persistFiles) {
180
193
  await Promise.all(
181
194
  matchedFiles.map(async (file) => {
182
- await fs2.rm(file, { recursive: true });
195
+ await fs.rm(file, { recursive: true });
183
196
  })
184
197
  );
185
198
  } else {
186
199
  await Promise.all(
187
200
  matchedFiles.map(async (file) => {
188
201
  const directory = dirname(file);
189
- await fs2.rm(directory, { recursive: true, force: true });
202
+ await fs.rm(directory, { recursive: true, force: true });
190
203
  })
191
204
  );
192
205
  }
@@ -199,7 +212,7 @@ var addFileToResource = async (catalogDir, id, file, version) => {
199
212
  var getFileFromResource = async (catalogDir, id, file, version) => {
200
213
  const pathToResource = await findFileById(catalogDir, id, version);
201
214
  if (!pathToResource) throw new Error("Cannot find directory of resource");
202
- const exists = await fs2.access(join2(dirname(pathToResource), file.fileName)).then(() => true).catch(() => false);
215
+ const exists = await fs.access(join2(dirname(pathToResource), file.fileName)).then(() => true).catch(() => false);
203
216
  if (!exists) throw new Error(`File ${file.fileName} does not exist in resource ${id} v(${version})`);
204
217
  return fsSync2.readFileSync(join2(dirname(pathToResource), file.fileName), "utf-8");
205
218
  };
@@ -217,7 +230,7 @@ var writeEventToService = (directory) => async (event, service, options = { path
217
230
  await writeResource(directory, { ...event }, { ...options, path: pathForEvent, type: "event" });
218
231
  };
219
232
  var rmEvent = (directory) => async (path) => {
220
- await fs3.rm(join3(directory, path), { recursive: true });
233
+ await fs2.rm(join3(directory, path), { recursive: true });
221
234
  };
222
235
  var rmEventById = (directory) => async (id, version, persistFiles) => {
223
236
  await rmResourceById(directory, id, version, { type: "event", persistFiles });
@@ -233,7 +246,7 @@ var eventHasVersion = (directory) => async (id, version) => {
233
246
  };
234
247
 
235
248
  // src/commands.ts
236
- import fs4 from "node:fs/promises";
249
+ import fs3 from "node:fs/promises";
237
250
  import { join as join4 } from "node:path";
238
251
  var getCommand = (directory) => async (id, version) => getResource(directory, id, version, { type: "command" });
239
252
  var getCommands = (directory) => async (options) => getResources(directory, { type: "commands", ...options });
@@ -244,7 +257,7 @@ var writeCommandToService = (directory) => async (command, service, options = {
244
257
  await writeResource(directory, { ...command }, { ...options, path: pathForEvent, type: "command" });
245
258
  };
246
259
  var rmCommand = (directory) => async (path) => {
247
- await fs4.rm(join4(directory, path), { recursive: true });
260
+ await fs3.rm(join4(directory, path), { recursive: true });
248
261
  };
249
262
  var rmCommandById = (directory) => async (id, version, persistFiles) => rmResourceById(directory, id, version, { type: "command", persistFiles });
250
263
  var versionCommand = (directory) => async (id) => versionResource(directory, id);
@@ -258,7 +271,7 @@ var commandHasVersion = (directory) => async (id, version) => {
258
271
  };
259
272
 
260
273
  // src/queries.ts
261
- import fs5 from "node:fs/promises";
274
+ import fs4 from "node:fs/promises";
262
275
  import { join as join5 } from "node:path";
263
276
  var getQuery = (directory) => async (id, version) => getResource(directory, id, version, { type: "query" });
264
277
  var writeQuery = (directory) => async (query, options = { path: "" }) => writeResource(directory, { ...query }, { ...options, type: "query" });
@@ -269,7 +282,7 @@ var writeQueryToService = (directory) => async (query, service, options = { path
269
282
  await writeResource(directory, { ...query }, { ...options, path: pathForQuery, type: "query" });
270
283
  };
271
284
  var rmQuery = (directory) => async (path) => {
272
- await fs5.rm(join5(directory, path), { recursive: true });
285
+ await fs4.rm(join5(directory, path), { recursive: true });
273
286
  };
274
287
  var rmQueryById = (directory) => async (id, version, persistFiles) => {
275
288
  await rmResourceById(directory, id, version, { type: "query", persistFiles });
@@ -285,7 +298,7 @@ var queryHasVersion = (directory) => async (id, version) => {
285
298
  };
286
299
 
287
300
  // src/services.ts
288
- import fs6 from "node:fs/promises";
301
+ import fs5 from "node:fs/promises";
289
302
  import { join as join6, dirname as dirname2 } from "node:path";
290
303
  var getService = (directory) => async (id, version) => getResource(directory, id, version, { type: "service" });
291
304
  var getServices = (directory) => async (options) => getResources(directory, {
@@ -315,7 +328,7 @@ var writeServiceToDomain = (directory) => async (service, domain, options = { pa
315
328
  };
316
329
  var versionService = (directory) => async (id) => versionResource(directory, id);
317
330
  var rmService = (directory) => async (path) => {
318
- await fs6.rm(join6(directory, path), { recursive: true });
331
+ await fs5.rm(join6(directory, path), { recursive: true });
319
332
  };
320
333
  var rmServiceById = (directory) => async (id, version, persistFiles) => rmResourceById(directory, id, version, { type: "service", persistFiles });
321
334
  var addFileToService = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
@@ -379,7 +392,7 @@ var serviceHasVersion = (directory) => async (id, version) => {
379
392
  };
380
393
 
381
394
  // src/domains.ts
382
- import fs7 from "node:fs/promises";
395
+ import fs6 from "node:fs/promises";
383
396
  import { join as join7 } from "node:path";
384
397
  var getDomain = (directory) => async (id, version) => getResource(directory, id, version, { type: "domain" });
385
398
  var getDomains = (directory) => async (options) => getResources(directory, {
@@ -396,7 +409,7 @@ var writeDomain = (directory) => async (domain, options = { path: "" }) => {
396
409
  };
397
410
  var versionDomain = (directory) => async (id) => versionResource(directory, id);
398
411
  var rmDomain = (directory) => async (path) => {
399
- await fs7.rm(join7(directory, path), { recursive: true });
412
+ await fs6.rm(join7(directory, path), { recursive: true });
400
413
  };
401
414
  var rmDomainById = (directory) => async (id, version, persistFiles) => rmResourceById(directory, id, version, { type: "domain", persistFiles });
402
415
  var addFileToDomain = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
@@ -419,13 +432,13 @@ var addServiceToDomain = (directory) => async (id, service, version) => {
419
432
  };
420
433
 
421
434
  // src/channels.ts
422
- import fs8 from "node:fs/promises";
435
+ import fs7 from "node:fs/promises";
423
436
  import { join as join8 } from "node:path";
424
437
  var getChannel = (directory) => async (id, version) => getResource(directory, id, version, { type: "channel" });
425
438
  var getChannels = (directory) => async (options) => getResources(directory, { type: "channels", ...options });
426
439
  var writeChannel = (directory) => async (channel, options = { path: "" }) => writeResource(directory, { ...channel }, { ...options, type: "channel" });
427
440
  var rmChannel = (directory) => async (path) => {
428
- await fs8.rm(join8(directory, path), { recursive: true });
441
+ await fs7.rm(join8(directory, path), { recursive: true });
429
442
  };
430
443
  var rmChannelById = (directory) => async (id, version, persistFiles) => rmResourceById(directory, id, version, { type: "channel", persistFiles });
431
444
  var versionChannel = (directory) => async (id) => versionResource(directory, id);
@@ -471,7 +484,7 @@ var addMessageToChannel = (directory, collection) => async (id, _message, versio
471
484
  };
472
485
 
473
486
  // src/teams.ts
474
- import fs9 from "node:fs/promises";
487
+ import fs8 from "node:fs/promises";
475
488
  import fsSync3 from "node:fs";
476
489
  import { join as join9 } from "node:path";
477
490
  import matter3 from "gray-matter";
@@ -513,7 +526,7 @@ var writeTeam = (catalogDir) => async (team, options = {}) => {
513
526
  fsSync3.writeFileSync(join9(catalogDir, "", `${resource.id}.md`), document);
514
527
  };
515
528
  var rmTeamById = (catalogDir) => async (id) => {
516
- await fs9.rm(join9(catalogDir, `${id}.md`), { recursive: true });
529
+ await fs8.rm(join9(catalogDir, `${id}.md`), { recursive: true });
517
530
  };
518
531
 
519
532
  // src/users.ts