@eventcatalog/sdk 0.1.4 → 1.1.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.
- package/README.md +1 -1
- package/dist/commands.d.mts +27 -1
- package/dist/commands.d.ts +27 -1
- package/dist/commands.js +9 -2
- package/dist/commands.js.map +1 -1
- package/dist/commands.mjs +7 -1
- package/dist/commands.mjs.map +1 -1
- package/dist/domains.js.map +1 -1
- package/dist/domains.mjs.map +1 -1
- package/dist/events.d.mts +29 -1
- package/dist/events.d.ts +29 -1
- package/dist/events.js +12 -3
- package/dist/events.js.map +1 -1
- package/dist/events.mjs +10 -2
- package/dist/events.mjs.map +1 -1
- package/dist/index.d.mts +128 -1
- package/dist/index.d.ts +128 -1
- package/dist/index.js +183 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +184 -43
- package/dist/index.mjs.map +1 -1
- package/dist/queries.d.mts +217 -0
- package/dist/queries.d.ts +217 -0
- package/dist/queries.js +219 -0
- package/dist/queries.js.map +1 -0
- package/dist/queries.mjs +176 -0
- package/dist/queries.mjs.map +1 -0
- package/dist/services.d.mts +28 -1
- package/dist/services.d.ts +28 -1
- package/dist/services.js +9 -2
- package/dist/services.js.map +1 -1
- package/dist/services.mjs +7 -1
- package/dist/services.mjs.map +1 -1
- package/dist/types.d.d.mts +2 -1
- package/dist/types.d.d.ts +2 -1
- package/dist/types.d.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
*/
|
|
21
|
+
declare const getQuery: (directory: string) => (id: string, version?: string) => Promise<Query>;
|
|
22
|
+
/**
|
|
23
|
+
* Write a query to EventCatalog.
|
|
24
|
+
*
|
|
25
|
+
* You can optionally override the path of the query.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import utils from '@eventcatalog/utils';
|
|
30
|
+
*
|
|
31
|
+
* const { writeQuery } = utils('/path/to/eventcatalog');
|
|
32
|
+
*
|
|
33
|
+
* // Write an event to the catalog
|
|
34
|
+
* // Event would be written to queries/GetOrder
|
|
35
|
+
* await writeQuery({
|
|
36
|
+
* id: 'GetOrder',
|
|
37
|
+
* name: 'Get Order',
|
|
38
|
+
* version: '0.0.1',
|
|
39
|
+
* summary: 'This is a summary',
|
|
40
|
+
* markdown: '# Hello world',
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Write an event to the catalog but override the path
|
|
44
|
+
* // Event would be written to queries/Inventory/GetOrder
|
|
45
|
+
* await writeQuery({
|
|
46
|
+
* id: 'GetOrder',
|
|
47
|
+
* name: 'Get Order',
|
|
48
|
+
* version: '0.0.1',
|
|
49
|
+
* summary: 'This is a summary',
|
|
50
|
+
* markdown: '# Hello world',
|
|
51
|
+
* }, { path: "/Orders/GetOrder"});
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare const writeQuery: (directory: string) => (query: Query, options?: {
|
|
55
|
+
path: string;
|
|
56
|
+
}) => Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Write a query to a service in EventCatalog.
|
|
59
|
+
*
|
|
60
|
+
* You can optionally override the path of the event.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import utils from '@eventcatalog/utils';
|
|
65
|
+
*
|
|
66
|
+
* const { writeQueryToService } = utils('/path/to/eventcatalog');
|
|
67
|
+
*
|
|
68
|
+
* // Write an event to a given service in the catalog
|
|
69
|
+
* // Event would be written to services/Orders/queries/GetOrder
|
|
70
|
+
* await writeQueryToService({
|
|
71
|
+
* id: 'GetOrder',
|
|
72
|
+
* name: 'Get Order',
|
|
73
|
+
* version: '0.0.1',
|
|
74
|
+
* summary: 'This is a summary',
|
|
75
|
+
* markdown: '# Hello world',
|
|
76
|
+
* }, { id: 'Orders' });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare const writeQueryToService: (directory: string) => (query: Query, service: {
|
|
80
|
+
id: string;
|
|
81
|
+
version?: string;
|
|
82
|
+
}, options?: {
|
|
83
|
+
path: string;
|
|
84
|
+
}) => Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Delete a query at it's given path.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* import utils from '@eventcatalog/utils';
|
|
91
|
+
*
|
|
92
|
+
* const { rmQuery } = utils('/path/to/eventcatalog');
|
|
93
|
+
*
|
|
94
|
+
* // removes an query at the given path (queries dir is appended to the given path)
|
|
95
|
+
* // Removes the query at queries/GetOrders
|
|
96
|
+
* await rmQuery('/GetOrders');
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare const rmQuery: (directory: string) => (path: string) => Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Delete a query by it's id.
|
|
102
|
+
*
|
|
103
|
+
* Optionally specify a version to delete a specific version of the query.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* import utils from '@eventcatalog/utils';
|
|
108
|
+
*
|
|
109
|
+
* const { rmQueryById } = utils('/path/to/eventcatalog');
|
|
110
|
+
*
|
|
111
|
+
* // deletes the latest InventoryAdjusted query
|
|
112
|
+
* await rmQueryById('GetOrder');
|
|
113
|
+
*
|
|
114
|
+
* // deletes a specific version of the GetOrder query
|
|
115
|
+
* await rmQueryById('GetOrder', '0.0.1');
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare const rmQueryById: (directory: string) => (id: string, version?: string) => Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Version a query by it's id.
|
|
121
|
+
*
|
|
122
|
+
* Takes the latest query and moves it to a versioned directory.
|
|
123
|
+
* All files with this query are also versioned (e.g /queries/GetOrder/schema.json)
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* import utils from '@eventcatalog/utils';
|
|
128
|
+
*
|
|
129
|
+
* const { versionQuery } = utils('/path/to/eventcatalog');
|
|
130
|
+
*
|
|
131
|
+
* // moves the latest GetOrder query to a versioned directory
|
|
132
|
+
* // the version within that query is used as the version number.
|
|
133
|
+
* await versionQuery('GetOrder');
|
|
134
|
+
*
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
declare const versionQuery: (directory: string) => (id: string) => Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Add a file to a query by it's id.
|
|
140
|
+
*
|
|
141
|
+
* Optionally specify a version to add a file to a specific version of the query.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* import utils from '@eventcatalog/utils';
|
|
146
|
+
*
|
|
147
|
+
* const { addFileToQuery } = utils('/path/to/eventcatalog');
|
|
148
|
+
*
|
|
149
|
+
* // adds a file to the latest GetOrder query
|
|
150
|
+
* await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' });
|
|
151
|
+
*
|
|
152
|
+
* // adds a file to a specific version of the GetOrder query
|
|
153
|
+
* await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');
|
|
154
|
+
*
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
declare const addFileToQuery: (directory: string) => (id: string, file: {
|
|
158
|
+
content: string;
|
|
159
|
+
fileName: string;
|
|
160
|
+
}, version?: string) => Promise<void>;
|
|
161
|
+
/**
|
|
162
|
+
* Add a schema to a query by it's id.
|
|
163
|
+
*
|
|
164
|
+
* Optionally specify a version to add a schema to a specific version of the query.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import utils from '@eventcatalog/utils';
|
|
169
|
+
*
|
|
170
|
+
* const { addSchemaToQuery } = utils('/path/to/eventcatalog');
|
|
171
|
+
*
|
|
172
|
+
* // JSON schema example
|
|
173
|
+
* const schema = {
|
|
174
|
+
* "$schema": "http://json-schema.org/draft-07/schema#",
|
|
175
|
+
* "type": "object",
|
|
176
|
+
* "properties": {
|
|
177
|
+
* "name": {
|
|
178
|
+
* "type": "string"
|
|
179
|
+
* },
|
|
180
|
+
* "age": {
|
|
181
|
+
* "type": "number"
|
|
182
|
+
* }
|
|
183
|
+
* },
|
|
184
|
+
* "required": ["name", "age"]
|
|
185
|
+
* };
|
|
186
|
+
*
|
|
187
|
+
* // adds a schema to the latest GetOrder query
|
|
188
|
+
* await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' });
|
|
189
|
+
*
|
|
190
|
+
* // adds a file to a specific version of the GetOrder query
|
|
191
|
+
* await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' }, '0.0.1');
|
|
192
|
+
*
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare const addSchemaToQuery: (directory: string) => (id: string, schema: {
|
|
196
|
+
schema: string;
|
|
197
|
+
fileName: string;
|
|
198
|
+
}, version?: string) => Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Check to see if the catalog has a version for the given query.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* import utils from '@eventcatalog/utils';
|
|
205
|
+
*
|
|
206
|
+
* const { queryHasVersion } = utils('/path/to/eventcatalog');
|
|
207
|
+
*
|
|
208
|
+
* // returns true if version is found for the given event and version (supports semver)
|
|
209
|
+
* await queryHasVersion('GetOrder', '0.0.1');
|
|
210
|
+
* await queryHasVersion('GetOrder', 'latest');
|
|
211
|
+
* await queryHasVersion('GetOrder', '0.0.x');*
|
|
212
|
+
*
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
declare const queryHasVersion: (directory: string) => (id: string, version: string) => Promise<boolean>;
|
|
216
|
+
|
|
217
|
+
export { addFileToQuery, addSchemaToQuery, getQuery, queryHasVersion, rmQuery, rmQueryById, versionQuery, writeQuery, writeQueryToService };
|
package/dist/queries.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
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
|
+
getQuery: () => getQuery,
|
|
36
|
+
queryHasVersion: () => queryHasVersion,
|
|
37
|
+
rmQuery: () => rmQuery,
|
|
38
|
+
rmQueryById: () => rmQueryById,
|
|
39
|
+
versionQuery: () => versionQuery,
|
|
40
|
+
writeQuery: () => writeQuery,
|
|
41
|
+
writeQueryToService: () => writeQueryToService
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(queries_exports);
|
|
44
|
+
var import_promises3 = __toESM(require("fs/promises"));
|
|
45
|
+
var import_node_path2 = require("path");
|
|
46
|
+
|
|
47
|
+
// src/internal/utils.ts
|
|
48
|
+
var import_glob = require("glob");
|
|
49
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
50
|
+
var import_fs_extra = require("fs-extra");
|
|
51
|
+
var import_node_path = require("path");
|
|
52
|
+
var import_gray_matter = __toESM(require("gray-matter"));
|
|
53
|
+
var import_semver = require("semver");
|
|
54
|
+
var versionExists = async (catalogDir, id, version) => {
|
|
55
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
56
|
+
const matchedFiles = await searchFilesForId(files, id, version) || [];
|
|
57
|
+
return matchedFiles.length > 0;
|
|
58
|
+
};
|
|
59
|
+
var findFileById = async (catalogDir, id, version) => {
|
|
60
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
61
|
+
const matchedFiles = await searchFilesForId(files, id) || [];
|
|
62
|
+
const latestVersion = matchedFiles.find((path) => !path.includes("versioned"));
|
|
63
|
+
if (!version) {
|
|
64
|
+
return latestVersion;
|
|
65
|
+
}
|
|
66
|
+
const parsedFiles = matchedFiles.map((path) => {
|
|
67
|
+
const { data } = import_gray_matter.default.read(path);
|
|
68
|
+
return { ...data, path };
|
|
69
|
+
});
|
|
70
|
+
const semverRange = (0, import_semver.validRange)(version);
|
|
71
|
+
if (semverRange && (0, import_semver.valid)(version)) {
|
|
72
|
+
const match2 = parsedFiles.filter((c) => (0, import_semver.satisfies)(c.version, semverRange));
|
|
73
|
+
return match2.length > 0 ? match2[0].path : void 0;
|
|
74
|
+
}
|
|
75
|
+
const sorted = parsedFiles.sort((a, b) => {
|
|
76
|
+
return a.version.localeCompare(b.version);
|
|
77
|
+
});
|
|
78
|
+
const match = sorted.length > 0 ? [sorted[sorted.length - 1]] : [];
|
|
79
|
+
if (match.length > 0) {
|
|
80
|
+
return match[0].path;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var getFiles = async (pattern) => {
|
|
84
|
+
try {
|
|
85
|
+
const files = await (0, import_glob.glob)(pattern, { ignore: "node_modules/**" });
|
|
86
|
+
return files;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new Error(`Error finding files: ${error}`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var searchFilesForId = async (files, id, version) => {
|
|
92
|
+
const idRegex = new RegExp(`^id:\\s*(['"]|>-)?\\s*${id}['"]?\\s*$`, "m");
|
|
93
|
+
const versionRegex = new RegExp(`^version:\\s*['"]?${version}['"]?\\s*$`, "m");
|
|
94
|
+
const matches = await Promise.all(
|
|
95
|
+
files.map(async (file) => {
|
|
96
|
+
const content = await import_promises.default.readFile(file, "utf-8");
|
|
97
|
+
const hasIdMatch = content.match(idRegex);
|
|
98
|
+
if (version && !content.match(versionRegex)) {
|
|
99
|
+
return void 0;
|
|
100
|
+
}
|
|
101
|
+
if (hasIdMatch) {
|
|
102
|
+
return file;
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
return matches.filter(Boolean).filter((file) => file !== void 0);
|
|
107
|
+
};
|
|
108
|
+
var copyDir = async (catalogDir, source, target, filter) => {
|
|
109
|
+
const tmpDirectory = (0, import_node_path.join)(catalogDir, "tmp");
|
|
110
|
+
await import_promises.default.mkdir(tmpDirectory, { recursive: true });
|
|
111
|
+
await (0, import_fs_extra.copy)(source, tmpDirectory, {
|
|
112
|
+
overwrite: true,
|
|
113
|
+
filter
|
|
114
|
+
});
|
|
115
|
+
await (0, import_fs_extra.copy)(tmpDirectory, target, {
|
|
116
|
+
overwrite: true,
|
|
117
|
+
filter
|
|
118
|
+
});
|
|
119
|
+
await import_promises.default.rm(tmpDirectory, { recursive: true });
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/internal/resources.ts
|
|
123
|
+
var import_path = require("path");
|
|
124
|
+
var import_gray_matter2 = __toESM(require("gray-matter"));
|
|
125
|
+
var import_promises2 = __toESM(require("fs/promises"));
|
|
126
|
+
var versionResource = async (catalogDir, id) => {
|
|
127
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
128
|
+
const matchedFiles = await searchFilesForId(files, id);
|
|
129
|
+
if (matchedFiles.length === 0) {
|
|
130
|
+
throw new Error(`No event found with id: ${id}`);
|
|
131
|
+
}
|
|
132
|
+
const file = matchedFiles[0];
|
|
133
|
+
const sourceDirectory = (0, import_path.dirname)(file);
|
|
134
|
+
const { data: { version = "0.0.1" } = {} } = import_gray_matter2.default.read(file);
|
|
135
|
+
const targetDirectory = (0, import_path.join)(sourceDirectory, "versioned", version);
|
|
136
|
+
await import_promises2.default.mkdir(targetDirectory, { recursive: true });
|
|
137
|
+
await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
|
|
138
|
+
return !src.includes("versioned");
|
|
139
|
+
});
|
|
140
|
+
await import_promises2.default.readdir(sourceDirectory).then(async (resourceFiles) => {
|
|
141
|
+
await Promise.all(
|
|
142
|
+
resourceFiles.map(async (file2) => {
|
|
143
|
+
if (file2 !== "versioned") {
|
|
144
|
+
await import_promises2.default.rm((0, import_path.join)(sourceDirectory, file2), { recursive: true });
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
var writeResource = async (catalogDir, resource, options = { path: "", type: "" }) => {
|
|
151
|
+
const path = options.path || `/${resource.id}`;
|
|
152
|
+
const exists = await versionExists(catalogDir, resource.id, resource.version);
|
|
153
|
+
if (exists) {
|
|
154
|
+
throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);
|
|
155
|
+
}
|
|
156
|
+
const { markdown, ...frontmatter } = resource;
|
|
157
|
+
const document = import_gray_matter2.default.stringify(markdown.trim(), frontmatter);
|
|
158
|
+
await import_promises2.default.mkdir((0, import_path.join)(catalogDir, path), { recursive: true });
|
|
159
|
+
await import_promises2.default.writeFile((0, import_path.join)(catalogDir, path, "index.md"), document);
|
|
160
|
+
};
|
|
161
|
+
var getResource = async (catalogDir, id, version, options) => {
|
|
162
|
+
const file = await findFileById(catalogDir, id, version);
|
|
163
|
+
if (!file) return;
|
|
164
|
+
const { data, content } = import_gray_matter2.default.read(file);
|
|
165
|
+
return {
|
|
166
|
+
...data,
|
|
167
|
+
markdown: content.trim()
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
var rmResourceById = async (catalogDir, id, version, options) => {
|
|
171
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
172
|
+
const matchedFiles = await searchFilesForId(files, id, version);
|
|
173
|
+
if (matchedFiles.length === 0) {
|
|
174
|
+
throw new Error(`No ${options?.type || "resource"} found with id: ${id}`);
|
|
175
|
+
}
|
|
176
|
+
await Promise.all(matchedFiles.map((file) => import_promises2.default.rm(file)));
|
|
177
|
+
};
|
|
178
|
+
var addFileToResource = async (catalogDir, id, file, version) => {
|
|
179
|
+
const pathToResource = await findFileById(catalogDir, id, version);
|
|
180
|
+
if (!pathToResource) throw new Error("Cannot find directory to write file to");
|
|
181
|
+
await import_promises2.default.writeFile((0, import_path.join)((0, import_path.dirname)(pathToResource), file.fileName), file.content);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// src/queries.ts
|
|
185
|
+
var getQuery = (directory) => async (id, version) => getResource(directory, id, version, { type: "query" });
|
|
186
|
+
var writeQuery = (directory) => async (query, options = { path: "" }) => writeResource(directory, { ...query }, { ...options, type: "query" });
|
|
187
|
+
var writeQueryToService = (directory) => async (query, service, options = { path: "" }) => {
|
|
188
|
+
let pathForQuery = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/queries` : `/${service.id}/queries`;
|
|
189
|
+
pathForQuery = (0, import_node_path2.join)(pathForQuery, query.id);
|
|
190
|
+
await writeResource(directory, { ...query }, { ...options, path: pathForQuery, type: "query" });
|
|
191
|
+
};
|
|
192
|
+
var rmQuery = (directory) => async (path) => {
|
|
193
|
+
await import_promises3.default.rm((0, import_node_path2.join)(directory, path), { recursive: true });
|
|
194
|
+
};
|
|
195
|
+
var rmQueryById = (directory) => async (id, version) => {
|
|
196
|
+
await rmResourceById(directory, id, version, { type: "query" });
|
|
197
|
+
};
|
|
198
|
+
var versionQuery = (directory) => async (id) => versionResource(directory, id);
|
|
199
|
+
var addFileToQuery = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
|
|
200
|
+
var addSchemaToQuery = (directory) => async (id, schema, version) => {
|
|
201
|
+
await addFileToQuery(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
|
|
202
|
+
};
|
|
203
|
+
var queryHasVersion = (directory) => async (id, version) => {
|
|
204
|
+
const file = await findFileById(directory, id, version);
|
|
205
|
+
return !!file;
|
|
206
|
+
};
|
|
207
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
208
|
+
0 && (module.exports = {
|
|
209
|
+
addFileToQuery,
|
|
210
|
+
addSchemaToQuery,
|
|
211
|
+
getQuery,
|
|
212
|
+
queryHasVersion,
|
|
213
|
+
rmQuery,
|
|
214
|
+
rmQueryById,
|
|
215
|
+
versionQuery,
|
|
216
|
+
writeQuery,
|
|
217
|
+
writeQueryToService
|
|
218
|
+
});
|
|
219
|
+
//# sourceMappingURL=queries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/queries.ts","../src/internal/utils.ts","../src/internal/resources.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { findFileById } from './internal/utils';\nimport type { Query } from './types';\nimport { addFileToResource, getResource, rmResourceById, versionResource, writeResource } from './internal/resources';\n\n/**\n * Returns a query from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the query\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getQuery } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * const event = await getQuery('GetOrder');\n *\n * // Gets a version of the event\n * const event = await getQuery('GetOrder', '0.0.1');\n * ```\n */\nexport const getQuery =\n (directory: string) =>\n async (id: string, version?: string): Promise<Query> =>\n getResource(directory, id, version, { type: 'query' }) as Promise<Query>;\n\n/**\n * Write a query to EventCatalog.\n *\n * You can optionally override the path of the query.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeQuery } = utils('/path/to/eventcatalog');\n *\n * // Write an event to the catalog\n * // Event would be written to queries/GetOrder\n * await writeQuery({\n * id: 'GetOrder',\n * name: 'Get Order',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * });\n *\n * // Write an event to the catalog but override the path\n * // Event would be written to queries/Inventory/GetOrder\n * await writeQuery({\n * id: 'GetOrder',\n * name: 'Get Order',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Orders/GetOrder\"});\n * ```\n */\nexport const writeQuery =\n (directory: string) =>\n async (query: Query, options: { path: string } = { path: '' }) =>\n writeResource(directory, { ...query }, { ...options, type: 'query' });\n/**\n * Write a query to a service in EventCatalog.\n *\n * You can optionally override the path of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeQueryToService } = utils('/path/to/eventcatalog');\n *\n * // Write an event to a given service in the catalog\n * // Event would be written to services/Orders/queries/GetOrder\n * await writeQueryToService({\n * id: 'GetOrder',\n * name: 'Get Order',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { id: 'Orders' });\n * ```\n */\nexport const writeQueryToService =\n (directory: string) =>\n async (query: Query, service: { id: string; version?: string }, options: { path: string } = { path: '' }) => {\n let pathForQuery =\n service.version && service.version !== 'latest'\n ? `/${service.id}/versioned/${service.version}/queries`\n : `/${service.id}/queries`;\n pathForQuery = join(pathForQuery, query.id);\n await writeResource(directory, { ...query }, { ...options, path: pathForQuery, type: 'query' });\n };\n\n/**\n * Delete a query at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmQuery } = utils('/path/to/eventcatalog');\n *\n * // removes an query at the given path (queries dir is appended to the given path)\n * // Removes the query at queries/GetOrders\n * await rmQuery('/GetOrders');\n * ```\n */\nexport const rmQuery = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete a query by it's id.\n *\n * Optionally specify a version to delete a specific version of the query.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmQueryById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryAdjusted query\n * await rmQueryById('GetOrder');\n *\n * // deletes a specific version of the GetOrder query\n * await rmQueryById('GetOrder', '0.0.1');\n * ```\n */\nexport const rmQueryById = (directory: string) => async (id: string, version?: string) => {\n await rmResourceById(directory, id, version, { type: 'query' });\n};\n\n/**\n * Version a query by it's id.\n *\n * Takes the latest query and moves it to a versioned directory.\n * All files with this query are also versioned (e.g /queries/GetOrder/schema.json)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionQuery } = utils('/path/to/eventcatalog');\n *\n * // moves the latest GetOrder query to a versioned directory\n * // the version within that query is used as the version number.\n * await versionQuery('GetOrder');\n *\n * ```\n */\nexport const versionQuery = (directory: string) => async (id: string) => versionResource(directory, id);\n\n/**\n * Add a file to a query by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the query.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToQuery } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest GetOrder query\n * await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the GetOrder query\n * await addFileToQuery('GetOrder', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToQuery =\n (directory: string) => async (id: string, file: { content: string; fileName: string }, version?: string) =>\n addFileToResource(directory, id, file, version);\n\n/**\n * Add a schema to a query by it's id.\n *\n * Optionally specify a version to add a schema to a specific version of the query.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addSchemaToQuery } = utils('/path/to/eventcatalog');\n *\n * // JSON schema example\n * const schema = {\n * \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n * \"type\": \"object\",\n * \"properties\": {\n * \"name\": {\n * \"type\": \"string\"\n * },\n * \"age\": {\n * \"type\": \"number\"\n * }\n * },\n * \"required\": [\"name\", \"age\"]\n * };\n *\n * // adds a schema to the latest GetOrder query\n * await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' });\n *\n * // adds a file to a specific version of the GetOrder query\n * await addSchemaToQuery('GetOrder', { schema, fileName: 'schema.json' }, '0.0.1');\n *\n * ```\n */\nexport const addSchemaToQuery =\n (directory: string) => async (id: string, schema: { schema: string; fileName: string }, version?: string) => {\n await addFileToQuery(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);\n };\n\n/**\n * Check to see if the catalog has a version for the given query.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { queryHasVersion } = utils('/path/to/eventcatalog');\n *\n * // returns true if version is found for the given event and version (supports semver)\n * await queryHasVersion('GetOrder', '0.0.1');\n * await queryHasVersion('GetOrder', 'latest');\n * await queryHasVersion('GetOrder', '0.0.x');*\n *\n * ```\n */\nexport const queryHasVersion = (directory: string) => async (id: string, version: string) => {\n const file = await findFileById(directory, id, version);\n return !!file;\n};\n","import { glob } from 'glob';\nimport fs from 'node:fs/promises';\nimport { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';\nimport { join } from 'node:path';\nimport matter from 'gray-matter';\nimport { satisfies, validRange, valid } from 'semver';\n\n/**\n * Returns true if a given version of a resource id exists in the catalog\n */\nexport const versionExists = async (catalogDir: string, id: string, version: string) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id, version)) || [];\n return matchedFiles.length > 0;\n};\n\nexport const findFileById = async (catalogDir: string, id: string, version?: string): Promise<string | undefined> => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id)) || [];\n const latestVersion = matchedFiles.find((path) => !path.includes('versioned'));\n\n // If no version is provided, return the latest version\n if (!version) {\n return latestVersion;\n }\n\n // map files into gray matter to get versions\n const parsedFiles = matchedFiles.map((path) => {\n const { data } = matter.read(path);\n return { ...data, path };\n }) as any[];\n\n const semverRange = validRange(version);\n\n if (semverRange && valid(version)) {\n const match = parsedFiles.filter((c) => satisfies(c.version, semverRange));\n return match.length > 0 ? match[0].path : undefined;\n }\n\n // Order by version\n const sorted = parsedFiles.sort((a, b) => {\n return a.version.localeCompare(b.version);\n });\n\n // latest version\n const match = sorted.length > 0 ? [sorted[sorted.length - 1]] : [];\n\n if (match.length > 0) {\n return match[0].path;\n }\n};\n\nexport const getFiles = async (pattern: string) => {\n try {\n const files = await glob(pattern, { ignore: 'node_modules/**' });\n return files;\n } catch (error) {\n throw new Error(`Error finding files: ${error}`);\n }\n};\n\nexport const searchFilesForId = async (files: string[], id: string, version?: string) => {\n const idRegex = new RegExp(`^id:\\\\s*(['\"]|>-)?\\\\s*${id}['\"]?\\\\s*$`, 'm');\n const versionRegex = new RegExp(`^version:\\\\s*['\"]?${version}['\"]?\\\\s*$`, 'm');\n\n const matches = await Promise.all(\n files.map(async (file) => {\n const content = await fs.readFile(file, 'utf-8');\n const hasIdMatch = content.match(idRegex);\n\n // Check version if provided\n if (version && !content.match(versionRegex)) {\n return undefined;\n }\n\n if (hasIdMatch) {\n return file;\n }\n })\n );\n\n return matches.filter(Boolean).filter((file) => file !== undefined);\n};\n\n/**\n * Function to copy a directory from source to target, uses a tmp directory\n * @param catalogDir\n * @param source\n * @param target\n * @param filter\n */\nexport const copyDir = async (catalogDir: string, source: string, target: string, filter?: CopyFilterAsync | CopyFilterSync) => {\n const tmpDirectory = join(catalogDir, 'tmp');\n await fs.mkdir(tmpDirectory, { recursive: true });\n\n // Copy everything over\n await copy(source, tmpDirectory, {\n overwrite: true,\n filter,\n });\n\n await copy(tmpDirectory, target, {\n overwrite: true,\n filter,\n });\n\n // Remove the tmp directory\n await fs.rm(tmpDirectory, { recursive: true });\n};\n\n// Makes sure values in sends/recieves are unique\nexport const uniqueMessages = (messages: { id: string; version: string }[]): { id: string; version: string }[] => {\n const uniqueSet = new Set();\n\n return messages.filter((message) => {\n const key = `${message.id}-${message.version}`;\n if (!uniqueSet.has(key)) {\n uniqueSet.add(key);\n return true;\n }\n return false;\n });\n};\n","import { dirname, join } from 'path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './utils';\nimport matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { Message, Service } from '../types';\n\ntype Resource = Service | Message;\n\nexport const versionResource = async (catalogDir: string, id: string) => {\n // Find all the events in the directory\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = await searchFilesForId(files, id);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No event found with id: ${id}`);\n }\n\n // Event that is in the route of the project\n const file = matchedFiles[0];\n const sourceDirectory = dirname(file);\n const { data: { version = '0.0.1' } = {} } = matter.read(file);\n const targetDirectory = join(sourceDirectory, 'versioned', version);\n\n await fs.mkdir(targetDirectory, { recursive: true });\n\n // Copy the event to the versioned directory\n await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {\n return !src.includes('versioned');\n });\n\n // Remove all the files in the root of the resource as they have now been versioned\n await fs.readdir(sourceDirectory).then(async (resourceFiles) => {\n await Promise.all(\n resourceFiles.map(async (file) => {\n if (file !== 'versioned') {\n await fs.rm(join(sourceDirectory, file), { recursive: true });\n }\n })\n );\n });\n};\n\nexport const writeResource = async (\n catalogDir: string,\n resource: Resource,\n options: { path: string; type: string } = { path: '', type: '' }\n) => {\n // Get the path\n const path = options.path || `/${resource.id}`;\n const exists = await versionExists(catalogDir, resource.id, resource.version);\n\n if (exists) {\n throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = resource;\n const document = matter.stringify(markdown.trim(), frontmatter);\n await fs.mkdir(join(catalogDir, path), { recursive: true });\n await fs.writeFile(join(catalogDir, path, 'index.md'), document);\n};\n\nexport const getResource = async (\n catalogDir: string,\n id: string,\n version?: string,\n options?: { type: string }\n): Promise<Resource | undefined> => {\n const file = await findFileById(catalogDir, id, version);\n if (!file) return;\n\n const { data, content } = matter.read(file);\n\n return {\n ...data,\n markdown: content.trim(),\n } as Resource;\n};\n\nexport const rmResourceById = async (catalogDir: string, id: string, version?: string, options?: { type: string }) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n\n const matchedFiles = await searchFilesForId(files, id, version);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No ${options?.type || 'resource'} found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\nexport const addFileToResource = async (\n catalogDir: string,\n id: string,\n file: { content: string; fileName: string },\n version?: string\n) => {\n const pathToResource = await findFileById(catalogDir, id, version);\n\n if (!pathToResource) throw new Error('Cannot find directory to write file to');\n\n await fs.writeFile(join(dirname(pathToResource), file.fileName), file.content);\n};\n\nexport const getFileFromResource = async (catalogDir: string, id: string, file: { fileName: string }, version?: string) => {\n const pathToResource = await findFileById(catalogDir, id, version);\n\n if (!pathToResource) throw new Error('Cannot find directory of resource');\n\n const exists = await fs\n .access(join(dirname(pathToResource), file.fileName))\n .then(() => true)\n .catch(() => false);\n if (!exists) throw new Error(`File ${file.fileName} does not exist in resource ${id} v(${version})`);\n\n return fs.readFile(join(dirname(pathToResource), file.fileName), 'utf-8');\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,mBAAe;AACf,IAAAC,oBAAqB;;;ACDrB,kBAAqB;AACrB,sBAAe;AACf,sBAAsD;AACtD,uBAAqB;AACrB,yBAAmB;AACnB,oBAA6C;AAKtC,IAAM,gBAAgB,OAAO,YAAoB,IAAY,YAAoB;AACtF,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,cAAc;AACxD,QAAM,eAAgB,MAAM,iBAAiB,OAAO,IAAI,OAAO,KAAM,CAAC;AACtE,SAAO,aAAa,SAAS;AAC/B;AAEO,IAAM,eAAe,OAAO,YAAoB,IAAY,YAAkD;AACnH,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,cAAc;AACxD,QAAM,eAAgB,MAAM,iBAAiB,OAAO,EAAE,KAAM,CAAC;AAC7D,QAAM,gBAAgB,aAAa,KAAK,CAAC,SAAS,CAAC,KAAK,SAAS,WAAW,CAAC;AAG7E,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAGA,QAAM,cAAc,aAAa,IAAI,CAAC,SAAS;AAC7C,UAAM,EAAE,KAAK,IAAI,mBAAAC,QAAO,KAAK,IAAI;AACjC,WAAO,EAAE,GAAG,MAAM,KAAK;AAAA,EACzB,CAAC;AAED,QAAM,kBAAc,0BAAW,OAAO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B,OAAO,IAAY,MAA6C,YACrF,kBAAkB,WAAW,IAAI,MAAM,OAAO;AAoC3C,IAAM,mBACX,CAAC,cAAsB,OAAO,IAAY,QAA8C,YAAqB;AAC3G,QAAM,eAAe,SAAS,EAAE,IAAI,EAAE,SAAS,OAAO,QAAQ,UAAU,OAAO,SAAS,GAAG,OAAO;AACpG;AAkBK,IAAM,kBAAkB,CAAC,cAAsB,OAAO,IAAY,YAAoB;AAC3F,QAAM,OAAO,MAAM,aAAa,WAAW,IAAI,OAAO;AACtD,SAAO,CAAC,CAAC;AACX;","names":["import_promises","import_node_path","matter","match","fs","import_gray_matter","import_promises","matter","fs","file","fs"]}
|
package/dist/queries.mjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// src/queries.ts
|
|
2
|
+
import fs3 from "node:fs/promises";
|
|
3
|
+
import { join as join3 } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/internal/utils.ts
|
|
6
|
+
import { glob } from "glob";
|
|
7
|
+
import fs from "node:fs/promises";
|
|
8
|
+
import { copy } from "fs-extra";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import matter from "gray-matter";
|
|
11
|
+
import { satisfies, validRange, valid } from "semver";
|
|
12
|
+
var versionExists = async (catalogDir, id, version) => {
|
|
13
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
14
|
+
const matchedFiles = await searchFilesForId(files, id, version) || [];
|
|
15
|
+
return matchedFiles.length > 0;
|
|
16
|
+
};
|
|
17
|
+
var findFileById = async (catalogDir, id, version) => {
|
|
18
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
19
|
+
const matchedFiles = await searchFilesForId(files, id) || [];
|
|
20
|
+
const latestVersion = matchedFiles.find((path) => !path.includes("versioned"));
|
|
21
|
+
if (!version) {
|
|
22
|
+
return latestVersion;
|
|
23
|
+
}
|
|
24
|
+
const parsedFiles = matchedFiles.map((path) => {
|
|
25
|
+
const { data } = matter.read(path);
|
|
26
|
+
return { ...data, path };
|
|
27
|
+
});
|
|
28
|
+
const semverRange = validRange(version);
|
|
29
|
+
if (semverRange && valid(version)) {
|
|
30
|
+
const match2 = parsedFiles.filter((c) => satisfies(c.version, semverRange));
|
|
31
|
+
return match2.length > 0 ? match2[0].path : void 0;
|
|
32
|
+
}
|
|
33
|
+
const sorted = parsedFiles.sort((a, b) => {
|
|
34
|
+
return a.version.localeCompare(b.version);
|
|
35
|
+
});
|
|
36
|
+
const match = sorted.length > 0 ? [sorted[sorted.length - 1]] : [];
|
|
37
|
+
if (match.length > 0) {
|
|
38
|
+
return match[0].path;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var getFiles = async (pattern) => {
|
|
42
|
+
try {
|
|
43
|
+
const files = await glob(pattern, { ignore: "node_modules/**" });
|
|
44
|
+
return files;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new Error(`Error finding files: ${error}`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var searchFilesForId = async (files, id, version) => {
|
|
50
|
+
const idRegex = new RegExp(`^id:\\s*(['"]|>-)?\\s*${id}['"]?\\s*$`, "m");
|
|
51
|
+
const versionRegex = new RegExp(`^version:\\s*['"]?${version}['"]?\\s*$`, "m");
|
|
52
|
+
const matches = await Promise.all(
|
|
53
|
+
files.map(async (file) => {
|
|
54
|
+
const content = await fs.readFile(file, "utf-8");
|
|
55
|
+
const hasIdMatch = content.match(idRegex);
|
|
56
|
+
if (version && !content.match(versionRegex)) {
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
if (hasIdMatch) {
|
|
60
|
+
return file;
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
return matches.filter(Boolean).filter((file) => file !== void 0);
|
|
65
|
+
};
|
|
66
|
+
var copyDir = async (catalogDir, source, target, filter) => {
|
|
67
|
+
const tmpDirectory = join(catalogDir, "tmp");
|
|
68
|
+
await fs.mkdir(tmpDirectory, { recursive: true });
|
|
69
|
+
await copy(source, tmpDirectory, {
|
|
70
|
+
overwrite: true,
|
|
71
|
+
filter
|
|
72
|
+
});
|
|
73
|
+
await copy(tmpDirectory, target, {
|
|
74
|
+
overwrite: true,
|
|
75
|
+
filter
|
|
76
|
+
});
|
|
77
|
+
await fs.rm(tmpDirectory, { recursive: true });
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/internal/resources.ts
|
|
81
|
+
import { dirname, join as join2 } from "path";
|
|
82
|
+
import matter2 from "gray-matter";
|
|
83
|
+
import fs2 from "node:fs/promises";
|
|
84
|
+
var versionResource = async (catalogDir, id) => {
|
|
85
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
86
|
+
const matchedFiles = await searchFilesForId(files, id);
|
|
87
|
+
if (matchedFiles.length === 0) {
|
|
88
|
+
throw new Error(`No event found with id: ${id}`);
|
|
89
|
+
}
|
|
90
|
+
const file = matchedFiles[0];
|
|
91
|
+
const sourceDirectory = dirname(file);
|
|
92
|
+
const { data: { version = "0.0.1" } = {} } = matter2.read(file);
|
|
93
|
+
const targetDirectory = join2(sourceDirectory, "versioned", version);
|
|
94
|
+
await fs2.mkdir(targetDirectory, { recursive: true });
|
|
95
|
+
await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
|
|
96
|
+
return !src.includes("versioned");
|
|
97
|
+
});
|
|
98
|
+
await fs2.readdir(sourceDirectory).then(async (resourceFiles) => {
|
|
99
|
+
await Promise.all(
|
|
100
|
+
resourceFiles.map(async (file2) => {
|
|
101
|
+
if (file2 !== "versioned") {
|
|
102
|
+
await fs2.rm(join2(sourceDirectory, file2), { recursive: true });
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
var writeResource = async (catalogDir, resource, options = { path: "", type: "" }) => {
|
|
109
|
+
const path = options.path || `/${resource.id}`;
|
|
110
|
+
const exists = await versionExists(catalogDir, resource.id, resource.version);
|
|
111
|
+
if (exists) {
|
|
112
|
+
throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);
|
|
113
|
+
}
|
|
114
|
+
const { markdown, ...frontmatter } = resource;
|
|
115
|
+
const document = matter2.stringify(markdown.trim(), frontmatter);
|
|
116
|
+
await fs2.mkdir(join2(catalogDir, path), { recursive: true });
|
|
117
|
+
await fs2.writeFile(join2(catalogDir, path, "index.md"), document);
|
|
118
|
+
};
|
|
119
|
+
var getResource = async (catalogDir, id, version, options) => {
|
|
120
|
+
const file = await findFileById(catalogDir, id, version);
|
|
121
|
+
if (!file) return;
|
|
122
|
+
const { data, content } = matter2.read(file);
|
|
123
|
+
return {
|
|
124
|
+
...data,
|
|
125
|
+
markdown: content.trim()
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
var rmResourceById = async (catalogDir, id, version, options) => {
|
|
129
|
+
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
130
|
+
const matchedFiles = await searchFilesForId(files, id, version);
|
|
131
|
+
if (matchedFiles.length === 0) {
|
|
132
|
+
throw new Error(`No ${options?.type || "resource"} found with id: ${id}`);
|
|
133
|
+
}
|
|
134
|
+
await Promise.all(matchedFiles.map((file) => fs2.rm(file)));
|
|
135
|
+
};
|
|
136
|
+
var addFileToResource = async (catalogDir, id, file, version) => {
|
|
137
|
+
const pathToResource = await findFileById(catalogDir, id, version);
|
|
138
|
+
if (!pathToResource) throw new Error("Cannot find directory to write file to");
|
|
139
|
+
await fs2.writeFile(join2(dirname(pathToResource), file.fileName), file.content);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/queries.ts
|
|
143
|
+
var getQuery = (directory) => async (id, version) => getResource(directory, id, version, { type: "query" });
|
|
144
|
+
var writeQuery = (directory) => async (query, options = { path: "" }) => writeResource(directory, { ...query }, { ...options, type: "query" });
|
|
145
|
+
var writeQueryToService = (directory) => async (query, service, options = { path: "" }) => {
|
|
146
|
+
let pathForQuery = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/queries` : `/${service.id}/queries`;
|
|
147
|
+
pathForQuery = join3(pathForQuery, query.id);
|
|
148
|
+
await writeResource(directory, { ...query }, { ...options, path: pathForQuery, type: "query" });
|
|
149
|
+
};
|
|
150
|
+
var rmQuery = (directory) => async (path) => {
|
|
151
|
+
await fs3.rm(join3(directory, path), { recursive: true });
|
|
152
|
+
};
|
|
153
|
+
var rmQueryById = (directory) => async (id, version) => {
|
|
154
|
+
await rmResourceById(directory, id, version, { type: "query" });
|
|
155
|
+
};
|
|
156
|
+
var versionQuery = (directory) => async (id) => versionResource(directory, id);
|
|
157
|
+
var addFileToQuery = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
|
|
158
|
+
var addSchemaToQuery = (directory) => async (id, schema, version) => {
|
|
159
|
+
await addFileToQuery(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
|
|
160
|
+
};
|
|
161
|
+
var queryHasVersion = (directory) => async (id, version) => {
|
|
162
|
+
const file = await findFileById(directory, id, version);
|
|
163
|
+
return !!file;
|
|
164
|
+
};
|
|
165
|
+
export {
|
|
166
|
+
addFileToQuery,
|
|
167
|
+
addSchemaToQuery,
|
|
168
|
+
getQuery,
|
|
169
|
+
queryHasVersion,
|
|
170
|
+
rmQuery,
|
|
171
|
+
rmQueryById,
|
|
172
|
+
versionQuery,
|
|
173
|
+
writeQuery,
|
|
174
|
+
writeQueryToService
|
|
175
|
+
};
|
|
176
|
+
//# sourceMappingURL=queries.mjs.map
|