@eventcatalog/sdk 1.4.6 → 1.4.7

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
@@ -99,6 +99,7 @@ import matter2 from "gray-matter";
99
99
  import fs2 from "node:fs/promises";
100
100
  import fsSync2 from "node:fs";
101
101
  import { satisfies as satisfies2 } from "semver";
102
+ import { lock, unlock } from "proper-lockfile";
102
103
  var versionResource = async (catalogDir, id) => {
103
104
  const files = await getFiles(`${catalogDir}/**/index.md`);
104
105
  const matchedFiles = await searchFilesForId(files, id);
@@ -130,24 +131,39 @@ var writeResource = async (catalogDir, resource, options = {
130
131
  versionExistingContent: false
131
132
  }) => {
132
133
  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`);
134
+ const fullPath = join2(catalogDir, path);
135
+ fsSync2.mkdirSync(fullPath, { recursive: true });
136
+ const lockPath = join2(fullPath, "index.md");
137
+ if (!fsSync2.existsSync(lockPath)) {
138
+ fsSync2.writeFileSync(lockPath, "");
136
139
  }
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}`);
140
+ try {
141
+ await lock(lockPath, {
142
+ retries: 5,
143
+ stale: 1e4
144
+ // 10 seconds
145
+ });
146
+ const exists = await versionExists(catalogDir, resource.id, resource.version);
147
+ if (exists && !options.override) {
148
+ throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
149
+ }
150
+ const { markdown, ...frontmatter } = resource;
151
+ if (options.versionExistingContent && !exists) {
152
+ const currentResource = await getResource(catalogDir, resource.id);
153
+ if (currentResource) {
154
+ if (satisfies2(resource.version, `>${currentResource.version}`)) {
155
+ await versionResource(catalogDir, resource.id);
156
+ } else {
157
+ throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
158
+ }
145
159
  }
146
160
  }
161
+ const document = matter2.stringify(markdown.trim(), frontmatter);
162
+ fsSync2.writeFileSync(lockPath, document);
163
+ } finally {
164
+ await unlock(lockPath).catch(() => {
165
+ });
147
166
  }
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
167
  };
152
168
  var getResource = async (catalogDir, id, version, options) => {
153
169
  const file = await findFileById(catalogDir, id, version);