@eventcatalog/sdk 2.6.9 → 2.7.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.
- package/README.md +0 -32
- package/dist/domains.d.mts +22 -1
- package/dist/domains.d.ts +22 -1
- package/dist/domains.js +17 -0
- package/dist/domains.js.map +1 -1
- package/dist/domains.mjs +16 -0
- package/dist/domains.mjs.map +1 -1
- package/dist/entities.d.mts +174 -0
- package/dist/entities.d.ts +174 -0
- package/dist/entities.js +318 -0
- package/dist/entities.js.map +1 -0
- package/dist/entities.mjs +277 -0
- package/dist/entities.mjs.map +1 -0
- package/dist/eventcatalog.js +230 -93
- package/dist/eventcatalog.js.map +1 -1
- package/dist/eventcatalog.mjs +232 -95
- package/dist/eventcatalog.mjs.map +1 -1
- package/dist/index.d.mts +120 -5
- package/dist/index.d.ts +120 -5
- package/dist/index.js +226 -89
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +226 -89
- package/dist/index.mjs.map +1 -1
- package/dist/messages.js.map +1 -1
- package/dist/messages.mjs.map +1 -1
- package/dist/services.d.mts +22 -1
- package/dist/services.d.ts +22 -1
- package/dist/services.js +24 -0
- package/dist/services.js.map +1 -1
- package/dist/services.mjs +23 -0
- package/dist/services.mjs.map +1 -1
- package/dist/types.d.d.mts +29 -1
- package/dist/types.d.d.ts +29 -1
- package/dist/types.d.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,174 @@
|
|
|
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 };
|
|
@@ -0,0 +1,174 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
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_path2 = 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((path) => !path.includes("versioned"));
|
|
61
|
+
if (!version) {
|
|
62
|
+
return latestVersion;
|
|
63
|
+
}
|
|
64
|
+
const parsedFiles = matchedFiles.map((path) => {
|
|
65
|
+
const { data } = import_gray_matter.default.read(path);
|
|
66
|
+
return { ...data, path };
|
|
67
|
+
});
|
|
68
|
+
const semverRange = (0, import_semver.validRange)(version);
|
|
69
|
+
if (semverRange && (0, import_semver.valid)(version)) {
|
|
70
|
+
const match2 = parsedFiles.filter((c) => (0, import_semver.satisfies)(c.version, semverRange));
|
|
71
|
+
return match2.length > 0 ? match2[0].path : void 0;
|
|
72
|
+
}
|
|
73
|
+
const sorted = parsedFiles.sort((a, b) => {
|
|
74
|
+
return a.version.localeCompare(b.version);
|
|
75
|
+
});
|
|
76
|
+
const match = sorted.length > 0 ? [sorted[sorted.length - 1]] : [];
|
|
77
|
+
if (match.length > 0) {
|
|
78
|
+
return match[0].path;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var getFiles = async (pattern, ignore = "") => {
|
|
82
|
+
try {
|
|
83
|
+
const normalizedInputPattern = (0, import_node_path.normalize)(pattern);
|
|
84
|
+
const absoluteBaseDir = (0, import_node_path.resolve)(
|
|
85
|
+
normalizedInputPattern.includes("**") ? normalizedInputPattern.split("**")[0] : (0, import_node_path.dirname)(normalizedInputPattern)
|
|
86
|
+
);
|
|
87
|
+
let relativePattern = (0, import_node_path.relative)(absoluteBaseDir, normalizedInputPattern);
|
|
88
|
+
relativePattern = relativePattern.replace(/\\/g, "/");
|
|
89
|
+
const ignoreList = Array.isArray(ignore) ? ignore : [ignore];
|
|
90
|
+
const files = (0, import_glob.globSync)(relativePattern, {
|
|
91
|
+
cwd: absoluteBaseDir,
|
|
92
|
+
ignore: ["node_modules/**", ...ignoreList],
|
|
93
|
+
absolute: true,
|
|
94
|
+
nodir: true
|
|
95
|
+
});
|
|
96
|
+
return files.map(import_node_path.normalize);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
const absoluteBaseDirForError = (0, import_node_path.resolve)(
|
|
99
|
+
(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))
|
|
100
|
+
);
|
|
101
|
+
const relativePatternForError = (0, import_node_path.relative)(absoluteBaseDirForError, (0, import_node_path.normalize)(pattern)).replace(/\\/g, "/");
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Error finding files for pattern "${pattern}" (using cwd: "${absoluteBaseDirForError}", globPattern: "${relativePatternForError}"): ${error.message}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var searchFilesForId = async (files, id, version) => {
|
|
108
|
+
const escapedId = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
109
|
+
const idRegex = new RegExp(`^id:\\s*(['"]|>-)?\\s*${escapedId}['"]?\\s*$`, "m");
|
|
110
|
+
const versionRegex = new RegExp(`^version:\\s*['"]?${version}['"]?\\s*$`, "m");
|
|
111
|
+
const matches = files.map((file) => {
|
|
112
|
+
const content = import_node_fs.default.readFileSync(file, "utf-8");
|
|
113
|
+
const hasIdMatch = content.match(idRegex);
|
|
114
|
+
if (version && !content.match(versionRegex)) {
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
if (hasIdMatch) {
|
|
118
|
+
return file;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return matches.filter(Boolean).filter((file) => file !== void 0);
|
|
122
|
+
};
|
|
123
|
+
var copyDir = async (catalogDir, source, target, filter) => {
|
|
124
|
+
const tmpDirectory = (0, import_node_path.join)(catalogDir, "tmp");
|
|
125
|
+
import_node_fs.default.mkdirSync(tmpDirectory, { recursive: true });
|
|
126
|
+
await (0, import_fs_extra.copy)(source, tmpDirectory, {
|
|
127
|
+
overwrite: true,
|
|
128
|
+
filter
|
|
129
|
+
});
|
|
130
|
+
await (0, import_fs_extra.copy)(tmpDirectory, target, {
|
|
131
|
+
overwrite: true,
|
|
132
|
+
filter
|
|
133
|
+
});
|
|
134
|
+
import_node_fs.default.rmSync(tmpDirectory, { recursive: true });
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/internal/resources.ts
|
|
138
|
+
var import_path = require("path");
|
|
139
|
+
var import_gray_matter2 = __toESM(require("gray-matter"));
|
|
140
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
141
|
+
var import_node_fs2 = __toESM(require("fs"));
|
|
142
|
+
var import_semver2 = require("semver");
|
|
143
|
+
var import_proper_lockfile = require("proper-lockfile");
|
|
144
|
+
var versionResource = async (catalogDir, id) => {
|
|
145
|
+
const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
|
|
146
|
+
const matchedFiles = await searchFilesForId(files, id);
|
|
147
|
+
if (matchedFiles.length === 0) {
|
|
148
|
+
throw new Error(`No resource found with id: ${id}`);
|
|
149
|
+
}
|
|
150
|
+
const file = matchedFiles[0];
|
|
151
|
+
const sourceDirectory = (0, import_path.dirname)(file);
|
|
152
|
+
const { data: { version = "0.0.1" } = {} } = import_gray_matter2.default.read(file);
|
|
153
|
+
const targetDirectory = getVersionedDirectory(sourceDirectory, version);
|
|
154
|
+
import_node_fs2.default.mkdirSync(targetDirectory, { recursive: true });
|
|
155
|
+
await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
|
|
156
|
+
return !src.includes("versioned");
|
|
157
|
+
});
|
|
158
|
+
await import_promises.default.readdir(sourceDirectory).then(async (resourceFiles) => {
|
|
159
|
+
await Promise.all(
|
|
160
|
+
resourceFiles.map(async (file2) => {
|
|
161
|
+
if (file2 !== "versioned") {
|
|
162
|
+
import_node_fs2.default.rmSync((0, import_path.join)(sourceDirectory, file2), { recursive: true });
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
var writeResource = async (catalogDir, resource, options = {
|
|
169
|
+
path: "",
|
|
170
|
+
type: "",
|
|
171
|
+
override: false,
|
|
172
|
+
versionExistingContent: false,
|
|
173
|
+
format: "mdx"
|
|
174
|
+
}) => {
|
|
175
|
+
const path = options.path || `/${resource.id}`;
|
|
176
|
+
const fullPath = (0, import_path.join)(catalogDir, path);
|
|
177
|
+
const format = options.format || "mdx";
|
|
178
|
+
import_node_fs2.default.mkdirSync(fullPath, { recursive: true });
|
|
179
|
+
const lockPath = (0, import_path.join)(fullPath, `index.${format}`);
|
|
180
|
+
if (!import_node_fs2.default.existsSync(lockPath)) {
|
|
181
|
+
import_node_fs2.default.writeFileSync(lockPath, "");
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
await (0, import_proper_lockfile.lock)(lockPath, {
|
|
185
|
+
retries: 5,
|
|
186
|
+
stale: 1e4
|
|
187
|
+
// 10 seconds
|
|
188
|
+
});
|
|
189
|
+
const exists = await versionExists(catalogDir, resource.id, resource.version);
|
|
190
|
+
if (exists && !options.override) {
|
|
191
|
+
throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
|
|
192
|
+
}
|
|
193
|
+
const { markdown, ...frontmatter } = resource;
|
|
194
|
+
if (options.versionExistingContent && !exists) {
|
|
195
|
+
const currentResource = await getResource(catalogDir, resource.id);
|
|
196
|
+
if (currentResource) {
|
|
197
|
+
if ((0, import_semver2.satisfies)(resource.version, `>${currentResource.version}`)) {
|
|
198
|
+
await versionResource(catalogDir, resource.id);
|
|
199
|
+
} else {
|
|
200
|
+
throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const document = import_gray_matter2.default.stringify(markdown.trim(), frontmatter);
|
|
205
|
+
import_node_fs2.default.writeFileSync(lockPath, document);
|
|
206
|
+
} finally {
|
|
207
|
+
await (0, import_proper_lockfile.unlock)(lockPath).catch(() => {
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var getResource = async (catalogDir, id, version, options, filePath) => {
|
|
212
|
+
const attachSchema = options?.attachSchema || false;
|
|
213
|
+
const file = filePath || (id ? await findFileById(catalogDir, id, version) : void 0);
|
|
214
|
+
if (!file || !import_node_fs2.default.existsSync(file)) return;
|
|
215
|
+
const { data, content } = import_gray_matter2.default.read(file);
|
|
216
|
+
if (attachSchema && data?.schemaPath) {
|
|
217
|
+
const resourceDirectory = (0, import_path.dirname)(file);
|
|
218
|
+
const pathToSchema = (0, import_path.join)(resourceDirectory, data.schemaPath);
|
|
219
|
+
if (import_node_fs2.default.existsSync(pathToSchema)) {
|
|
220
|
+
const schema = import_node_fs2.default.readFileSync(pathToSchema, "utf8");
|
|
221
|
+
try {
|
|
222
|
+
data.schema = JSON.parse(schema);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
data.schema = schema;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
...data,
|
|
230
|
+
markdown: content.trim()
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
var getResources = async (catalogDir, {
|
|
234
|
+
type,
|
|
235
|
+
latestOnly = false,
|
|
236
|
+
ignore = [],
|
|
237
|
+
pattern = "",
|
|
238
|
+
attachSchema = false
|
|
239
|
+
}) => {
|
|
240
|
+
const ignoreList = latestOnly ? `**/versioned/**` : "";
|
|
241
|
+
const filePattern = pattern || `${catalogDir}/**/${type}/**/index.{md,mdx}`;
|
|
242
|
+
const files = await getFiles(filePattern, [ignoreList, ...ignore]);
|
|
243
|
+
if (files.length === 0) return;
|
|
244
|
+
return files.map((file) => {
|
|
245
|
+
const { data, content } = import_gray_matter2.default.read(file);
|
|
246
|
+
if (attachSchema && data?.schemaPath) {
|
|
247
|
+
const resourceDirectory = (0, import_path.dirname)(file);
|
|
248
|
+
const pathToSchema = (0, import_path.join)(resourceDirectory, data.schemaPath);
|
|
249
|
+
if (import_node_fs2.default.existsSync(pathToSchema)) {
|
|
250
|
+
const schema = import_node_fs2.default.readFileSync(pathToSchema, "utf8");
|
|
251
|
+
try {
|
|
252
|
+
data.schema = JSON.parse(schema);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
data.schema = schema;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
...data,
|
|
260
|
+
markdown: content.trim()
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
var rmResourceById = async (catalogDir, id, version, options) => {
|
|
265
|
+
const files = await getFiles(`${catalogDir}/**/index.{md,mdx}`);
|
|
266
|
+
const matchedFiles = await searchFilesForId(files, id, version);
|
|
267
|
+
if (matchedFiles.length === 0) {
|
|
268
|
+
throw new Error(`No ${options?.type || "resource"} found with id: ${id}`);
|
|
269
|
+
}
|
|
270
|
+
if (options?.persistFiles) {
|
|
271
|
+
await Promise.all(
|
|
272
|
+
matchedFiles.map(async (file) => {
|
|
273
|
+
await import_promises.default.rm(file, { recursive: true });
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
} else {
|
|
277
|
+
await Promise.all(
|
|
278
|
+
matchedFiles.map(async (file) => {
|
|
279
|
+
const directory = (0, import_path.dirname)(file);
|
|
280
|
+
await import_promises.default.rm(directory, { recursive: true, force: true });
|
|
281
|
+
})
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
var getVersionedDirectory = (sourceDirectory, version) => {
|
|
286
|
+
return (0, import_path.join)(sourceDirectory, "versioned", version);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/entities.ts
|
|
290
|
+
var getEntity = (directory) => async (id, version) => getResource(directory, id, version, { type: "entity" });
|
|
291
|
+
var getEntities = (directory) => async (options) => getResources(directory, { type: "entities", latestOnly: options?.latestOnly });
|
|
292
|
+
var writeEntity = (directory) => async (entity, options = {
|
|
293
|
+
path: "",
|
|
294
|
+
override: false,
|
|
295
|
+
format: "mdx"
|
|
296
|
+
}) => writeResource(directory, { ...entity }, { ...options, type: "entity" });
|
|
297
|
+
var rmEntity = (directory) => async (path) => {
|
|
298
|
+
await import_promises2.default.rm((0, import_node_path2.join)(directory, path), { recursive: true });
|
|
299
|
+
};
|
|
300
|
+
var rmEntityById = (directory) => async (id, version, persistFiles) => {
|
|
301
|
+
await rmResourceById(directory, id, version, { type: "entity", persistFiles });
|
|
302
|
+
};
|
|
303
|
+
var versionEntity = (directory) => async (id) => versionResource(directory, id);
|
|
304
|
+
var entityHasVersion = (directory) => async (id, version) => {
|
|
305
|
+
const file = await findFileById(directory, id, version);
|
|
306
|
+
return !!file;
|
|
307
|
+
};
|
|
308
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
309
|
+
0 && (module.exports = {
|
|
310
|
+
entityHasVersion,
|
|
311
|
+
getEntities,
|
|
312
|
+
getEntity,
|
|
313
|
+
rmEntity,
|
|
314
|
+
rmEntityById,
|
|
315
|
+
versionEntity,
|
|
316
|
+
writeEntity
|
|
317
|
+
});
|
|
318
|
+
//# sourceMappingURL=entities.js.map
|