@eventcatalog/sdk 1.3.2 → 1.4.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.
@@ -0,0 +1,83 @@
1
+ import { Team } from './types.d.mjs';
2
+
3
+ /**
4
+ * Returns a team from EventCatalog.
5
+ *
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import utils from '@eventcatalog/utils';
10
+ *
11
+ * const { getTeam } = utils('/path/to/eventcatalog');
12
+ *
13
+ * // Gets the team with the given id
14
+ * const team = await getTeam('eventcatalog-core-team');
15
+ *
16
+ * ```
17
+ */
18
+ declare const getTeam: (catalogDir: string) => (id: string) => Promise<Team | undefined>;
19
+ /**
20
+ * Returns all teams from EventCatalog.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import utils from '@eventcatalog/utils';
25
+ *
26
+ * const { getTeams } = utils('/path/to/eventcatalog');
27
+ *
28
+ * // Gets all teams from the catalog
29
+ * const channels = await getTeams();
30
+ *
31
+ * ```
32
+ */
33
+ declare const getTeams: (catalogDir: string) => (options?: {}) => Promise<Team[]>;
34
+ /**
35
+ * Write a team to EventCatalog.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import utils from '@eventcatalog/utils';
40
+ *
41
+ * const { writeTeam } = utils('/path/to/eventcatalog');
42
+ *
43
+ * // Write a team to the catalog
44
+ * // team would be written to teams/EventCatalogCoreTeam
45
+ * await writeTeam({
46
+ * id: 'eventcatalog-core-team',
47
+ * name: 'EventCatalogCoreTeam',
48
+ * members: ['dboyne', 'asmith', 'msmith'],
49
+ * email: 'test@test.com',
50
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
51
+ * });
52
+ *
53
+ * // Write a team to the catalog and override the existing content (if there is any)
54
+ * await writeTeam({
55
+ * id: 'eventcatalog-core-team',
56
+ * name: 'EventCatalogCoreTeam',
57
+ * members: ['dboyne', 'asmith', 'msmith'],
58
+ * email: 'test@test.com',
59
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
60
+ * }, { override: true });
61
+ *
62
+ * ```
63
+ */
64
+ declare const writeTeam: (catalogDir: string) => (team: Team, options?: {
65
+ override?: boolean;
66
+ }) => Promise<void>;
67
+ /**
68
+ * Delete a team by it's id.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import utils from '@eventcatalog/utils';
73
+ *
74
+ * const { rmTeamById } = utils('/path/to/eventcatalog');
75
+ *
76
+ * // deletes the EventCatalogCoreTeam team
77
+ * await rmTeamById('eventcatalog-core-team');
78
+ *
79
+ * ```
80
+ */
81
+ declare const rmTeamById: (catalogDir: string) => (id: string) => Promise<void>;
82
+
83
+ export { getTeam, getTeams, rmTeamById, writeTeam };
@@ -0,0 +1,83 @@
1
+ import { Team } from './types.d.js';
2
+
3
+ /**
4
+ * Returns a team from EventCatalog.
5
+ *
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import utils from '@eventcatalog/utils';
10
+ *
11
+ * const { getTeam } = utils('/path/to/eventcatalog');
12
+ *
13
+ * // Gets the team with the given id
14
+ * const team = await getTeam('eventcatalog-core-team');
15
+ *
16
+ * ```
17
+ */
18
+ declare const getTeam: (catalogDir: string) => (id: string) => Promise<Team | undefined>;
19
+ /**
20
+ * Returns all teams from EventCatalog.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import utils from '@eventcatalog/utils';
25
+ *
26
+ * const { getTeams } = utils('/path/to/eventcatalog');
27
+ *
28
+ * // Gets all teams from the catalog
29
+ * const channels = await getTeams();
30
+ *
31
+ * ```
32
+ */
33
+ declare const getTeams: (catalogDir: string) => (options?: {}) => Promise<Team[]>;
34
+ /**
35
+ * Write a team to EventCatalog.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import utils from '@eventcatalog/utils';
40
+ *
41
+ * const { writeTeam } = utils('/path/to/eventcatalog');
42
+ *
43
+ * // Write a team to the catalog
44
+ * // team would be written to teams/EventCatalogCoreTeam
45
+ * await writeTeam({
46
+ * id: 'eventcatalog-core-team',
47
+ * name: 'EventCatalogCoreTeam',
48
+ * members: ['dboyne', 'asmith', 'msmith'],
49
+ * email: 'test@test.com',
50
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
51
+ * });
52
+ *
53
+ * // Write a team to the catalog and override the existing content (if there is any)
54
+ * await writeTeam({
55
+ * id: 'eventcatalog-core-team',
56
+ * name: 'EventCatalogCoreTeam',
57
+ * members: ['dboyne', 'asmith', 'msmith'],
58
+ * email: 'test@test.com',
59
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
60
+ * }, { override: true });
61
+ *
62
+ * ```
63
+ */
64
+ declare const writeTeam: (catalogDir: string) => (team: Team, options?: {
65
+ override?: boolean;
66
+ }) => Promise<void>;
67
+ /**
68
+ * Delete a team by it's id.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import utils from '@eventcatalog/utils';
73
+ *
74
+ * const { rmTeamById } = utils('/path/to/eventcatalog');
75
+ *
76
+ * // deletes the EventCatalogCoreTeam team
77
+ * await rmTeamById('eventcatalog-core-team');
78
+ *
79
+ * ```
80
+ */
81
+ declare const rmTeamById: (catalogDir: string) => (id: string) => Promise<void>;
82
+
83
+ export { getTeam, getTeams, rmTeamById, writeTeam };
package/dist/teams.js ADDED
@@ -0,0 +1,107 @@
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/teams.ts
31
+ var teams_exports = {};
32
+ __export(teams_exports, {
33
+ getTeam: () => getTeam,
34
+ getTeams: () => getTeams,
35
+ rmTeamById: () => rmTeamById,
36
+ writeTeam: () => writeTeam
37
+ });
38
+ module.exports = __toCommonJS(teams_exports);
39
+ var import_promises2 = __toESM(require("fs/promises"));
40
+ var import_node_path2 = require("path");
41
+ var import_gray_matter2 = __toESM(require("gray-matter"));
42
+
43
+ // src/internal/utils.ts
44
+ var import_glob = require("glob");
45
+ var import_promises = __toESM(require("fs/promises"));
46
+ var import_fs_extra = require("fs-extra");
47
+ var import_node_path = require("path");
48
+ var import_gray_matter = __toESM(require("gray-matter"));
49
+ var import_semver = require("semver");
50
+ var getFiles = async (pattern, ignore = "") => {
51
+ try {
52
+ const files = await (0, import_glob.glob)(pattern, { ignore: ["node_modules/**", ignore] });
53
+ return files;
54
+ } catch (error) {
55
+ throw new Error(`Error finding files: ${error}`);
56
+ }
57
+ };
58
+
59
+ // src/teams.ts
60
+ var getTeam = (catalogDir) => async (id) => {
61
+ const files = await getFiles(`${catalogDir}/${id}.md`);
62
+ if (files.length == 0) return void 0;
63
+ const file = files[0];
64
+ const { data, content } = import_gray_matter2.default.read(file);
65
+ return {
66
+ ...data,
67
+ id: data.id,
68
+ name: data.name,
69
+ markdown: content.trim()
70
+ };
71
+ };
72
+ var getTeams = (catalogDir) => async (options) => {
73
+ const files = await getFiles(`${catalogDir}/teams/*.md`);
74
+ if (files.length === 0) return [];
75
+ return files.map((file) => {
76
+ const { data, content } = import_gray_matter2.default.read(file);
77
+ return {
78
+ ...data,
79
+ id: data.id,
80
+ name: data.name,
81
+ markdown: content.trim()
82
+ };
83
+ });
84
+ };
85
+ var writeTeam = (catalogDir) => async (team, options = {}) => {
86
+ const resource = { ...team };
87
+ const currentTeam = await getTeam(catalogDir)(resource.id);
88
+ const exists = currentTeam !== void 0;
89
+ if (exists && !options.override) {
90
+ throw new Error(`Failed to write ${resource.id} (team) as it already exists`);
91
+ }
92
+ const { markdown, ...frontmatter } = resource;
93
+ const document = import_gray_matter2.default.stringify(markdown, frontmatter);
94
+ await import_promises2.default.mkdir((0, import_node_path2.join)(catalogDir, ""), { recursive: true });
95
+ await import_promises2.default.writeFile((0, import_node_path2.join)(catalogDir, "", `${resource.id}.md`), document);
96
+ };
97
+ var rmTeamById = (catalogDir) => async (id) => {
98
+ await import_promises2.default.rm((0, import_node_path2.join)(catalogDir, `${id}.md`), { recursive: true });
99
+ };
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ getTeam,
103
+ getTeams,
104
+ rmTeamById,
105
+ writeTeam
106
+ });
107
+ //# sourceMappingURL=teams.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/teams.ts","../src/internal/utils.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport type { Team } from './types';\nimport matter from 'gray-matter';\nimport { getFiles } from './internal/utils';\n\n/**\n * Returns a team from EventCatalog.\n *\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getTeam } = utils('/path/to/eventcatalog');\n *\n * // Gets the team with the given id\n * const team = await getTeam('eventcatalog-core-team');\n *\n * ```\n */\nexport const getTeam =\n (catalogDir: string) =>\n async (id: string): Promise<Team | undefined> => {\n const files = await getFiles(`${catalogDir}/${id}.md`);\n\n if (files.length == 0) return undefined;\n const file = files[0];\n\n const { data, content } = matter.read(file);\n return {\n ...data,\n id: data.id,\n name: data.name,\n markdown: content.trim(),\n } as Team;\n };\n\n/**\n * Returns all teams from EventCatalog.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getTeams } = utils('/path/to/eventcatalog');\n *\n * // Gets all teams from the catalog\n * const channels = await getTeams();\n *\n * ```\n */\nexport const getTeams =\n (catalogDir: string) =>\n async (options?: {}): Promise<Team[]> => {\n const files = await getFiles(`${catalogDir}/teams/*.md`);\n if (files.length === 0) return [];\n\n return files.map((file) => {\n const { data, content } = matter.read(file);\n return {\n ...data,\n id: data.id,\n name: data.name,\n markdown: content.trim(),\n } as Team;\n });\n };\n\n/**\n * Write a team to EventCatalog.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeTeam } = utils('/path/to/eventcatalog');\n *\n * // Write a team to the catalog\n * // team would be written to teams/EventCatalogCoreTeam\n * await writeTeam({\n * id: 'eventcatalog-core-team',\n * name: 'EventCatalogCoreTeam',\n * members: ['dboyne', 'asmith', 'msmith'],\n * email: 'test@test.com',\n * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123\n * });\n *\n * // Write a team to the catalog and override the existing content (if there is any)\n * await writeTeam({\n * id: 'eventcatalog-core-team',\n * name: 'EventCatalogCoreTeam',\n * members: ['dboyne', 'asmith', 'msmith'],\n * email: 'test@test.com',\n * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123\n * }, { override: true });\n *\n * ```\n */\nexport const writeTeam =\n (catalogDir: string) =>\n async (team: Team, options: { override?: boolean } = {}) => {\n const resource: Team = { ...team };\n\n // Get the path\n const currentTeam = await getTeam(catalogDir)(resource.id);\n const exists = currentTeam !== undefined;\n\n if (exists && !options.override) {\n throw new Error(`Failed to write ${resource.id} (team) as it already exists`);\n }\n\n const { markdown, ...frontmatter } = resource;\n\n const document = matter.stringify(markdown, frontmatter);\n await fs.mkdir(join(catalogDir, ''), { recursive: true });\n await fs.writeFile(join(catalogDir, '', `${resource.id}.md`), document);\n };\n\n/**\n * Delete a team by it's id.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmTeamById } = utils('/path/to/eventcatalog');\n *\n * // deletes the EventCatalogCoreTeam team\n * await rmTeamById('eventcatalog-core-team');\n *\n * ```\n */\nexport const rmTeamById = (catalogDir: string) => async (id: string) => {\n await fs.rm(join(catalogDir, `${id}.md`), { recursive: true });\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, ignore: string = '') => {\n try {\n const files = await glob(pattern, { ignore: ['node_modules/**', ignore] });\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 uniqueVersions = (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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,mBAAe;AACf,IAAAC,oBAAqB;AAErB,IAAAC,sBAAmB;;;ACHnB,kBAAqB;AACrB,sBAAe;AACf,sBAAsD;AACtD,uBAAqB;AACrB,yBAAmB;AACnB,oBAA6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AS,EAAE,8BAA8B;AAAA,EAC9E;AAEA,QAAM,EAAE,UAAU,GAAG,YAAY,IAAI;AAErC,QAAM,WAAW,oBAAAA,QAAO,UAAU,UAAU,WAAW;AACvD,QAAM,iBAAAC,QAAG,UAAM,wBAAK,YAAY,EAAE,GAAG,EAAE,WAAW,KAAK,CAAC;AACxD,QAAM,iBAAAA,QAAG,cAAU,wBAAK,YAAY,IAAI,GAAG,SAAS,EAAE,KAAK,GAAG,QAAQ;AACxE;AAgBK,IAAM,aAAa,CAAC,eAAuB,OAAO,OAAe;AACtE,QAAM,iBAAAA,QAAG,OAAG,wBAAK,YAAY,GAAG,EAAE,KAAK,GAAG,EAAE,WAAW,KAAK,CAAC;AAC/D;","names":["import_promises","import_node_path","import_gray_matter","matter","fs"]}
package/dist/teams.mjs ADDED
@@ -0,0 +1,67 @@
1
+ // src/teams.ts
2
+ import fs from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import matter2 from "gray-matter";
5
+
6
+ // src/internal/utils.ts
7
+ import { glob } from "glob";
8
+ import { copy } from "fs-extra";
9
+ import matter from "gray-matter";
10
+ import { satisfies, validRange, valid } from "semver";
11
+ var getFiles = async (pattern, ignore = "") => {
12
+ try {
13
+ const files = await glob(pattern, { ignore: ["node_modules/**", ignore] });
14
+ return files;
15
+ } catch (error) {
16
+ throw new Error(`Error finding files: ${error}`);
17
+ }
18
+ };
19
+
20
+ // src/teams.ts
21
+ var getTeam = (catalogDir) => async (id) => {
22
+ const files = await getFiles(`${catalogDir}/${id}.md`);
23
+ if (files.length == 0) return void 0;
24
+ const file = files[0];
25
+ const { data, content } = matter2.read(file);
26
+ return {
27
+ ...data,
28
+ id: data.id,
29
+ name: data.name,
30
+ markdown: content.trim()
31
+ };
32
+ };
33
+ var getTeams = (catalogDir) => async (options) => {
34
+ const files = await getFiles(`${catalogDir}/teams/*.md`);
35
+ if (files.length === 0) return [];
36
+ return files.map((file) => {
37
+ const { data, content } = matter2.read(file);
38
+ return {
39
+ ...data,
40
+ id: data.id,
41
+ name: data.name,
42
+ markdown: content.trim()
43
+ };
44
+ });
45
+ };
46
+ var writeTeam = (catalogDir) => async (team, options = {}) => {
47
+ const resource = { ...team };
48
+ const currentTeam = await getTeam(catalogDir)(resource.id);
49
+ const exists = currentTeam !== void 0;
50
+ if (exists && !options.override) {
51
+ throw new Error(`Failed to write ${resource.id} (team) as it already exists`);
52
+ }
53
+ const { markdown, ...frontmatter } = resource;
54
+ const document = matter2.stringify(markdown, frontmatter);
55
+ await fs.mkdir(join(catalogDir, ""), { recursive: true });
56
+ await fs.writeFile(join(catalogDir, "", `${resource.id}.md`), document);
57
+ };
58
+ var rmTeamById = (catalogDir) => async (id) => {
59
+ await fs.rm(join(catalogDir, `${id}.md`), { recursive: true });
60
+ };
61
+ export {
62
+ getTeam,
63
+ getTeams,
64
+ rmTeamById,
65
+ writeTeam
66
+ };
67
+ //# sourceMappingURL=teams.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/teams.ts","../src/internal/utils.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport type { Team } from './types';\nimport matter from 'gray-matter';\nimport { getFiles } from './internal/utils';\n\n/**\n * Returns a team from EventCatalog.\n *\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getTeam } = utils('/path/to/eventcatalog');\n *\n * // Gets the team with the given id\n * const team = await getTeam('eventcatalog-core-team');\n *\n * ```\n */\nexport const getTeam =\n (catalogDir: string) =>\n async (id: string): Promise<Team | undefined> => {\n const files = await getFiles(`${catalogDir}/${id}.md`);\n\n if (files.length == 0) return undefined;\n const file = files[0];\n\n const { data, content } = matter.read(file);\n return {\n ...data,\n id: data.id,\n name: data.name,\n markdown: content.trim(),\n } as Team;\n };\n\n/**\n * Returns all teams from EventCatalog.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getTeams } = utils('/path/to/eventcatalog');\n *\n * // Gets all teams from the catalog\n * const channels = await getTeams();\n *\n * ```\n */\nexport const getTeams =\n (catalogDir: string) =>\n async (options?: {}): Promise<Team[]> => {\n const files = await getFiles(`${catalogDir}/teams/*.md`);\n if (files.length === 0) return [];\n\n return files.map((file) => {\n const { data, content } = matter.read(file);\n return {\n ...data,\n id: data.id,\n name: data.name,\n markdown: content.trim(),\n } as Team;\n });\n };\n\n/**\n * Write a team to EventCatalog.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeTeam } = utils('/path/to/eventcatalog');\n *\n * // Write a team to the catalog\n * // team would be written to teams/EventCatalogCoreTeam\n * await writeTeam({\n * id: 'eventcatalog-core-team',\n * name: 'EventCatalogCoreTeam',\n * members: ['dboyne', 'asmith', 'msmith'],\n * email: 'test@test.com',\n * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123\n * });\n *\n * // Write a team to the catalog and override the existing content (if there is any)\n * await writeTeam({\n * id: 'eventcatalog-core-team',\n * name: 'EventCatalogCoreTeam',\n * members: ['dboyne', 'asmith', 'msmith'],\n * email: 'test@test.com',\n * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123\n * }, { override: true });\n *\n * ```\n */\nexport const writeTeam =\n (catalogDir: string) =>\n async (team: Team, options: { override?: boolean } = {}) => {\n const resource: Team = { ...team };\n\n // Get the path\n const currentTeam = await getTeam(catalogDir)(resource.id);\n const exists = currentTeam !== undefined;\n\n if (exists && !options.override) {\n throw new Error(`Failed to write ${resource.id} (team) as it already exists`);\n }\n\n const { markdown, ...frontmatter } = resource;\n\n const document = matter.stringify(markdown, frontmatter);\n await fs.mkdir(join(catalogDir, ''), { recursive: true });\n await fs.writeFile(join(catalogDir, '', `${resource.id}.md`), document);\n };\n\n/**\n * Delete a team by it's id.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmTeamById } = utils('/path/to/eventcatalog');\n *\n * // deletes the EventCatalogCoreTeam team\n * await rmTeamById('eventcatalog-core-team');\n *\n * ```\n */\nexport const rmTeamById = (catalogDir: string) => async (id: string) => {\n await fs.rm(join(catalogDir, `${id}.md`), { recursive: true });\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, ignore: string = '') => {\n try {\n const files = await glob(pattern, { ignore: ['node_modules/**', ignore] });\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 uniqueVersions = (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"],"mappings":";AAAA,OAAO,QAAQ;AACf,SAAS,YAAY;AAErB,OAAOA,aAAY;;;ACHnB,SAAS,YAAY;AAErB,SAAS,YAA6C;AAEtD,OAAO,YAAY;AACnB,SAAS,WAAW,YAAY,aAAa;AA+CtC,IAAM,WAAW,OAAO,SAAiB,SAAiB,OAAO;AACtE,MAAI;AACF,UAAM,QAAQ,MAAM,KAAK,SAAS,EAAE,QAAQ,CAAC,mBAAmB,MAAM,EAAE,CAAC;AACzE,WAAO;AAAA,EACT,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,wBAAwB,KAAK,EAAE;AAAA,EACjD;AACF;;;ADtCO,IAAM,UACX,CAAC,eACD,OAAO,OAA0C;AAC/C,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,IAAI,EAA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Y,GAAG,EAAE,KAAK,GAAG,EAAE,WAAW,KAAK,CAAC;AAC/D;","names":["matter","matter"]}
@@ -70,6 +70,7 @@ interface Team {
70
70
  ownedCommands?: Command[];
71
71
  ownedServices?: Service[];
72
72
  ownedEvents?: Event[];
73
+ markdown: string;
73
74
  }
74
75
 
75
76
  interface User {
@@ -84,6 +85,7 @@ interface User {
84
85
  ownedEvents?: Event[];
85
86
  ownedCommands?: Command[];
86
87
  associatedTeams?: Team[];
88
+ markdown: string;
87
89
  }
88
90
 
89
91
  interface Badge {
package/dist/types.d.d.ts CHANGED
@@ -70,6 +70,7 @@ interface Team {
70
70
  ownedCommands?: Command[];
71
71
  ownedServices?: Service[];
72
72
  ownedEvents?: Event[];
73
+ markdown: string;
73
74
  }
74
75
 
75
76
  interface User {
@@ -84,6 +85,7 @@ interface User {
84
85
  ownedEvents?: Event[];
85
86
  ownedCommands?: Command[];
86
87
  associatedTeams?: Team[];
88
+ markdown: string;
87
89
  }
88
90
 
89
91
  interface Badge {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.d.ts"],"sourcesContent":["// Base type for all resources (domains, services and messages)\nexport interface BaseSchema {\n id: string;\n name: string;\n summary?: string;\n version: string;\n badges?: Badge[];\n owners?: string[];\n schemaPath?: string;\n markdown: string;\n}\n\nexport type ResourcePointer = {\n id: string;\n version: string;\n};\n\nexport interface ChannelPointer extends ResourcePointer {\n parameters?: Record<string, string>;\n}\n\nexport type Message = Event | Command;\n\nenum ResourceType {\n Service = 'service',\n Event = 'event',\n Command = 'command',\n}\n\nexport interface Event extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Command extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Query extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Channel extends BaseSchema {\n address?: string;\n protocols?: string[];\n // parameters?: Record<string, Parameter>;\n parameters?: {\n [key: string]: {\n enum?: string[];\n default?: string;\n examples?: string[];\n description?: string;\n };\n };\n}\n\nexport interface Specifications {\n asyncapiPath?: string;\n openapiPath?: string;\n}\n\nexport interface Service extends BaseSchema {\n sends?: ResourcePointer[];\n receives?: ResourcePointer[];\n specifications?: Specifications;\n}\n\nexport interface Domain extends BaseSchema {\n services?: ResourcePointer[];\n}\n\nexport interface Team {\n id: string;\n name: string;\n summary?: string;\n email?: string;\n hidden?: boolean;\n slackDirectMessageUrl?: string;\n members?: User[];\n ownedCommands?: Command[];\n ownedServices?: Service[];\n ownedEvents?: Event[];\n}\n\nexport interface User {\n id: string;\n name: string;\n avatarUrl: string;\n role?: string;\n hidden?: boolean;\n email?: string;\n slackDirectMessageUrl?: string;\n ownedServices?: Service[];\n ownedEvents?: Event[];\n ownedCommands?: Command[];\n associatedTeams?: Team[];\n}\n\nexport interface Badge {\n content: string;\n backgroundColor: string;\n textColor: string;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
1
+ {"version":3,"sources":["../src/types.d.ts"],"sourcesContent":["// Base type for all resources (domains, services and messages)\nexport interface BaseSchema {\n id: string;\n name: string;\n summary?: string;\n version: string;\n badges?: Badge[];\n owners?: string[];\n schemaPath?: string;\n markdown: string;\n}\n\nexport type ResourcePointer = {\n id: string;\n version: string;\n};\n\nexport interface ChannelPointer extends ResourcePointer {\n parameters?: Record<string, string>;\n}\n\nexport type Message = Event | Command;\n\nenum ResourceType {\n Service = 'service',\n Event = 'event',\n Command = 'command',\n}\n\nexport interface Event extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Command extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Query extends BaseSchema {\n channels?: ChannelPointer[];\n}\nexport interface Channel extends BaseSchema {\n address?: string;\n protocols?: string[];\n // parameters?: Record<string, Parameter>;\n parameters?: {\n [key: string]: {\n enum?: string[];\n default?: string;\n examples?: string[];\n description?: string;\n };\n };\n}\n\nexport interface Specifications {\n asyncapiPath?: string;\n openapiPath?: string;\n}\n\nexport interface Service extends BaseSchema {\n sends?: ResourcePointer[];\n receives?: ResourcePointer[];\n specifications?: Specifications;\n}\n\nexport interface Domain extends BaseSchema {\n services?: ResourcePointer[];\n}\n\nexport interface Team {\n id: string;\n name: string;\n summary?: string;\n email?: string;\n hidden?: boolean;\n slackDirectMessageUrl?: string;\n members?: User[];\n ownedCommands?: Command[];\n ownedServices?: Service[];\n ownedEvents?: Event[];\n markdown: string;\n}\n\nexport interface User {\n id: string;\n name: string;\n avatarUrl: string;\n role?: string;\n hidden?: boolean;\n email?: string;\n slackDirectMessageUrl?: string;\n ownedServices?: Service[];\n ownedEvents?: Event[];\n ownedCommands?: Command[];\n associatedTeams?: Team[];\n markdown: string;\n}\n\nexport interface Badge {\n content: string;\n backgroundColor: string;\n textColor: string;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
@@ -0,0 +1,83 @@
1
+ import { User } from './types.d.mjs';
2
+
3
+ /**
4
+ * Returns a user from EventCatalog.
5
+ *
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import utils from '@eventcatalog/utils';
10
+ *
11
+ * const { getUser } = utils('/path/to/eventcatalog');
12
+ *
13
+ * // Gets the user with the given id
14
+ * const user = await getUser('eventcatalog-core-user');
15
+ *
16
+ * ```
17
+ */
18
+ declare const getUser: (catalogDir: string) => (id: string) => Promise<User | undefined>;
19
+ /**
20
+ * Returns all users from EventCatalog.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import utils from '@eventcatalog/utils';
25
+ *
26
+ * const { getUsers } = utils('/path/to/eventcatalog');
27
+ *
28
+ * // Gets all users from the catalog
29
+ * const channels = await getUsers();
30
+ *
31
+ * ```
32
+ */
33
+ declare const getUsers: (catalogDir: string) => (options?: {}) => Promise<User[]>;
34
+ /**
35
+ * Write a user to EventCatalog.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import utils from '@eventcatalog/utils';
40
+ *
41
+ * const { writeUser } = utils('/path/to/eventcatalog');
42
+ *
43
+ * // Write a user to the catalog
44
+ * // user would be written to users/eventcatalog-tech-lead
45
+ * await writeUser({
46
+ * id: 'eventcatalog-tech-lead',
47
+ * name: 'EventCatalog Tech Lead',
48
+ * email: 'test@test.com',
49
+ * avatarUrl: 'https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png',
50
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
51
+ * });
52
+ *
53
+ * // Write a team to the catalog and override the existing content (if there is any)
54
+ * await writeUser({
55
+ * id: 'eventcatalog-tech-lead',
56
+ * name: 'EventCatalog Tech Lead',
57
+ * email: 'test@test.com',
58
+ * avatarUrl: 'https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png',
59
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
60
+ * }, { override: true });
61
+ *
62
+ * ```
63
+ */
64
+ declare const writeUser: (catalogDir: string) => (user: User, options?: {
65
+ override?: boolean;
66
+ }) => Promise<void>;
67
+ /**
68
+ * Delete a user by it's id.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import utils from '@eventcatalog/utils';
73
+ *
74
+ * const { rmUserById } = utils('/path/to/eventcatalog');
75
+ *
76
+ * // deletes the user with id eventcatalog-core-user
77
+ * await rmUserById('eventcatalog-core-user');
78
+ *
79
+ * ```
80
+ */
81
+ declare const rmUserById: (catalogDir: string) => (id: string) => Promise<void>;
82
+
83
+ export { getUser, getUsers, rmUserById, writeUser };
@@ -0,0 +1,83 @@
1
+ import { User } from './types.d.js';
2
+
3
+ /**
4
+ * Returns a user from EventCatalog.
5
+ *
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import utils from '@eventcatalog/utils';
10
+ *
11
+ * const { getUser } = utils('/path/to/eventcatalog');
12
+ *
13
+ * // Gets the user with the given id
14
+ * const user = await getUser('eventcatalog-core-user');
15
+ *
16
+ * ```
17
+ */
18
+ declare const getUser: (catalogDir: string) => (id: string) => Promise<User | undefined>;
19
+ /**
20
+ * Returns all users from EventCatalog.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import utils from '@eventcatalog/utils';
25
+ *
26
+ * const { getUsers } = utils('/path/to/eventcatalog');
27
+ *
28
+ * // Gets all users from the catalog
29
+ * const channels = await getUsers();
30
+ *
31
+ * ```
32
+ */
33
+ declare const getUsers: (catalogDir: string) => (options?: {}) => Promise<User[]>;
34
+ /**
35
+ * Write a user to EventCatalog.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import utils from '@eventcatalog/utils';
40
+ *
41
+ * const { writeUser } = utils('/path/to/eventcatalog');
42
+ *
43
+ * // Write a user to the catalog
44
+ * // user would be written to users/eventcatalog-tech-lead
45
+ * await writeUser({
46
+ * id: 'eventcatalog-tech-lead',
47
+ * name: 'EventCatalog Tech Lead',
48
+ * email: 'test@test.com',
49
+ * avatarUrl: 'https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png',
50
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
51
+ * });
52
+ *
53
+ * // Write a team to the catalog and override the existing content (if there is any)
54
+ * await writeUser({
55
+ * id: 'eventcatalog-tech-lead',
56
+ * name: 'EventCatalog Tech Lead',
57
+ * email: 'test@test.com',
58
+ * avatarUrl: 'https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png',
59
+ * slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123
60
+ * }, { override: true });
61
+ *
62
+ * ```
63
+ */
64
+ declare const writeUser: (catalogDir: string) => (user: User, options?: {
65
+ override?: boolean;
66
+ }) => Promise<void>;
67
+ /**
68
+ * Delete a user by it's id.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import utils from '@eventcatalog/utils';
73
+ *
74
+ * const { rmUserById } = utils('/path/to/eventcatalog');
75
+ *
76
+ * // deletes the user with id eventcatalog-core-user
77
+ * await rmUserById('eventcatalog-core-user');
78
+ *
79
+ * ```
80
+ */
81
+ declare const rmUserById: (catalogDir: string) => (id: string) => Promise<void>;
82
+
83
+ export { getUser, getUsers, rmUserById, writeUser };