@eventcatalog/sdk 2.11.0 → 2.12.1

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.
Files changed (114) hide show
  1. package/README.md +86 -1
  2. package/dist/cli/index.d.mts +1 -0
  3. package/dist/cli/index.d.ts +1 -0
  4. package/dist/{eventcatalog.js → cli/index.js} +588 -378
  5. package/dist/cli/index.js.map +1 -0
  6. package/dist/{eventcatalog.mjs → cli/index.mjs} +666 -436
  7. package/dist/cli/index.mjs.map +1 -0
  8. package/dist/cli-docs.d.mts +43 -0
  9. package/dist/cli-docs.d.ts +43 -0
  10. package/dist/cli-docs.js +1614 -0
  11. package/dist/cli-docs.js.map +1 -0
  12. package/dist/cli-docs.mjs +1586 -0
  13. package/dist/cli-docs.mjs.map +1 -0
  14. package/dist/index.d.mts +443 -25
  15. package/dist/index.d.ts +443 -25
  16. package/dist/index.js +204 -121
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +204 -121
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +13 -4
  21. package/dist/channels.d.mts +0 -214
  22. package/dist/channels.d.ts +0 -214
  23. package/dist/channels.js +0 -432
  24. package/dist/channels.js.map +0 -1
  25. package/dist/channels.mjs +0 -384
  26. package/dist/channels.mjs.map +0 -1
  27. package/dist/commands.d.mts +0 -274
  28. package/dist/commands.d.ts +0 -274
  29. package/dist/commands.js +0 -394
  30. package/dist/commands.js.map +0 -1
  31. package/dist/commands.mjs +0 -350
  32. package/dist/commands.mjs.map +0 -1
  33. package/dist/containers.d.mts +0 -179
  34. package/dist/containers.d.ts +0 -179
  35. package/dist/containers.js +0 -388
  36. package/dist/containers.js.map +0 -1
  37. package/dist/containers.mjs +0 -345
  38. package/dist/containers.mjs.map +0 -1
  39. package/dist/custom-docs.d.mts +0 -89
  40. package/dist/custom-docs.d.ts +0 -89
  41. package/dist/custom-docs.js +0 -164
  42. package/dist/custom-docs.js.map +0 -1
  43. package/dist/custom-docs.mjs +0 -122
  44. package/dist/custom-docs.mjs.map +0 -1
  45. package/dist/data-products.d.mts +0 -225
  46. package/dist/data-products.d.ts +0 -225
  47. package/dist/data-products.js +0 -375
  48. package/dist/data-products.js.map +0 -1
  49. package/dist/data-products.mjs +0 -332
  50. package/dist/data-products.mjs.map +0 -1
  51. package/dist/data-stores.d.mts +0 -179
  52. package/dist/data-stores.d.ts +0 -179
  53. package/dist/data-stores.js +0 -401
  54. package/dist/data-stores.js.map +0 -1
  55. package/dist/data-stores.mjs +0 -356
  56. package/dist/data-stores.mjs.map +0 -1
  57. package/dist/domains.d.mts +0 -347
  58. package/dist/domains.d.ts +0 -347
  59. package/dist/domains.js +0 -547
  60. package/dist/domains.js.map +0 -1
  61. package/dist/domains.mjs +0 -498
  62. package/dist/domains.mjs.map +0 -1
  63. package/dist/entities.d.mts +0 -174
  64. package/dist/entities.d.ts +0 -174
  65. package/dist/entities.js +0 -348
  66. package/dist/entities.js.map +0 -1
  67. package/dist/entities.mjs +0 -307
  68. package/dist/entities.mjs.map +0 -1
  69. package/dist/eventcatalog.d.mts +0 -21
  70. package/dist/eventcatalog.d.ts +0 -21
  71. package/dist/eventcatalog.js.map +0 -1
  72. package/dist/eventcatalog.mjs.map +0 -1
  73. package/dist/events.d.mts +0 -276
  74. package/dist/events.d.ts +0 -276
  75. package/dist/events.js +0 -393
  76. package/dist/events.js.map +0 -1
  77. package/dist/events.mjs +0 -349
  78. package/dist/events.mjs.map +0 -1
  79. package/dist/messages.d.mts +0 -65
  80. package/dist/messages.d.ts +0 -65
  81. package/dist/messages.js +0 -312
  82. package/dist/messages.js.map +0 -1
  83. package/dist/messages.mjs +0 -269
  84. package/dist/messages.mjs.map +0 -1
  85. package/dist/queries.d.mts +0 -276
  86. package/dist/queries.d.ts +0 -276
  87. package/dist/queries.js +0 -394
  88. package/dist/queries.js.map +0 -1
  89. package/dist/queries.mjs +0 -350
  90. package/dist/queries.mjs.map +0 -1
  91. package/dist/services.d.mts +0 -384
  92. package/dist/services.d.ts +0 -384
  93. package/dist/services.js +0 -577
  94. package/dist/services.js.map +0 -1
  95. package/dist/services.mjs +0 -526
  96. package/dist/services.mjs.map +0 -1
  97. package/dist/teams.d.mts +0 -90
  98. package/dist/teams.d.ts +0 -90
  99. package/dist/teams.js +0 -246
  100. package/dist/teams.js.map +0 -1
  101. package/dist/teams.mjs +0 -202
  102. package/dist/teams.mjs.map +0 -1
  103. package/dist/types.d.d.mts +0 -331
  104. package/dist/types.d.d.ts +0 -331
  105. package/dist/types.d.js +0 -19
  106. package/dist/types.d.js.map +0 -1
  107. package/dist/types.d.mjs +0 -1
  108. package/dist/types.d.mjs.map +0 -1
  109. package/dist/users.d.mts +0 -83
  110. package/dist/users.d.ts +0 -83
  111. package/dist/users.js +0 -127
  112. package/dist/users.js.map +0 -1
  113. package/dist/users.mjs +0 -88
  114. package/dist/users.mjs.map +0 -1
package/dist/queries.d.ts DELETED
@@ -1,276 +0,0 @@
1
- import { Query } from './types.d.js';
2
-
3
- /**
4
- * Returns a query from EventCatalog.
5
- *
6
- * You can optionally specify a version to get a specific version of the query
7
- *
8
- * @example
9
- * ```ts
10
- * import utils from '@eventcatalog/utils';
11
- *
12
- * const { getQuery } = utils('/path/to/eventcatalog');
13
- *
14
- * // Gets the latest version of the event
15
- * const event = await getQuery('GetOrder');
16
- *
17
- * // Gets a version of the event
18
- * const event = await getQuery('GetOrder', '0.0.1');
19
- *
20
- * // Gets the query with the schema attached
21
- * const event = await getQuery('GetOrder', '0.0.1', { attachSchema: true });
22
- * ```
23
- */
24
- declare const getQuery: (directory: string) => (id: string, version?: string, options?: {
25
- attachSchema?: boolean;
26
- }) => Promise<Query>;
27
- /**
28
- * Write a query to EventCatalog.
29
- *
30
- * You can optionally override the path of the query.
31
- *
32
- * @example
33
- * ```ts
34
- * import utils from '@eventcatalog/utils';
35
- *
36
- * const { writeQuery } = utils('/path/to/eventcatalog');
37
- *
38
- * // Write an event to the catalog
39
- * // Event would be written to queries/GetOrder
40
- * await writeQuery({
41
- * id: 'GetOrder',
42
- * name: 'Get Order',
43
- * version: '0.0.1',
44
- * summary: 'This is a summary',
45
- * markdown: '# Hello world',
46
- * });
47
- *
48
- * // Write an event to the catalog but override the path
49
- * // Event would be written to queries/Inventory/GetOrder
50
- * await writeQuery({
51
- * id: 'GetOrder',
52
- * name: 'Get Order',
53
- * version: '0.0.1',
54
- * summary: 'This is a summary',
55
- * markdown: '# Hello world',
56
- * }, { path: "/Orders/GetOrder"});
57
- *
58
- * // Write a query to the catalog and override the existing content (if there is any)
59
- * await writeQuery({
60
- * id: 'GetOrder',
61
- * name: 'Get Order',
62
- * version: '0.0.1',
63
- * summary: 'This is a summary',
64
- * markdown: '# Hello world',
65
- * }, { override: true });
66
- *
67
- * // Write a query to the catalog and version the previous version
68
- * // only works if the new version is greater than the previous version
69
- * await writeQuery({
70
- * id: 'GetOrder',
71
- * name: 'Get Order',
72
- * version: '0.0.1',
73
- * summary: 'This is a summary',
74
- * markdown: '# Hello world',
75
- * }, { versionExistingContent: true });
76
- *
77
- * ```
78
- */
79
- declare const writeQuery: (directory: string) => (query: Query, options?: {
80
- path?: string;
81
- override?: boolean;
82
- versionExistingContent?: boolean;
83
- format?: "md" | "mdx";
84
- }) => Promise<void>;
85
- /**
86
- * Returns all queries from EventCatalog.
87
- *
88
- * You can optionally specify if you want to get the latest version of the queries.
89
- *
90
- * @example
91
- * ```ts
92
- * import utils from '@eventcatalog/utils';
93
- *
94
- * const { getQueries } = utils('/path/to/eventcatalog');
95
- *
96
- * // Gets all queries (and versions) from the catalog
97
- * const queries = await getQueries();
98
- *
99
- * // Gets all queries (only latest version) from the catalog
100
- * const queries = await getQueries({ latestOnly: true });
101
- *
102
- * // Gets all queries with the schema attached
103
- * const queries = await getQueries({ attachSchema: true });
104
- * ```
105
- */
106
- declare const getQueries: (directory: string) => (options?: {
107
- latestOnly?: boolean;
108
- attachSchema?: boolean;
109
- }) => Promise<Query[]>;
110
- /**
111
- * Write a query to a service in EventCatalog.
112
- *
113
- * You can optionally override the path of the event.
114
- *
115
- * @example
116
- * ```ts
117
- * import utils from '@eventcatalog/utils';
118
- *
119
- * const { writeQueryToService } = utils('/path/to/eventcatalog');
120
- *
121
- * // Write an event to a given service in the catalog
122
- * // Event would be written to services/Orders/queries/GetOrder
123
- * await writeQueryToService({
124
- * id: 'GetOrder',
125
- * name: 'Get Order',
126
- * version: '0.0.1',
127
- * summary: 'This is a summary',
128
- * markdown: '# Hello world',
129
- * }, { id: 'Orders' });
130
- * ```
131
- */
132
- declare const writeQueryToService: (directory: string) => (query: Query, service: {
133
- id: string;
134
- version?: string;
135
- }, options?: {
136
- path?: string;
137
- format?: "md" | "mdx";
138
- override?: boolean;
139
- }) => Promise<void>;
140
- /**
141
- * Delete a query at it's given path.
142
- *
143
- * @example
144
- * ```ts
145
- * import utils from '@eventcatalog/utils';
146
- *
147
- * const { rmQuery } = utils('/path/to/eventcatalog');
148
- *
149
- * // removes an query at the given path (queries dir is appended to the given path)
150
- * // Removes the query at queries/GetOrders
151
- * await rmQuery('/GetOrders');
152
- * ```
153
- */
154
- declare const rmQuery: (directory: string) => (path: string) => Promise<void>;
155
- /**
156
- * Delete a query by it's id.
157
- *
158
- * Optionally specify a version to delete a specific version of the query.
159
- *
160
- * @example
161
- * ```ts
162
- * import utils from '@eventcatalog/utils';
163
- *
164
- * const { rmQueryById } = utils('/path/to/eventcatalog');
165
- *
166
- * // deletes the latest InventoryAdjusted query
167
- * await rmQueryById('GetOrder');
168
- *
169
- * // deletes a specific version of the GetOrder query
170
- * await rmQueryById('GetOrder', '0.0.1');
171
- * ```
172
- */
173
- declare const rmQueryById: (directory: string) => (id: string, version?: string, persistFiles?: boolean) => Promise<void>;
174
- /**
175
- * Version a query by it's id.
176
- *
177
- * Takes the latest query and moves it to a versioned directory.
178
- * All files with this query are also versioned (e.g /queries/GetOrder/schema.json)
179
- *
180
- * @example
181
- * ```ts
182
- * import utils from '@eventcatalog/utils';
183
- *
184
- * const { versionQuery } = utils('/path/to/eventcatalog');
185
- *
186
- * // moves the latest GetOrder query to a versioned directory
187
- * // the version within that query is used as the version number.
188
- * await versionQuery('GetOrder');
189
- *
190
- * ```
191
- */
192
- declare const versionQuery: (directory: string) => (id: string) => Promise<void>;
193
- /**
194
- * Add a file to a query by it's id.
195
- *
196
- * Optionally specify a version to add a file to a specific version of the query.
197
- *
198
- * @example
199
- * ```ts
200
- * import utils from '@eventcatalog/utils';
201
- *
202
- * const { addFileToQuery } = utils('/path/to/eventcatalog');
203
- *
204
- * // adds a file to the latest GetOrder query
205
- * await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' });
206
- *
207
- * // adds a file to a specific version of the GetOrder query
208
- * await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');
209
- *
210
- * ```
211
- */
212
- declare const addFileToQuery: (directory: string) => (id: string, file: {
213
- content: string;
214
- fileName: string;
215
- }, version?: string, options?: {
216
- path?: string;
217
- }) => Promise<void>;
218
- /**
219
- * Add a schema to a query by it's id.
220
- *
221
- * Optionally specify a version to add a schema to a specific version of the query.
222
- *
223
- * @example
224
- * ```ts
225
- * import utils from '@eventcatalog/utils';
226
- *
227
- * const { addSchemaToQuery } = utils('/path/to/eventcatalog');
228
- *
229
- * // JSON schema example
230
- * const schema = {
231
- * "$schema": "http://json-schema.org/draft-07/schema#",
232
- * "type": "object",
233
- * "properties": {
234
- * "name": {
235
- * "type": "string"
236
- * },
237
- * "age": {
238
- * "type": "number"
239
- * }
240
- * },
241
- * "required": ["name", "age"]
242
- * };
243
- *
244
- * // adds a schema to the latest GetOrder query
245
- * await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' });
246
- *
247
- * // adds a file to a specific version of the GetOrder query
248
- * await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' }, '0.0.1');
249
- *
250
- * ```
251
- */
252
- declare const addSchemaToQuery: (directory: string) => (id: string, schema: {
253
- schema: string;
254
- fileName: string;
255
- }, version?: string, options?: {
256
- path?: string;
257
- }) => Promise<void>;
258
- /**
259
- * Check to see if the catalog has a version for the given query.
260
- *
261
- * @example
262
- * ```ts
263
- * import utils from '@eventcatalog/utils';
264
- *
265
- * const { queryHasVersion } = utils('/path/to/eventcatalog');
266
- *
267
- * // returns true if version is found for the given event and version (supports semver)
268
- * await queryHasVersion('GetOrder', '0.0.1');
269
- * await queryHasVersion('GetOrder', 'latest');
270
- * await queryHasVersion('GetOrder', '0.0.x');*
271
- *
272
- * ```
273
- */
274
- declare const queryHasVersion: (directory: string) => (id: string, version?: string) => Promise<boolean>;
275
-
276
- export { addFileToQuery, addSchemaToQuery, getQueries, getQuery, queryHasVersion, rmQuery, rmQueryById, versionQuery, writeQuery, writeQueryToService };
package/dist/queries.js DELETED
@@ -1,394 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/queries.ts
31
- var queries_exports = {};
32
- __export(queries_exports, {
33
- addFileToQuery: () => addFileToQuery,
34
- addSchemaToQuery: () => addSchemaToQuery,
35
- getQueries: () => getQueries,
36
- getQuery: () => getQuery,
37
- queryHasVersion: () => queryHasVersion,
38
- rmQuery: () => rmQuery,
39
- rmQueryById: () => rmQueryById,
40
- versionQuery: () => versionQuery,
41
- writeQuery: () => writeQuery,
42
- writeQueryToService: () => writeQueryToService
43
- });
44
- module.exports = __toCommonJS(queries_exports);
45
- var import_promises2 = __toESM(require("fs/promises"));
46
- var import_node_path4 = require("path");
47
-
48
- // src/internal/utils.ts
49
- var import_glob = require("glob");
50
- var import_node_fs = __toESM(require("fs"));
51
- var import_fs_extra = require("fs-extra");
52
- var import_node_path = require("path");
53
- var import_gray_matter = __toESM(require("gray-matter"));
54
- var import_semver = require("semver");
55
- var versionExists = async (catalogDir, id, version) => {
56
- const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
57
- const matchedFiles = await searchFilesForId(files, id, version) || [];
58
- return matchedFiles.length > 0;
59
- };
60
- var findFileById = async (catalogDir, id, version) => {
61
- const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
62
- const matchedFiles = await searchFilesForId(files, id) || [];
63
- const latestVersion = matchedFiles.find((path2) => !path2.includes("versioned"));
64
- if (!version) {
65
- return latestVersion;
66
- }
67
- const parsedFiles = matchedFiles.map((path2) => {
68
- const { data } = import_gray_matter.default.read(path2);
69
- return { ...data, path: path2 };
70
- });
71
- if (version === "latest") {
72
- return latestVersion;
73
- }
74
- const exactMatch = parsedFiles.find((c) => c.version === version);
75
- if (exactMatch) {
76
- return exactMatch.path;
77
- }
78
- const semverRange = (0, import_semver.validRange)(version);
79
- if (semverRange) {
80
- const match = parsedFiles.filter((c) => {
81
- try {
82
- return (0, import_semver.satisfies)(c.version, semverRange);
83
- } catch (error) {
84
- return false;
85
- }
86
- });
87
- return match.length > 0 ? match[0].path : void 0;
88
- }
89
- return void 0;
90
- };
91
- var getFiles = async (pattern, ignore = "") => {
92
- try {
93
- const normalizedInputPattern = (0, import_node_path.normalize)(pattern);
94
- const absoluteBaseDir = (0, import_node_path.resolve)(
95
- normalizedInputPattern.includes("**") ? normalizedInputPattern.split("**")[0] : (0, import_node_path.dirname)(normalizedInputPattern)
96
- );
97
- let relativePattern = (0, import_node_path.relative)(absoluteBaseDir, normalizedInputPattern);
98
- relativePattern = relativePattern.replace(/\\/g, "/");
99
- const ignoreList = Array.isArray(ignore) ? ignore : [ignore];
100
- const files = (0, import_glob.globSync)(relativePattern, {
101
- cwd: absoluteBaseDir,
102
- ignore: ["node_modules/**", ...ignoreList],
103
- absolute: true,
104
- nodir: true
105
- });
106
- return files.map(import_node_path.normalize);
107
- } catch (error) {
108
- const absoluteBaseDirForError = (0, import_node_path.resolve)(
109
- (0, import_node_path.normalize)(pattern).includes("**") ? (0, import_node_path.normalize)(pattern).split("**")[0] : (0, import_node_path.dirname)((0, import_node_path.normalize)(pattern))
110
- );
111
- const relativePatternForError = (0, import_node_path.relative)(absoluteBaseDirForError, (0, import_node_path.normalize)(pattern)).replace(/\\/g, "/");
112
- throw new Error(
113
- `Error finding files for pattern "${pattern}" (using cwd: "${absoluteBaseDirForError}", globPattern: "${relativePatternForError}"): ${error.message}`
114
- );
115
- }
116
- };
117
- var searchFilesForId = async (files, id, version) => {
118
- const escapedId = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
119
- const idRegex = new RegExp(`^id:\\s*(['"]|>-)?\\s*${escapedId}['"]?\\s*$`, "m");
120
- const versionRegex = new RegExp(`^version:\\s*['"]?${version}['"]?\\s*$`, "m");
121
- const matches = files.map((file) => {
122
- const content = import_node_fs.default.readFileSync(file, "utf-8");
123
- const hasIdMatch = content.match(idRegex);
124
- if (version && !content.match(versionRegex)) {
125
- return void 0;
126
- }
127
- if (hasIdMatch) {
128
- return file;
129
- }
130
- });
131
- return matches.filter(Boolean).filter((file) => file !== void 0);
132
- };
133
- var copyDir = async (catalogDir, source, target, filter) => {
134
- const tmpDirectory = (0, import_node_path.join)(catalogDir, "tmp");
135
- import_node_fs.default.mkdirSync(tmpDirectory, { recursive: true });
136
- await (0, import_fs_extra.copy)(source, tmpDirectory, {
137
- overwrite: true,
138
- filter
139
- });
140
- await (0, import_fs_extra.copy)(tmpDirectory, target, {
141
- overwrite: true,
142
- filter
143
- });
144
- import_node_fs.default.rmSync(tmpDirectory, { recursive: true });
145
- };
146
-
147
- // src/internal/resources.ts
148
- var import_path = require("path");
149
- var import_gray_matter2 = __toESM(require("gray-matter"));
150
- var import_promises = __toESM(require("fs/promises"));
151
- var import_node_fs2 = __toESM(require("fs"));
152
- var import_semver2 = require("semver");
153
- var import_proper_lockfile = require("proper-lockfile");
154
- var import_node_path2 = require("path");
155
- var import_node_path3 = __toESM(require("path"));
156
- var versionResource = async (catalogDir, id) => {
157
- const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
158
- const matchedFiles = await searchFilesForId(files, id);
159
- if (matchedFiles.length === 0) {
160
- throw new Error(`No resource found with id: ${id}`);
161
- }
162
- const file = matchedFiles[0];
163
- const sourceDirectory = (0, import_path.dirname)(file).replace(/[/\\]versioned[/\\][^/\\]+[/\\]/, import_node_path3.default.sep);
164
- const { data: { version = "0.0.1" } = {} } = import_gray_matter2.default.read(file);
165
- const targetDirectory = getVersionedDirectory(sourceDirectory, version);
166
- import_node_fs2.default.mkdirSync(targetDirectory, { recursive: true });
167
- const ignoreListToCopy = ["events", "commands", "queries", "versioned"];
168
- await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
169
- const folderName = (0, import_node_path2.basename)(src);
170
- if (ignoreListToCopy.includes(folderName)) {
171
- return false;
172
- }
173
- return true;
174
- });
175
- await import_promises.default.readdir(sourceDirectory).then(async (resourceFiles) => {
176
- await Promise.all(
177
- resourceFiles.map(async (file2) => {
178
- if (ignoreListToCopy.includes(file2)) {
179
- return;
180
- }
181
- if (file2 !== "versioned") {
182
- import_node_fs2.default.rmSync((0, import_path.join)(sourceDirectory, file2), { recursive: true });
183
- }
184
- })
185
- );
186
- });
187
- };
188
- var writeResource = async (catalogDir, resource, options = {
189
- path: "",
190
- type: "",
191
- override: false,
192
- versionExistingContent: false,
193
- format: "mdx"
194
- }) => {
195
- const path2 = options.path || `/${resource.id}`;
196
- const fullPath = (0, import_path.join)(catalogDir, path2);
197
- const format = options.format || "mdx";
198
- import_node_fs2.default.mkdirSync(fullPath, { recursive: true });
199
- const lockPath = (0, import_path.join)(fullPath, `index.${format}`);
200
- if (!import_node_fs2.default.existsSync(lockPath)) {
201
- import_node_fs2.default.writeFileSync(lockPath, "");
202
- }
203
- try {
204
- await (0, import_proper_lockfile.lock)(lockPath, {
205
- retries: 5,
206
- stale: 1e4
207
- // 10 seconds
208
- });
209
- const exists = await versionExists(catalogDir, resource.id, resource.version);
210
- if (exists && !options.override) {
211
- throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
212
- }
213
- const { markdown, ...frontmatter } = resource;
214
- if (options.versionExistingContent && !exists) {
215
- const currentResource = await getResource(catalogDir, resource.id);
216
- if (currentResource) {
217
- if ((0, import_semver2.satisfies)(resource.version, `>${currentResource.version}`)) {
218
- await versionResource(catalogDir, resource.id);
219
- } else {
220
- throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
221
- }
222
- }
223
- }
224
- const document = import_gray_matter2.default.stringify(markdown.trim(), frontmatter);
225
- import_node_fs2.default.writeFileSync(lockPath, document);
226
- } finally {
227
- await (0, import_proper_lockfile.unlock)(lockPath).catch(() => {
228
- });
229
- }
230
- };
231
- var getResource = async (catalogDir, id, version, options, filePath) => {
232
- const attachSchema = options?.attachSchema || false;
233
- const file = filePath || (id ? await findFileById(catalogDir, id, version) : void 0);
234
- if (!file || !import_node_fs2.default.existsSync(file)) return;
235
- const { data, content } = import_gray_matter2.default.read(file);
236
- if (attachSchema && data?.schemaPath) {
237
- const resourceDirectory = (0, import_path.dirname)(file);
238
- const pathToSchema = (0, import_path.join)(resourceDirectory, data.schemaPath);
239
- if (import_node_fs2.default.existsSync(pathToSchema)) {
240
- const schema = import_node_fs2.default.readFileSync(pathToSchema, "utf8");
241
- try {
242
- data.schema = JSON.parse(schema);
243
- } catch (error) {
244
- data.schema = schema;
245
- }
246
- }
247
- }
248
- return {
249
- ...data,
250
- markdown: content.trim()
251
- };
252
- };
253
- var getResourcePath = async (catalogDir, id, version) => {
254
- const file = await findFileById(catalogDir, id, version);
255
- if (!file) return;
256
- return {
257
- fullPath: file,
258
- relativePath: file.replace(catalogDir, ""),
259
- directory: (0, import_path.dirname)(file.replace(catalogDir, ""))
260
- };
261
- };
262
- var getResources = async (catalogDir, {
263
- type,
264
- latestOnly = false,
265
- ignore = [],
266
- pattern = "",
267
- attachSchema = false
268
- }) => {
269
- const ignoreList = latestOnly ? `**/versioned/**` : "";
270
- const filePattern = pattern || `${catalogDir}/**/${type}/**/index.{md,mdx}`;
271
- const files = await getFiles(filePattern, [ignoreList, ...ignore]);
272
- if (files.length === 0) return;
273
- return files.map((file) => {
274
- const { data, content } = import_gray_matter2.default.read(file);
275
- if (attachSchema && data?.schemaPath) {
276
- const resourceDirectory = (0, import_path.dirname)(file);
277
- const pathToSchema = (0, import_path.join)(resourceDirectory, data.schemaPath);
278
- if (import_node_fs2.default.existsSync(pathToSchema)) {
279
- const schema = import_node_fs2.default.readFileSync(pathToSchema, "utf8");
280
- try {
281
- data.schema = JSON.parse(schema);
282
- } catch (error) {
283
- data.schema = schema;
284
- }
285
- }
286
- }
287
- return {
288
- ...data,
289
- markdown: content.trim()
290
- };
291
- });
292
- };
293
- var rmResourceById = async (catalogDir, id, version, options) => {
294
- const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
295
- const matchedFiles = await searchFilesForId(files, id, version);
296
- if (matchedFiles.length === 0) {
297
- throw new Error(`No ${options?.type || "resource"} found with id: ${id}`);
298
- }
299
- if (options?.persistFiles) {
300
- await Promise.all(
301
- matchedFiles.map(async (file) => {
302
- await import_promises.default.rm(file, { recursive: true });
303
- await waitForFileRemoval(file);
304
- })
305
- );
306
- } else {
307
- await Promise.all(
308
- matchedFiles.map(async (file) => {
309
- const directory = (0, import_path.dirname)(file);
310
- await import_promises.default.rm(directory, { recursive: true, force: true });
311
- await waitForFileRemoval(directory);
312
- })
313
- );
314
- }
315
- };
316
- var waitForFileRemoval = async (path2, maxRetries = 50, delay = 10) => {
317
- for (let i = 0; i < maxRetries; i++) {
318
- try {
319
- await import_promises.default.access(path2);
320
- await new Promise((resolve2) => setTimeout(resolve2, delay));
321
- } catch (error) {
322
- return;
323
- }
324
- }
325
- throw new Error(`File/directory ${path2} was not removed after ${maxRetries} attempts`);
326
- };
327
- var addFileToResource = async (catalogDir, id, file, version, options) => {
328
- let pathToResource;
329
- if (options?.path) {
330
- pathToResource = (0, import_path.join)(catalogDir, options.path, "index.mdx");
331
- } else {
332
- pathToResource = await findFileById(catalogDir, id, version);
333
- }
334
- if (!pathToResource) throw new Error("Cannot find directory to write file to");
335
- import_node_fs2.default.mkdirSync(import_node_path3.default.dirname(pathToResource), { recursive: true });
336
- let fileContent = file.content.trim();
337
- try {
338
- const json = JSON.parse(fileContent);
339
- fileContent = JSON.stringify(json, null, 2);
340
- } catch (error) {
341
- }
342
- import_node_fs2.default.writeFileSync((0, import_path.join)((0, import_path.dirname)(pathToResource), file.fileName), fileContent);
343
- };
344
- var getVersionedDirectory = (sourceDirectory, version) => {
345
- return (0, import_path.join)(sourceDirectory, "versioned", version);
346
- };
347
-
348
- // src/queries.ts
349
- var getQuery = (directory) => async (id, version, options) => getResource(directory, id, version, { type: "query", ...options });
350
- var writeQuery = (directory) => async (query, options = {
351
- path: "",
352
- override: false,
353
- versionExistingContent: false,
354
- format: "mdx"
355
- }) => writeResource(directory, { ...query }, { ...options, type: "query" });
356
- var getQueries = (directory) => async (options) => getResources(directory, { type: "queries", ...options });
357
- var writeQueryToService = (directory) => async (query, service, options = { path: "", format: "mdx", override: false }) => {
358
- const resourcePath = await getResourcePath(directory, service.id, service.version);
359
- if (!resourcePath) {
360
- throw new Error("Service not found");
361
- }
362
- let pathForQuery = service.version && service.version !== "latest" ? `${resourcePath.directory}/versioned/${service.version}/queries` : `${resourcePath.directory}/queries`;
363
- pathForQuery = (0, import_node_path4.join)(pathForQuery, query.id);
364
- await writeResource(directory, { ...query }, { ...options, path: pathForQuery, type: "query" });
365
- };
366
- var rmQuery = (directory) => async (path2) => {
367
- await import_promises2.default.rm((0, import_node_path4.join)(directory, path2), { recursive: true });
368
- };
369
- var rmQueryById = (directory) => async (id, version, persistFiles) => {
370
- await rmResourceById(directory, id, version, { type: "query", persistFiles });
371
- };
372
- var versionQuery = (directory) => async (id) => versionResource(directory, id);
373
- var addFileToQuery = (directory) => async (id, file, version, options) => addFileToResource(directory, id, file, version, options);
374
- var addSchemaToQuery = (directory) => async (id, schema, version, options) => {
375
- await addFileToQuery(directory)(id, { content: schema.schema, fileName: schema.fileName }, version, options);
376
- };
377
- var queryHasVersion = (directory) => async (id, version) => {
378
- const file = await findFileById(directory, id, version);
379
- return !!file;
380
- };
381
- // Annotate the CommonJS export names for ESM import in node:
382
- 0 && (module.exports = {
383
- addFileToQuery,
384
- addSchemaToQuery,
385
- getQueries,
386
- getQuery,
387
- queryHasVersion,
388
- rmQuery,
389
- rmQueryById,
390
- versionQuery,
391
- writeQuery,
392
- writeQueryToService
393
- });
394
- //# sourceMappingURL=queries.js.map