@eventcatalog/sdk 2.10.0 → 2.12.0

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