@eventcatalog/sdk 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/commands.d.mts +172 -0
  2. package/dist/commands.d.ts +172 -0
  3. package/dist/commands.js +191 -0
  4. package/dist/commands.js.map +1 -0
  5. package/dist/commands.mjs +150 -0
  6. package/dist/commands.mjs.map +1 -0
  7. package/dist/events.d.mts +3 -3
  8. package/dist/events.d.ts +3 -3
  9. package/dist/events.js +59 -47
  10. package/dist/events.js.map +1 -1
  11. package/dist/events.mjs +60 -48
  12. package/dist/events.mjs.map +1 -1
  13. package/dist/index.d.mts +64 -1
  14. package/dist/index.d.ts +64 -1
  15. package/dist/index.js +133 -105
  16. package/dist/index.js.map +1 -1
  17. package/dist/index.mjs +147 -119
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/services.d.mts +2 -2
  20. package/dist/services.d.ts +2 -2
  21. package/dist/services.js +54 -41
  22. package/dist/services.js.map +1 -1
  23. package/dist/services.mjs +54 -41
  24. package/dist/services.mjs.map +1 -1
  25. package/dist/types.d.js.map +1 -1
  26. package/package.json +2 -2
  27. package/dist/docs.d.mts +0 -3
  28. package/dist/docs.d.ts +0 -3
  29. package/dist/docs.js +0 -259
  30. package/dist/docs.js.map +0 -1
  31. package/dist/docs.mjs +0 -210
  32. package/dist/docs.mjs.map +0 -1
  33. package/dist/internal/utils.d.mts +0 -19
  34. package/dist/internal/utils.d.ts +0 -19
  35. package/dist/internal/utils.js +0 -103
  36. package/dist/internal/utils.js.map +0 -1
  37. package/dist/internal/utils.mjs +0 -64
  38. package/dist/internal/utils.mjs.map +0 -1
  39. package/dist/test/events.test.d.mts +0 -2
  40. package/dist/test/events.test.d.ts +0 -2
  41. package/dist/test/events.test.js +0 -16942
  42. package/dist/test/events.test.js.map +0 -1
  43. package/dist/test/events.test.mjs +0 -16927
  44. package/dist/test/events.test.mjs.map +0 -1
  45. package/dist/test/services.test.d.mts +0 -2
  46. package/dist/test/services.test.d.ts +0 -2
  47. package/dist/test/services.test.js +0 -16888
  48. package/dist/test/services.test.js.map +0 -1
  49. package/dist/test/services.test.mjs +0 -16873
  50. package/dist/test/services.test.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/events.ts","../src/internal/utils.ts"],"sourcesContent":["import matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { dirname } from 'node:path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Event } from './types';\n\n/**\n * Returns an event from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the event\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getEvent } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * cont event = await getEvent('InventoryAdjusted');\n *\n * // Gets a version of the event\n * cont event = await getEvent('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const getEvent =\n (directory: string) =>\n async (id: string, version?: string): Promise<Event> => {\n const file = await findFileById(directory, id, version);\n\n if (!file) throw new Error(`No event found for the given id: ${id}` + (version ? ` and version ${version}` : ''));\n\n const { data, content } = matter.read(file);\n\n return {\n ...data,\n markdown: content.trim(),\n } as Event;\n };\n\n/**\n * Write an event to EventCatalog.\n *\n * You can optionally overide the path of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeEvent } = utils('/path/to/eventcatalog');\n *\n * // Write an event to the catalog\n * // Event would be written to events/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\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 events/Inventory/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryAdjusted\"});\n * ```\n */\nexport const writeEvent =\n (directory: string) =>\n async (event: Event, options: { path: string } = { path: '' }) => {\n // Get the path\n const path = options.path || `/${event.id}`;\n const exists = await versionExists(directory, event.id, event.version);\n\n if (exists) {\n throw new Error(`Failed to write event as the version ${event.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = event;\n const document = matter.stringify(markdown.trim(), frontmatter);\n await fs.mkdir(join(directory, path), { recursive: true });\n await fs.writeFile(join(directory, path, 'index.md'), document);\n };\n\n/**\n * Delete an event at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEvent } = utils('/path/to/eventcatalog');\n *\n * // removes an event at the given path (events dir is appended to the given path)\n * // Removes the event at events/InventoryAdjusted\n * await rmEvent('/InventoryAdjusted');\n * ```\n */\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete an event by it's id.\n *\n * Optionally specify a version to delete a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEventById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryAdjusted event\n * await rmEventById('InventoryAdjusted');\n *\n * // deletes a specific version of the InventoryAdjusted event\n * await rmEventById('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const rmEventById = (directory: string) => async (id: string, version?: string) => {\n // Find all the events in the directory\n const files = await getFiles(`${directory}/**/index.md`);\n\n const matchedFiles = await searchFilesForId(files, id, version);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No event found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\n/**\n * Version an event by it's id.\n *\n * Takes the latest event and moves it to a versioned directory.\n * All files with this event are also versioned (e.g /events/InventoryAdjusted/schema.json)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionEvent } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryAdjusted event to a versioned directory\n * // the version within that event is used as the version number.\n * await versionEvent('InventoryAdjusted');\n *\n * ```\n */\nexport const versionEvent = (directory: string) => async (id: string) => {\n // Find all the events in the directory\n const files = await getFiles(`${directory}/**/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 eventDirectory = dirname(file);\n const { data: { version = '0.0.1' } = {} } = matter.read(file);\n const targetDirectory = join(eventDirectory, 'versioned', version);\n\n await fs.mkdir(targetDirectory, { recursive: true });\n\n // Copy the event to the versioned directory\n await copyDir(directory, eventDirectory, 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(eventDirectory).then(async (resourceFiles) => {\n await Promise.all(\n resourceFiles.map(async (file) => {\n if (file !== 'versioned') {\n await fs.rm(join(eventDirectory, file), { recursive: true });\n }\n })\n );\n });\n};\n\n/**\n * Add a file to an event by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToEvent } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToEvent =\n (directory: string) => async (id: string, file: { content: string; fileName: string }, version?: string) => {\n const pathToEvent = await findFileById(directory, id, version);\n if (!pathToEvent) throw new Error('Cannot find directory to write file to');\n const contentDirectory = dirname(pathToEvent);\n await fs.writeFile(join(contentDirectory, file.fileName), file.content);\n };\n\n/**\n * Add a schema to an event by it's id.\n *\n * Optionally specify a version to add a schame to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addSchemaToEvent } = 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 InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' }, '0.0.1');\n *\n * ```\n */\nexport const addSchemaToEvent =\n (directory: string) => async (id: string, schema: { schema: string; fileName: string }, version?: string) => {\n await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);\n };\n","import { glob } from 'glob';\nimport fs from 'node:fs/promises';\nimport { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';\nimport { join } from 'node:path';\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\n // Return the latest one\n if (!version) {\n return matchedFiles.find((path) => !path.includes('versioned'));\n }\n\n // Find the versioned event\n return matchedFiles.find((path) => path.includes(`versioned/${version}`));\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*['\"]?${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"],"mappings":";AAAA,OAAO,YAAY;AACnB,OAAOA,SAAQ;AACf,SAAS,QAAAC,aAAY;AACrB,SAAS,eAAe;;;ACHxB,SAAS,YAAY;AACrB,OAAO,QAAQ;AACf,SAAS,YAA6C;AACtD,SAAS,YAAY;AAKd,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;AAG7D,MAAI,CAAC,SAAS;AACZ,WAAO,aAAa,KAAK,CAAC,SAAS,CAAC,KAAK,SAAS,WAAW,CAAC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;AAC3G,QAAM,eAAe,SAAS,EAAE,IAAI,EAAE,SAAS,OAAO,QAAQ,UAAU,OAAO,SAAS,GAAG,OAAO;AACpG;","names":["fs","join","fs","join","file"]}
1
+ {"version":3,"sources":["../src/events.ts","../src/internal/resources.ts","../src/internal/utils.ts"],"sourcesContent":["import matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { dirname } from 'node:path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Event } from './types';\nimport { addFileToResource, getResource, rmResourceById, versionResource, writeResource } from './internal/resources';\n\n/**\n * Returns an event from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the event\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getEvent } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * const event = await getEvent('InventoryAdjusted');\n *\n * // Gets a version of the event\n * const event = await getEvent('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const getEvent =\n (directory: string) =>\n async (id: string, version?: string): Promise<Event> =>\n getResource(directory, id, version, { type: 'event' }) as Promise<Event>;\n\n/**\n * Write an event to EventCatalog.\n *\n * You can optionally overide the path of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeEvent } = utils('/path/to/eventcatalog');\n *\n * // Write an event to the catalog\n * // Event would be written to events/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\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 events/Inventory/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryAdjusted\"});\n * ```\n */\nexport const writeEvent =\n (directory: string) =>\n async (event: Event, options: { path: string } = { path: '' }) =>\n writeResource(directory, { ...event }, { ...options, type: 'event' });\n\n/**\n * Delete an event at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEvent } = utils('/path/to/eventcatalog');\n *\n * // removes an event at the given path (events dir is appended to the given path)\n * // Removes the event at events/InventoryAdjusted\n * await rmEvent('/InventoryAdjusted');\n * ```\n */\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete an event by it's id.\n *\n * Optionally specify a version to delete a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEventById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryAdjusted event\n * await rmEventById('InventoryAdjusted');\n *\n * // deletes a specific version of the InventoryAdjusted event\n * await rmEventById('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const rmEventById = (directory: string) => async (id: string, version?: string) =>\n rmResourceById(directory, id, version, { type: 'event' });\n\n/**\n * Version an event by it's id.\n *\n * Takes the latest event and moves it to a versioned directory.\n * All files with this event are also versioned (e.g /events/InventoryAdjusted/schema.json)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionEvent } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryAdjusted event to a versioned directory\n * // the version within that event is used as the version number.\n * await versionEvent('InventoryAdjusted');\n *\n * ```\n */\nexport const versionEvent = (directory: string) => async (id: string) => versionResource(directory, id);\n\n/**\n * Add a file to an event by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToEvent } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToEvent =\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 an event by it's id.\n *\n * Optionally specify a version to add a schema to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addSchemaToEvent } = 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 InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' }, '0.0.1');\n *\n * ```\n */\nexport const addSchemaToEvent =\n (directory: string) => async (id: string, schema: { schema: string; fileName: string }, version?: string) => {\n await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);\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> => {\n const file = await findFileById(catalogDir, id, version);\n\n if (!file)\n throw new Error(\n `No ${options?.type || 'resource'} found for the given id: ${id}` + (version ? ` and version ${version}` : '')\n );\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 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","import { glob } from 'glob';\nimport fs from 'node:fs/promises';\nimport { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';\nimport { join } from 'node:path';\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\n // Return the latest one\n if (!version) {\n return matchedFiles.find((path) => !path.includes('versioned'));\n }\n\n // Find the versioned event\n return matchedFiles.find((path) => path.includes(`versioned/${version}`));\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*['\"]?${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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AC3G,QAAM,eAAe,SAAS,EAAE,IAAI,EAAE,SAAS,OAAO,QAAQ,UAAU,OAAO,SAAS,GAAG,OAAO;AACpG;","names":["fs","join","join","fs","join","file","fs","join"]}
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { Event, Service } from './types.d.mjs';
1
+ import { Event, Command, Service } from './types.d.mjs';
2
2
 
3
3
  /**
4
4
  * Init the SDK for EventCatalog
@@ -65,6 +65,69 @@ declare const _default: (path: string) => {
65
65
  schema: string;
66
66
  fileName: string;
67
67
  }, version?: string) => Promise<void>;
68
+ /**
69
+ * ================================
70
+ * Commands
71
+ * ================================
72
+ */
73
+ /**
74
+ * Returns a command from EventCatalog
75
+ * @param id - The id of the command to retrieve
76
+ * @param version - Optional id of the version to get
77
+ * @returns
78
+ */
79
+ getCommand: (id: string, version?: string) => Promise<Command>;
80
+ /**
81
+ * Adds an command to EventCatalog
82
+ *
83
+ * @param command - The command to write
84
+ * @param options - Optional options to write the command
85
+ *
86
+ */
87
+ writeCommand: (command: Command, options?: {
88
+ path: string;
89
+ }) => Promise<void>;
90
+ /**
91
+ * Remove an command to EventCatalog (modeled on the standard POSIX rm utility)
92
+ *
93
+ * @param path - The path to your command, e.g. `/Inventory/InventoryAdjusted`
94
+ *
95
+ */
96
+ rmCommand: (path: string) => Promise<void>;
97
+ /**
98
+ * Remove an command by an Event id
99
+ *
100
+ * @param id - The id of the command you want to remove
101
+ *
102
+ */
103
+ rmCommandById: (id: string, version?: string) => Promise<void>;
104
+ /**
105
+ * Moves a given command id to the version directory
106
+ * @param directory
107
+ */
108
+ versionCommand: (id: string) => Promise<void>;
109
+ /**
110
+ * Adds a file to the given command
111
+ * @param id - The id of the command to add the file to
112
+ * @param file - File contents to add including the content and the file name
113
+ * @param version - Optional version of the command to add the file to
114
+ * @returns
115
+ */
116
+ addFileToCommand: (id: string, file: {
117
+ content: string;
118
+ fileName: string;
119
+ }, version?: string) => Promise<void>;
120
+ /**
121
+ * Adds a schema to the given command
122
+ * @param id - The id of the command to add the schema to
123
+ * @param schema - Schema contents to add including the content and the file name
124
+ * @param version - Optional version of the command to add the schema to
125
+ * @returns
126
+ */
127
+ addSchemaToCommand: (id: string, schema: {
128
+ schema: string;
129
+ fileName: string;
130
+ }, version?: string) => Promise<void>;
68
131
  /**
69
132
  * ================================
70
133
  * SERVICES
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Event, Service } from './types.d.js';
1
+ import { Event, Command, Service } from './types.d.js';
2
2
 
3
3
  /**
4
4
  * Init the SDK for EventCatalog
@@ -65,6 +65,69 @@ declare const _default: (path: string) => {
65
65
  schema: string;
66
66
  fileName: string;
67
67
  }, version?: string) => Promise<void>;
68
+ /**
69
+ * ================================
70
+ * Commands
71
+ * ================================
72
+ */
73
+ /**
74
+ * Returns a command from EventCatalog
75
+ * @param id - The id of the command to retrieve
76
+ * @param version - Optional id of the version to get
77
+ * @returns
78
+ */
79
+ getCommand: (id: string, version?: string) => Promise<Command>;
80
+ /**
81
+ * Adds an command to EventCatalog
82
+ *
83
+ * @param command - The command to write
84
+ * @param options - Optional options to write the command
85
+ *
86
+ */
87
+ writeCommand: (command: Command, options?: {
88
+ path: string;
89
+ }) => Promise<void>;
90
+ /**
91
+ * Remove an command to EventCatalog (modeled on the standard POSIX rm utility)
92
+ *
93
+ * @param path - The path to your command, e.g. `/Inventory/InventoryAdjusted`
94
+ *
95
+ */
96
+ rmCommand: (path: string) => Promise<void>;
97
+ /**
98
+ * Remove an command by an Event id
99
+ *
100
+ * @param id - The id of the command you want to remove
101
+ *
102
+ */
103
+ rmCommandById: (id: string, version?: string) => Promise<void>;
104
+ /**
105
+ * Moves a given command id to the version directory
106
+ * @param directory
107
+ */
108
+ versionCommand: (id: string) => Promise<void>;
109
+ /**
110
+ * Adds a file to the given command
111
+ * @param id - The id of the command to add the file to
112
+ * @param file - File contents to add including the content and the file name
113
+ * @param version - Optional version of the command to add the file to
114
+ * @returns
115
+ */
116
+ addFileToCommand: (id: string, file: {
117
+ content: string;
118
+ fileName: string;
119
+ }, version?: string) => Promise<void>;
120
+ /**
121
+ * Adds a schema to the given command
122
+ * @param id - The id of the command to add the schema to
123
+ * @param schema - Schema contents to add including the content and the file name
124
+ * @param version - Optional version of the command to add the schema to
125
+ * @returns
126
+ */
127
+ addSchemaToCommand: (id: string, schema: {
128
+ schema: string;
129
+ fileName: string;
130
+ }, version?: string) => Promise<void>;
68
131
  /**
69
132
  * ================================
70
133
  * SERVICES
package/dist/index.js CHANGED
@@ -36,10 +36,11 @@ module.exports = __toCommonJS(src_exports);
36
36
  var import_node_path5 = require("path");
37
37
 
38
38
  // src/events.ts
39
- var import_gray_matter = __toESM(require("gray-matter"));
40
- var import_promises2 = __toESM(require("fs/promises"));
39
+ var import_promises3 = __toESM(require("fs/promises"));
41
40
  var import_node_path2 = require("path");
42
- var import_node_path3 = require("path");
41
+
42
+ // src/internal/resources.ts
43
+ var import_path = require("path");
43
44
 
44
45
  // src/internal/utils.ts
45
46
  var import_glob = require("glob");
@@ -98,138 +99,110 @@ var copyDir = async (catalogDir, source, target, filter) => {
98
99
  await import_promises.default.rm(tmpDirectory, { recursive: true });
99
100
  };
100
101
 
101
- // src/events.ts
102
- var getEvent = (directory) => async (id, version) => {
103
- const file = await findFileById(directory, id, version);
104
- if (!file) throw new Error(`No event found for the given id: ${id}` + (version ? ` and version ${version}` : ""));
105
- const { data, content } = import_gray_matter.default.read(file);
106
- return {
107
- ...data,
108
- markdown: content.trim()
109
- };
110
- };
111
- var writeEvent = (directory) => async (event, options = { path: "" }) => {
112
- const path = options.path || `/${event.id}`;
113
- const exists = await versionExists(directory, event.id, event.version);
114
- if (exists) {
115
- throw new Error(`Failed to write event as the version ${event.version} already exists`);
116
- }
117
- const { markdown, ...frontmatter } = event;
118
- const document = import_gray_matter.default.stringify(markdown.trim(), frontmatter);
119
- await import_promises2.default.mkdir((0, import_node_path2.join)(directory, path), { recursive: true });
120
- await import_promises2.default.writeFile((0, import_node_path2.join)(directory, path, "index.md"), document);
121
- };
122
- var rmEvent = (directory) => async (path) => {
123
- await import_promises2.default.rm((0, import_node_path2.join)(directory, path), { recursive: true });
124
- };
125
- var rmEventById = (directory) => async (id, version) => {
126
- const files = await getFiles(`${directory}/**/index.md`);
127
- const matchedFiles = await searchFilesForId(files, id, version);
128
- if (matchedFiles.length === 0) {
129
- throw new Error(`No event found with id: ${id}`);
130
- }
131
- await Promise.all(matchedFiles.map((file) => import_promises2.default.rm(file)));
132
- };
133
- var versionEvent = (directory) => async (id) => {
134
- const files = await getFiles(`${directory}/**/index.md`);
102
+ // src/internal/resources.ts
103
+ var import_gray_matter = __toESM(require("gray-matter"));
104
+ var import_promises2 = __toESM(require("fs/promises"));
105
+ var versionResource = async (catalogDir, id) => {
106
+ const files = await getFiles(`${catalogDir}/**/index.md`);
135
107
  const matchedFiles = await searchFilesForId(files, id);
136
108
  if (matchedFiles.length === 0) {
137
109
  throw new Error(`No event found with id: ${id}`);
138
110
  }
139
111
  const file = matchedFiles[0];
140
- const eventDirectory = (0, import_node_path3.dirname)(file);
112
+ const sourceDirectory = (0, import_path.dirname)(file);
141
113
  const { data: { version = "0.0.1" } = {} } = import_gray_matter.default.read(file);
142
- const targetDirectory = (0, import_node_path2.join)(eventDirectory, "versioned", version);
114
+ const targetDirectory = (0, import_path.join)(sourceDirectory, "versioned", version);
143
115
  await import_promises2.default.mkdir(targetDirectory, { recursive: true });
144
- await copyDir(directory, eventDirectory, targetDirectory, (src) => {
116
+ await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
145
117
  return !src.includes("versioned");
146
118
  });
147
- await import_promises2.default.readdir(eventDirectory).then(async (resourceFiles) => {
119
+ await import_promises2.default.readdir(sourceDirectory).then(async (resourceFiles) => {
148
120
  await Promise.all(
149
121
  resourceFiles.map(async (file2) => {
150
122
  if (file2 !== "versioned") {
151
- await import_promises2.default.rm((0, import_node_path2.join)(eventDirectory, file2), { recursive: true });
123
+ await import_promises2.default.rm((0, import_path.join)(sourceDirectory, file2), { recursive: true });
152
124
  }
153
125
  })
154
126
  );
155
127
  });
156
128
  };
157
- var addFileToEvent = (directory) => async (id, file, version) => {
158
- const pathToEvent = await findFileById(directory, id, version);
159
- if (!pathToEvent) throw new Error("Cannot find directory to write file to");
160
- const contentDirectory = (0, import_node_path3.dirname)(pathToEvent);
161
- await import_promises2.default.writeFile((0, import_node_path2.join)(contentDirectory, file.fileName), file.content);
162
- };
163
- var addSchemaToEvent = (directory) => async (id, schema, version) => {
164
- await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
129
+ var writeResource = async (catalogDir, resource, options = { path: "", type: "" }) => {
130
+ const path = options.path || `/${resource.id}`;
131
+ const exists = await versionExists(catalogDir, resource.id, resource.version);
132
+ if (exists) {
133
+ throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);
134
+ }
135
+ const { markdown, ...frontmatter } = resource;
136
+ const document = import_gray_matter.default.stringify(markdown.trim(), frontmatter);
137
+ await import_promises2.default.mkdir((0, import_path.join)(catalogDir, path), { recursive: true });
138
+ await import_promises2.default.writeFile((0, import_path.join)(catalogDir, path, "index.md"), document);
165
139
  };
166
-
167
- // src/services.ts
168
- var import_gray_matter2 = __toESM(require("gray-matter"));
169
- var import_promises3 = __toESM(require("fs/promises"));
170
- var import_node_path4 = require("path");
171
- var getService = (directory) => async (id, version) => {
172
- const file = await findFileById(directory, id, version);
173
- if (!file) throw new Error(`No service found for the given id: ${id}` + (version ? ` and version ${version}` : ""));
174
- const { data, content } = import_gray_matter2.default.read(file);
140
+ var getResource = async (catalogDir, id, version, options) => {
141
+ const file = await findFileById(catalogDir, id, version);
142
+ if (!file)
143
+ throw new Error(
144
+ `No ${options?.type || "resource"} found for the given id: ${id}` + (version ? ` and version ${version}` : "")
145
+ );
146
+ const { data, content } = import_gray_matter.default.read(file);
175
147
  return {
176
148
  ...data,
177
149
  markdown: content.trim()
178
150
  };
179
151
  };
180
- var writeService = (directory) => async (service, options = { path: "" }) => {
181
- const path = options.path || `/${service.id}`;
182
- const exists = await versionExists(directory, service.id, service.version);
183
- if (exists) {
184
- throw new Error(`Failed to write service as the version ${service.version} already exists`);
185
- }
186
- const { markdown, ...frontmatter } = service;
187
- const document = import_gray_matter2.default.stringify(markdown.trim(), frontmatter);
188
- await import_promises3.default.mkdir((0, import_node_path4.join)(directory, path), { recursive: true });
189
- await import_promises3.default.writeFile((0, import_node_path4.join)(directory, path, "index.md"), document);
190
- };
191
- var versionService = (directory) => async (id) => {
192
- const files = await getFiles(`${directory}/**/index.md`);
193
- const matchedFiles = await searchFilesForId(files, id);
152
+ var rmResourceById = async (catalogDir, id, version, options) => {
153
+ const files = await getFiles(`${catalogDir}/**/index.md`);
154
+ const matchedFiles = await searchFilesForId(files, id, version);
194
155
  if (matchedFiles.length === 0) {
195
- throw new Error(`No service found with id: ${id}`);
156
+ throw new Error(`No ${options?.type || "resource"} found with id: ${id}`);
196
157
  }
197
- const file = matchedFiles[0];
198
- const eventDirectory = (0, import_node_path4.dirname)(file);
199
- const { data: { version = "0.0.1" } = {} } = import_gray_matter2.default.read(file);
200
- const targetDirectory = (0, import_node_path4.join)(eventDirectory, "versioned", version);
201
- await import_promises3.default.mkdir(targetDirectory, { recursive: true });
202
- await copyDir(directory, eventDirectory, targetDirectory, (src) => {
203
- return !src.includes("versioned");
204
- });
205
- await import_promises3.default.readdir(eventDirectory).then(async (resourceFiles) => {
206
- await Promise.all(
207
- resourceFiles.map(async (file2) => {
208
- if (file2 !== "versioned") {
209
- await import_promises3.default.rm((0, import_node_path4.join)(eventDirectory, file2), { recursive: true });
210
- }
211
- })
212
- );
213
- });
158
+ await Promise.all(matchedFiles.map((file) => import_promises2.default.rm(file)));
214
159
  };
215
- var rmService = (directory) => async (path) => {
216
- await import_promises3.default.rm((0, import_node_path4.join)(directory, path), { recursive: true });
160
+ var addFileToResource = async (catalogDir, id, file, version) => {
161
+ const pathToResource = await findFileById(catalogDir, id, version);
162
+ if (!pathToResource) throw new Error("Cannot find directory to write file to");
163
+ await import_promises2.default.writeFile((0, import_path.join)((0, import_path.dirname)(pathToResource), file.fileName), file.content);
217
164
  };
218
- var rmServiceById = (directory) => async (id, version) => {
219
- const files = await getFiles(`${directory}/**/index.md`);
220
- const matchedFiles = await searchFilesForId(files, id, version);
221
- if (matchedFiles.length === 0) {
222
- throw new Error(`No service found with id: ${id}`);
223
- }
224
- await Promise.all(matchedFiles.map((file) => import_promises3.default.rm(file)));
165
+
166
+ // src/events.ts
167
+ var getEvent = (directory) => async (id, version) => getResource(directory, id, version, { type: "event" });
168
+ var writeEvent = (directory) => async (event, options = { path: "" }) => writeResource(directory, { ...event }, { ...options, type: "event" });
169
+ var rmEvent = (directory) => async (path) => {
170
+ await import_promises3.default.rm((0, import_node_path2.join)(directory, path), { recursive: true });
171
+ };
172
+ var rmEventById = (directory) => async (id, version) => rmResourceById(directory, id, version, { type: "event" });
173
+ var versionEvent = (directory) => async (id) => versionResource(directory, id);
174
+ var addFileToEvent = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
175
+ var addSchemaToEvent = (directory) => async (id, schema, version) => {
176
+ await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
225
177
  };
226
- var addFileToService = (directory) => async (id, file, version) => {
227
- const pathToEvent = await findFileById(directory, id, version);
228
- if (!pathToEvent) throw new Error("Cannot find directory to write file to");
229
- const contentDirectory = (0, import_node_path4.dirname)(pathToEvent);
230
- await import_promises3.default.writeFile((0, import_node_path4.join)(contentDirectory, file.fileName), file.content);
178
+
179
+ // src/commands.ts
180
+ var import_promises4 = __toESM(require("fs/promises"));
181
+ var import_node_path3 = require("path");
182
+ var getCommand = (directory) => async (id, version) => getResource(directory, id, version, { type: "command" });
183
+ var writeCommand = (directory) => async (command, options = { path: "" }) => writeResource(directory, { ...command }, { ...options, type: "command" });
184
+ var rmCommand = (directory) => async (path) => {
185
+ await import_promises4.default.rm((0, import_node_path3.join)(directory, path), { recursive: true });
186
+ };
187
+ var rmCommandById = (directory) => async (id, version) => rmResourceById(directory, id, version, { type: "command" });
188
+ var versionCommand = (directory) => async (id) => versionResource(directory, id);
189
+ var addFileToCommand = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
190
+ var addSchemaToCommand = (directory) => async (id, schema, version) => {
191
+ await addFileToCommand(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
231
192
  };
232
193
 
194
+ // src/services.ts
195
+ var import_promises5 = __toESM(require("fs/promises"));
196
+ var import_node_path4 = require("path");
197
+ var getService = (directory) => async (id, version) => getResource(directory, id, version, { type: "service" });
198
+ var writeService = (directory) => async (service, options = { path: "" }) => writeResource(directory, { ...service }, { ...options, type: "service" });
199
+ var versionService = (directory) => async (id) => versionResource(directory, id);
200
+ var rmService = (directory) => async (path) => {
201
+ await import_promises5.default.rm((0, import_node_path4.join)(directory, path), { recursive: true });
202
+ };
203
+ var rmServiceById = (directory) => async (id, version) => rmResourceById(directory, id, version, { type: "service" });
204
+ var addFileToService = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
205
+
233
206
  // src/index.ts
234
207
  var src_default = (path) => {
235
208
  return {
@@ -283,6 +256,61 @@ var src_default = (path) => {
283
256
  * @returns
284
257
  */
285
258
  addSchemaToEvent: addSchemaToEvent((0, import_node_path5.join)(path, "events")),
259
+ /**
260
+ * ================================
261
+ * Commands
262
+ * ================================
263
+ */
264
+ /**
265
+ * Returns a command from EventCatalog
266
+ * @param id - The id of the command to retrieve
267
+ * @param version - Optional id of the version to get
268
+ * @returns
269
+ */
270
+ getCommand: getCommand((0, import_node_path5.join)(path, "commands")),
271
+ /**
272
+ * Adds an command to EventCatalog
273
+ *
274
+ * @param command - The command to write
275
+ * @param options - Optional options to write the command
276
+ *
277
+ */
278
+ writeCommand: writeCommand((0, import_node_path5.join)(path, "commands")),
279
+ /**
280
+ * Remove an command to EventCatalog (modeled on the standard POSIX rm utility)
281
+ *
282
+ * @param path - The path to your command, e.g. `/Inventory/InventoryAdjusted`
283
+ *
284
+ */
285
+ rmCommand: rmCommand((0, import_node_path5.join)(path, "commands")),
286
+ /**
287
+ * Remove an command by an Event id
288
+ *
289
+ * @param id - The id of the command you want to remove
290
+ *
291
+ */
292
+ rmCommandById: rmCommandById((0, import_node_path5.join)(path, "commands")),
293
+ /**
294
+ * Moves a given command id to the version directory
295
+ * @param directory
296
+ */
297
+ versionCommand: versionCommand((0, import_node_path5.join)(path, "commands")),
298
+ /**
299
+ * Adds a file to the given command
300
+ * @param id - The id of the command to add the file to
301
+ * @param file - File contents to add including the content and the file name
302
+ * @param version - Optional version of the command to add the file to
303
+ * @returns
304
+ */
305
+ addFileToCommand: addFileToCommand((0, import_node_path5.join)(path, "commands")),
306
+ /**
307
+ * Adds a schema to the given command
308
+ * @param id - The id of the command to add the schema to
309
+ * @param schema - Schema contents to add including the content and the file name
310
+ * @param version - Optional version of the command to add the schema to
311
+ * @returns
312
+ */
313
+ addSchemaToCommand: addSchemaToCommand((0, import_node_path5.join)(path, "commands")),
286
314
  /**
287
315
  * ================================
288
316
  * SERVICES