@eventcatalog/sdk 0.0.2 → 0.0.4
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/dist/docs.d.mts +1 -0
- package/dist/docs.d.ts +1 -0
- package/dist/docs.js +80 -2
- package/dist/docs.js.map +1 -1
- package/dist/docs.mjs +73 -1
- package/dist/docs.mjs.map +1 -1
- package/dist/events.d.mts +137 -2
- package/dist/events.d.ts +137 -2
- package/dist/events.js.map +1 -1
- package/dist/events.mjs.map +1 -1
- package/dist/index.d.mts +53 -1
- package/dist/index.d.ts +53 -1
- package/dist/index.js +121 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -8
- package/dist/index.mjs.map +1 -1
- package/dist/services.d.mts +133 -0
- package/dist/services.d.ts +133 -0
- package/dist/services.js +173 -0
- package/dist/services.js.map +1 -0
- package/dist/services.mjs +133 -0
- package/dist/services.mjs.map +1 -0
- package/dist/test/events.test.js +147 -33
- package/dist/test/events.test.js.map +1 -1
- package/dist/test/events.test.mjs +149 -35
- package/dist/test/events.test.mjs.map +1 -1
- package/dist/test/services.test.d.mts +2 -0
- package/dist/test/services.test.d.ts +2 -0
- package/dist/test/services.test.js +16888 -0
- package/dist/test/services.test.js.map +1 -0
- package/dist/test/services.test.mjs +16873 -0
- package/dist/test/services.test.mjs.map +1 -0
- package/dist/types.d.d.mts +53 -12
- package/dist/types.d.d.ts +53 -12
- package/dist/types.d.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/events.ts","../src/internal/utils.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { rmEvent, rmEventById, writeEvent, versionEvent, getEvent, addFileToEvent, addSchemaToEvent } from './events';\n\n/**\n * Init the SDK for EventCatalog\n *\n * @param path - The path to the EventCatalog directory\n *\n */\nexport default (path: string) => {\n return {\n /**\n * Returns an events from EventCatalog\n * @param id - The id of the event to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getEvent: getEvent(join(path, 'events')),\n /**\n * Adds an event to EventCatalog\n *\n * @param event - The event to write\n * @param options - Optional options to write the event\n *\n */\n writeEvent: writeEvent(join(path, 'events')),\n /**\n * Remove an event to EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`\n *\n */\n rmEvent: rmEvent(join(path, 'events')),\n /**\n * Remove an event by an Event id\n *\n * @param id - The id of the event you want to remove\n *\n */\n rmEventById: rmEventById(join(path, 'events')),\n /**\n * Moves a given event id to the version directory\n * @param directory\n */\n versionEvent: versionEvent(join(path, 'events')),\n /**\n * Adds a file to the given event\n * @param id - The id of the event to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the event to add the file to\n * @returns\n */\n addFileToEvent: addFileToEvent(join(path, 'events')),\n /**\n * Adds a schema to the given event\n * @param id - The id of the event to add the schema to\n * @param schema - Schema contents to add including the content and the file name\n * @param version - Optional version of the event to add the schema to\n * @returns\n */\n addSchemaToEvent: addSchemaToEvent(join(path, 'events')),\n };\n};\n","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\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\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\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\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\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\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;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,oBAAqB;;;ACArB,yBAAmB;AACnB,IAAAC,mBAAe;AACf,IAAAC,oBAAqB;AACrB,IAAAA,oBAAwB;;;ACHxB,kBAAqB;AACrB,sBAAe;AACf,sBAAsD;AACtD,uBAAqB;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;AAAA,EAChE;AAGA,SAAO,aAAa,KAAK,CAAC,SAAS,KAAK,SAAS,aAAa,OAAO,EAAE,CAAC;AAC1E;AAEO,IAAM,WAAW,OAAO,YAAoB;AACjD,MAAI;AACF,UAAM,QAAQ,UAAM,kBAAK,SAAS,EAAE,QAAQ,kBAAkB,CAAC;AAC/D,WAAO;AAAA,EACT,SAAS,OAAO;AACd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AAiB;AAC/B,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOL,UAAU,aAAS,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQvC,YAAY,eAAW,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAO3C,SAAS,YAAQ,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOrC,aAAa,gBAAY,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,IAK7C,cAAc,iBAAa,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQ/C,gBAAgB,mBAAe,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQnD,kBAAkB,qBAAiB,wBAAK,MAAM,QAAQ,CAAC;AAAA,EACzD;AACF;","names":["import_node_path","import_promises","import_node_path","fs","matter","fs","file"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/events.ts","../src/internal/utils.ts","../src/services.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { rmEvent, rmEventById, writeEvent, versionEvent, getEvent, addFileToEvent, addSchemaToEvent } from './events';\nimport { writeService, getService, versionService, rmService, rmServiceById, addFileToService } from './services';\n\n/**\n * Init the SDK for EventCatalog\n *\n * @param path - The path to the EventCatalog directory\n *\n */\nexport default (path: string) => {\n return {\n /**\n * Returns an events from EventCatalog\n * @param id - The id of the event to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getEvent: getEvent(join(path, 'events')),\n /**\n * Adds an event to EventCatalog\n *\n * @param event - The event to write\n * @param options - Optional options to write the event\n *\n */\n writeEvent: writeEvent(join(path, 'events')),\n /**\n * Remove an event to EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`\n *\n */\n rmEvent: rmEvent(join(path, 'events')),\n /**\n * Remove an event by an Event id\n *\n * @param id - The id of the event you want to remove\n *\n */\n rmEventById: rmEventById(join(path, 'events')),\n /**\n * Moves a given event id to the version directory\n * @param directory\n */\n versionEvent: versionEvent(join(path, 'events')),\n /**\n * Adds a file to the given event\n * @param id - The id of the event to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the event to add the file to\n * @returns\n */\n addFileToEvent: addFileToEvent(join(path, 'events')),\n /**\n * Adds a schema to the given event\n * @param id - The id of the event to add the schema to\n * @param schema - Schema contents to add including the content and the file name\n * @param version - Optional version of the event to add the schema to\n * @returns\n */\n addSchemaToEvent: addSchemaToEvent(join(path, 'events')),\n\n /**\n * ================================\n * SERVICES\n * ================================\n */\n\n /**\n * Adds a service to EventCatalog\n *\n * @param service - The service to write\n * @param options - Optional options to write the event\n *\n */\n writeService: writeService(join(path, 'services')),\n /**\n * Returns a service from EventCatalog\n * @param id - The id of the service to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getService: getService(join(path, 'services')),\n /**\n * Moves a given service id to the version directory\n * @param directory\n */\n versionService: versionService(join(path, 'services')),\n /**\n * Remove a service from EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your service, e.g. `/InventoryService`\n *\n */\n rmService: rmService(join(path, 'services')),\n /**\n * Remove an service by an service id\n *\n * @param id - The id of the service you want to remove\n *\n */\n rmServiceById: rmServiceById(join(path, 'services')),\n /**\n * Adds a file to the given service\n * @param id - The id of the service to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the service to add the file to\n * @returns\n */\n addFileToService: addFileToService(join(path, 'services')),\n };\n};\n","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","import matter from 'gray-matter';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Service } from './types';\nimport fs from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\n\n/**\n * Returns a service from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the service\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getService } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * cont event = await getService('InventoryService');\n *\n * // Gets a version of the event\n * cont event = await getService('InventoryService', '0.0.1');\n * ```\n */\nexport const getService =\n (directory: string) =>\n async (id: string, version?: string): Promise<Service> => {\n const file = await findFileById(directory, id, version);\n\n if (!file) throw new Error(`No service 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 Service;\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 { writeService } = utils('/path/to/eventcatalog');\n *\n * // Write a service\n * // Event would be written to services/InventoryService\n * await writeService({\n * id: 'InventoryService',\n * name: 'Inventory Service',\n * version: '0.0.1',\n * summary: 'Service that handles the inventory',\n * markdown: '# Hello world',\n * });\n *\n * // Write an event to the catalog but override the path\n * // Event would be written to services/Inventory/InventoryService\n * await writeService({\n * id: 'InventoryService',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryService\"});\n * ```\n */\nexport const writeService =\n (directory: string) =>\n async (service: Service, options: { path: string } = { path: '' }) => {\n // Get the path\n const path = options.path || `/${service.id}`;\n const exists = await versionExists(directory, service.id, service.version);\n\n if (exists) {\n throw new Error(`Failed to write service as the version ${service.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = service;\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 * Version a service by it's id.\n *\n * Takes the latest service and moves it to a versioned directory.\n * All files with this service are also versioned. (e.g /services/InventoryService/openapi.yml)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionService } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryService service to a versioned directory\n * // the version within that service is used as the version number.\n * await versionService('InventoryService');\n *\n * ```\n */\nexport const versionService = (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 service found with id: ${id}`);\n }\n\n // Service 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 service 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 * Delete a service at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmService } = utils('/path/to/eventcatalog');\n *\n * // Removes the service at services/InventoryService\n * await rmService('/InventoryService');\n * ```\n */\nexport const rmService = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete a service by it's id.\n *\n * Optionally specify a version to delete a specific version of the service.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmServiceById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryService event\n * await rmServiceById('InventoryService');\n *\n * // deletes a specific version of the InventoryService event\n * await rmServiceById('InventoryService', '0.0.1');\n * ```\n */\nexport const rmServiceById = (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 service found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\n/**\n * Add a file to a service by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the service.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToService } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryService event\n * await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryService event\n * await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToService =\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,oBAAqB;;;ACArB,yBAAmB;AACnB,IAAAC,mBAAe;AACf,IAAAC,oBAAqB;AACrB,IAAAA,oBAAwB;;;ACHxB,kBAAqB;AACrB,sBAAe;AACf,sBAAsD;AACtD,u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;AAAA;AAAA,IAK7C,cAAc,iBAAa,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQ/C,gBAAgB,mBAAe,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQnD,kBAAkB,qBAAiB,wBAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAevD,cAAc,iBAAa,wBAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOjD,YAAY,eAAW,wBAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,IAK7C,gBAAgB,mBAAe,wBAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOrD,WAAW,cAAU,wBAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAO3C,eAAe,kBAAc,wBAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQnD,kBAAkB,qBAAiB,wBAAK,MAAM,UAAU,CAAC;AAAA,EAC3D;AACF;","names":["import_node_path","import_promises","import_node_path","fs","matter","fs","file","import_gray_matter","import_promises","import_node_path","matter","fs","file"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { join as
|
|
2
|
+
import { join as join4 } from "path";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import matter from "gray-matter";
|
|
@@ -130,6 +130,72 @@ var addSchemaToEvent = (directory) => async (id, schema, version) => {
|
|
|
130
130
|
await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
|
|
131
131
|
};
|
|
132
132
|
|
|
133
|
+
// src/services.ts
|
|
134
|
+
import matter2 from "gray-matter";
|
|
135
|
+
import fs3 from "fs/promises";
|
|
136
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
137
|
+
var getService = (directory) => async (id, version) => {
|
|
138
|
+
const file = await findFileById(directory, id, version);
|
|
139
|
+
if (!file) throw new Error(`No service found for the given id: ${id}` + (version ? ` and version ${version}` : ""));
|
|
140
|
+
const { data, content } = matter2.read(file);
|
|
141
|
+
return {
|
|
142
|
+
...data,
|
|
143
|
+
markdown: content.trim()
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
var writeService = (directory) => async (service, options = { path: "" }) => {
|
|
147
|
+
const path = options.path || `/${service.id}`;
|
|
148
|
+
const exists = await versionExists(directory, service.id, service.version);
|
|
149
|
+
if (exists) {
|
|
150
|
+
throw new Error(`Failed to write service as the version ${service.version} already exists`);
|
|
151
|
+
}
|
|
152
|
+
const { markdown, ...frontmatter } = service;
|
|
153
|
+
const document = matter2.stringify(markdown.trim(), frontmatter);
|
|
154
|
+
await fs3.mkdir(join3(directory, path), { recursive: true });
|
|
155
|
+
await fs3.writeFile(join3(directory, path, "index.md"), document);
|
|
156
|
+
};
|
|
157
|
+
var versionService = (directory) => async (id) => {
|
|
158
|
+
const files = await getFiles(`${directory}/**/index.md`);
|
|
159
|
+
const matchedFiles = await searchFilesForId(files, id);
|
|
160
|
+
if (matchedFiles.length === 0) {
|
|
161
|
+
throw new Error(`No service found with id: ${id}`);
|
|
162
|
+
}
|
|
163
|
+
const file = matchedFiles[0];
|
|
164
|
+
const eventDirectory = dirname2(file);
|
|
165
|
+
const { data: { version = "0.0.1" } = {} } = matter2.read(file);
|
|
166
|
+
const targetDirectory = join3(eventDirectory, "versioned", version);
|
|
167
|
+
await fs3.mkdir(targetDirectory, { recursive: true });
|
|
168
|
+
await copyDir(directory, eventDirectory, targetDirectory, (src) => {
|
|
169
|
+
return !src.includes("versioned");
|
|
170
|
+
});
|
|
171
|
+
await fs3.readdir(eventDirectory).then(async (resourceFiles) => {
|
|
172
|
+
await Promise.all(
|
|
173
|
+
resourceFiles.map(async (file2) => {
|
|
174
|
+
if (file2 !== "versioned") {
|
|
175
|
+
await fs3.rm(join3(eventDirectory, file2), { recursive: true });
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
var rmService = (directory) => async (path) => {
|
|
182
|
+
await fs3.rm(join3(directory, path), { recursive: true });
|
|
183
|
+
};
|
|
184
|
+
var rmServiceById = (directory) => async (id, version) => {
|
|
185
|
+
const files = await getFiles(`${directory}/**/index.md`);
|
|
186
|
+
const matchedFiles = await searchFilesForId(files, id, version);
|
|
187
|
+
if (matchedFiles.length === 0) {
|
|
188
|
+
throw new Error(`No service found with id: ${id}`);
|
|
189
|
+
}
|
|
190
|
+
await Promise.all(matchedFiles.map((file) => fs3.rm(file)));
|
|
191
|
+
};
|
|
192
|
+
var addFileToService = (directory) => async (id, file, version) => {
|
|
193
|
+
const pathToEvent = await findFileById(directory, id, version);
|
|
194
|
+
if (!pathToEvent) throw new Error("Cannot find directory to write file to");
|
|
195
|
+
const contentDirectory = dirname2(pathToEvent);
|
|
196
|
+
await fs3.writeFile(join3(contentDirectory, file.fileName), file.content);
|
|
197
|
+
};
|
|
198
|
+
|
|
133
199
|
// src/index.ts
|
|
134
200
|
var src_default = (path) => {
|
|
135
201
|
return {
|
|
@@ -139,7 +205,7 @@ var src_default = (path) => {
|
|
|
139
205
|
* @param version - Optional id of the version to get
|
|
140
206
|
* @returns
|
|
141
207
|
*/
|
|
142
|
-
getEvent: getEvent(
|
|
208
|
+
getEvent: getEvent(join4(path, "events")),
|
|
143
209
|
/**
|
|
144
210
|
* Adds an event to EventCatalog
|
|
145
211
|
*
|
|
@@ -147,26 +213,26 @@ var src_default = (path) => {
|
|
|
147
213
|
* @param options - Optional options to write the event
|
|
148
214
|
*
|
|
149
215
|
*/
|
|
150
|
-
writeEvent: writeEvent(
|
|
216
|
+
writeEvent: writeEvent(join4(path, "events")),
|
|
151
217
|
/**
|
|
152
218
|
* Remove an event to EventCatalog (modeled on the standard POSIX rm utility)
|
|
153
219
|
*
|
|
154
220
|
* @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`
|
|
155
221
|
*
|
|
156
222
|
*/
|
|
157
|
-
rmEvent: rmEvent(
|
|
223
|
+
rmEvent: rmEvent(join4(path, "events")),
|
|
158
224
|
/**
|
|
159
225
|
* Remove an event by an Event id
|
|
160
226
|
*
|
|
161
227
|
* @param id - The id of the event you want to remove
|
|
162
228
|
*
|
|
163
229
|
*/
|
|
164
|
-
rmEventById: rmEventById(
|
|
230
|
+
rmEventById: rmEventById(join4(path, "events")),
|
|
165
231
|
/**
|
|
166
232
|
* Moves a given event id to the version directory
|
|
167
233
|
* @param directory
|
|
168
234
|
*/
|
|
169
|
-
versionEvent: versionEvent(
|
|
235
|
+
versionEvent: versionEvent(join4(path, "events")),
|
|
170
236
|
/**
|
|
171
237
|
* Adds a file to the given event
|
|
172
238
|
* @param id - The id of the event to add the file to
|
|
@@ -174,7 +240,7 @@ var src_default = (path) => {
|
|
|
174
240
|
* @param version - Optional version of the event to add the file to
|
|
175
241
|
* @returns
|
|
176
242
|
*/
|
|
177
|
-
addFileToEvent: addFileToEvent(
|
|
243
|
+
addFileToEvent: addFileToEvent(join4(path, "events")),
|
|
178
244
|
/**
|
|
179
245
|
* Adds a schema to the given event
|
|
180
246
|
* @param id - The id of the event to add the schema to
|
|
@@ -182,7 +248,54 @@ var src_default = (path) => {
|
|
|
182
248
|
* @param version - Optional version of the event to add the schema to
|
|
183
249
|
* @returns
|
|
184
250
|
*/
|
|
185
|
-
addSchemaToEvent: addSchemaToEvent(
|
|
251
|
+
addSchemaToEvent: addSchemaToEvent(join4(path, "events")),
|
|
252
|
+
/**
|
|
253
|
+
* ================================
|
|
254
|
+
* SERVICES
|
|
255
|
+
* ================================
|
|
256
|
+
*/
|
|
257
|
+
/**
|
|
258
|
+
* Adds a service to EventCatalog
|
|
259
|
+
*
|
|
260
|
+
* @param service - The service to write
|
|
261
|
+
* @param options - Optional options to write the event
|
|
262
|
+
*
|
|
263
|
+
*/
|
|
264
|
+
writeService: writeService(join4(path, "services")),
|
|
265
|
+
/**
|
|
266
|
+
* Returns a service from EventCatalog
|
|
267
|
+
* @param id - The id of the service to retrieve
|
|
268
|
+
* @param version - Optional id of the version to get
|
|
269
|
+
* @returns
|
|
270
|
+
*/
|
|
271
|
+
getService: getService(join4(path, "services")),
|
|
272
|
+
/**
|
|
273
|
+
* Moves a given service id to the version directory
|
|
274
|
+
* @param directory
|
|
275
|
+
*/
|
|
276
|
+
versionService: versionService(join4(path, "services")),
|
|
277
|
+
/**
|
|
278
|
+
* Remove a service from EventCatalog (modeled on the standard POSIX rm utility)
|
|
279
|
+
*
|
|
280
|
+
* @param path - The path to your service, e.g. `/InventoryService`
|
|
281
|
+
*
|
|
282
|
+
*/
|
|
283
|
+
rmService: rmService(join4(path, "services")),
|
|
284
|
+
/**
|
|
285
|
+
* Remove an service by an service id
|
|
286
|
+
*
|
|
287
|
+
* @param id - The id of the service you want to remove
|
|
288
|
+
*
|
|
289
|
+
*/
|
|
290
|
+
rmServiceById: rmServiceById(join4(path, "services")),
|
|
291
|
+
/**
|
|
292
|
+
* Adds a file to the given service
|
|
293
|
+
* @param id - The id of the service to add the file to
|
|
294
|
+
* @param file - File contents to add including the content and the file name
|
|
295
|
+
* @param version - Optional version of the service to add the file to
|
|
296
|
+
* @returns
|
|
297
|
+
*/
|
|
298
|
+
addFileToService: addFileToService(join4(path, "services"))
|
|
186
299
|
};
|
|
187
300
|
};
|
|
188
301
|
export {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/events.ts","../src/internal/utils.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { rmEvent, rmEventById, writeEvent, versionEvent, getEvent, addFileToEvent, addSchemaToEvent } from './events';\n\n/**\n * Init the SDK for EventCatalog\n *\n * @param path - The path to the EventCatalog directory\n *\n */\nexport default (path: string) => {\n return {\n /**\n * Returns an events from EventCatalog\n * @param id - The id of the event to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getEvent: getEvent(join(path, 'events')),\n /**\n * Adds an event to EventCatalog\n *\n * @param event - The event to write\n * @param options - Optional options to write the event\n *\n */\n writeEvent: writeEvent(join(path, 'events')),\n /**\n * Remove an event to EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`\n *\n */\n rmEvent: rmEvent(join(path, 'events')),\n /**\n * Remove an event by an Event id\n *\n * @param id - The id of the event you want to remove\n *\n */\n rmEventById: rmEventById(join(path, 'events')),\n /**\n * Moves a given event id to the version directory\n * @param directory\n */\n versionEvent: versionEvent(join(path, 'events')),\n /**\n * Adds a file to the given event\n * @param id - The id of the event to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the event to add the file to\n * @returns\n */\n addFileToEvent: addFileToEvent(join(path, 'events')),\n /**\n * Adds a schema to the given event\n * @param id - The id of the event to add the schema to\n * @param schema - Schema contents to add including the content and the file name\n * @param version - Optional version of the event to add the schema to\n * @returns\n */\n addSchemaToEvent: addSchemaToEvent(join(path, 'events')),\n };\n};\n","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\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\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\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\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\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\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,SAAS,QAAAA,aAAY;;;ACArB,OAAO,YAAY;AACnB,OAAOC,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;AAAA,EAChE;AAGA,SAAO,aAAa,KAAK,CAAC,SAAS,KAAK,SAAS,aAAa,OAAO,EAAE,CAAC;AAC1E;AAEO,IAAM,WAAW,OAAO,YAAoB;AACjD,MAAI;AACF,UAAM,QAAQ,MAAM,KAAK,SAAS,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AAAA;AAAA,IAOL,UAAU,SAASE,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQvC,YAAY,WAAWA,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAO3C,SAAS,QAAQA,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOrC,aAAa,YAAYA,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,IAK7C,cAAc,aAAaA,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQ/C,gBAAgB,eAAeA,MAAK,MAAM,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQnD,kBAAkB,iBAAiBA,MAAK,MAAM,QAAQ,CAAC;AAAA,EACzD;AACF;","names":["join","fs","join","fs","join","file","join"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/events.ts","../src/internal/utils.ts","../src/services.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { rmEvent, rmEventById, writeEvent, versionEvent, getEvent, addFileToEvent, addSchemaToEvent } from './events';\nimport { writeService, getService, versionService, rmService, rmServiceById, addFileToService } from './services';\n\n/**\n * Init the SDK for EventCatalog\n *\n * @param path - The path to the EventCatalog directory\n *\n */\nexport default (path: string) => {\n return {\n /**\n * Returns an events from EventCatalog\n * @param id - The id of the event to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getEvent: getEvent(join(path, 'events')),\n /**\n * Adds an event to EventCatalog\n *\n * @param event - The event to write\n * @param options - Optional options to write the event\n *\n */\n writeEvent: writeEvent(join(path, 'events')),\n /**\n * Remove an event to EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`\n *\n */\n rmEvent: rmEvent(join(path, 'events')),\n /**\n * Remove an event by an Event id\n *\n * @param id - The id of the event you want to remove\n *\n */\n rmEventById: rmEventById(join(path, 'events')),\n /**\n * Moves a given event id to the version directory\n * @param directory\n */\n versionEvent: versionEvent(join(path, 'events')),\n /**\n * Adds a file to the given event\n * @param id - The id of the event to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the event to add the file to\n * @returns\n */\n addFileToEvent: addFileToEvent(join(path, 'events')),\n /**\n * Adds a schema to the given event\n * @param id - The id of the event to add the schema to\n * @param schema - Schema contents to add including the content and the file name\n * @param version - Optional version of the event to add the schema to\n * @returns\n */\n addSchemaToEvent: addSchemaToEvent(join(path, 'events')),\n\n /**\n * ================================\n * SERVICES\n * ================================\n */\n\n /**\n * Adds a service to EventCatalog\n *\n * @param service - The service to write\n * @param options - Optional options to write the event\n *\n */\n writeService: writeService(join(path, 'services')),\n /**\n * Returns a service from EventCatalog\n * @param id - The id of the service to retrieve\n * @param version - Optional id of the version to get\n * @returns\n */\n getService: getService(join(path, 'services')),\n /**\n * Moves a given service id to the version directory\n * @param directory\n */\n versionService: versionService(join(path, 'services')),\n /**\n * Remove a service from EventCatalog (modeled on the standard POSIX rm utility)\n *\n * @param path - The path to your service, e.g. `/InventoryService`\n *\n */\n rmService: rmService(join(path, 'services')),\n /**\n * Remove an service by an service id\n *\n * @param id - The id of the service you want to remove\n *\n */\n rmServiceById: rmServiceById(join(path, 'services')),\n /**\n * Adds a file to the given service\n * @param id - The id of the service to add the file to\n * @param file - File contents to add including the content and the file name\n * @param version - Optional version of the service to add the file to\n * @returns\n */\n addFileToService: addFileToService(join(path, 'services')),\n };\n};\n","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","import matter from 'gray-matter';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Service } from './types';\nimport fs from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\n\n/**\n * Returns a service from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the service\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getService } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * cont event = await getService('InventoryService');\n *\n * // Gets a version of the event\n * cont event = await getService('InventoryService', '0.0.1');\n * ```\n */\nexport const getService =\n (directory: string) =>\n async (id: string, version?: string): Promise<Service> => {\n const file = await findFileById(directory, id, version);\n\n if (!file) throw new Error(`No service 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 Service;\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 { writeService } = utils('/path/to/eventcatalog');\n *\n * // Write a service\n * // Event would be written to services/InventoryService\n * await writeService({\n * id: 'InventoryService',\n * name: 'Inventory Service',\n * version: '0.0.1',\n * summary: 'Service that handles the inventory',\n * markdown: '# Hello world',\n * });\n *\n * // Write an event to the catalog but override the path\n * // Event would be written to services/Inventory/InventoryService\n * await writeService({\n * id: 'InventoryService',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryService\"});\n * ```\n */\nexport const writeService =\n (directory: string) =>\n async (service: Service, options: { path: string } = { path: '' }) => {\n // Get the path\n const path = options.path || `/${service.id}`;\n const exists = await versionExists(directory, service.id, service.version);\n\n if (exists) {\n throw new Error(`Failed to write service as the version ${service.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = service;\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 * Version a service by it's id.\n *\n * Takes the latest service and moves it to a versioned directory.\n * All files with this service are also versioned. (e.g /services/InventoryService/openapi.yml)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionService } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryService service to a versioned directory\n * // the version within that service is used as the version number.\n * await versionService('InventoryService');\n *\n * ```\n */\nexport const versionService = (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 service found with id: ${id}`);\n }\n\n // Service 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 service 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 * Delete a service at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmService } = utils('/path/to/eventcatalog');\n *\n * // Removes the service at services/InventoryService\n * await rmService('/InventoryService');\n * ```\n */\nexport const rmService = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete a service by it's id.\n *\n * Optionally specify a version to delete a specific version of the service.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmServiceById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryService event\n * await rmServiceById('InventoryService');\n *\n * // deletes a specific version of the InventoryService event\n * await rmServiceById('InventoryService', '0.0.1');\n * ```\n */\nexport const rmServiceById = (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 service found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\n/**\n * Add a file to a service by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the service.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToService } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryService event\n * await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryService event\n * await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToService =\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"],"mappings":";AAAA,SAAS,QAAAA,aAAY;;;ACArB,OAAO,YAAY;AACnB,OAAOC,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,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAevD,cAAc,aAAaA,MAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOjD,YAAY,WAAWA,MAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,IAK7C,gBAAgB,eAAeA,MAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOrD,WAAW,UAAUA,MAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAO3C,eAAe,cAAcA,MAAK,MAAM,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQnD,kBAAkB,iBAAiBA,MAAK,MAAM,UAAU,CAAC;AAAA,EAC3D;AACF;","names":["join","fs","join","fs","join","file","matter","fs","dirname","join","matter","file","join"]}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Service } from './types.d.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a service from EventCatalog.
|
|
5
|
+
*
|
|
6
|
+
* You can optionally specify a version to get a specific version of the service
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import utils from '@eventcatalog/utils';
|
|
11
|
+
*
|
|
12
|
+
* const { getService } = utils('/path/to/eventcatalog');
|
|
13
|
+
*
|
|
14
|
+
* // Gets the latest version of the event
|
|
15
|
+
* cont event = await getService('InventoryService');
|
|
16
|
+
*
|
|
17
|
+
* // Gets a version of the event
|
|
18
|
+
* cont event = await getService('InventoryService', '0.0.1');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare const getService: (directory: string) => (id: string, version?: string) => Promise<Service>;
|
|
22
|
+
/**
|
|
23
|
+
* Write an event to EventCatalog.
|
|
24
|
+
*
|
|
25
|
+
* You can optionally overide the path of the event.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import utils from '@eventcatalog/utils';
|
|
30
|
+
*
|
|
31
|
+
* const { writeService } = utils('/path/to/eventcatalog');
|
|
32
|
+
*
|
|
33
|
+
* // Write a service
|
|
34
|
+
* // Event would be written to services/InventoryService
|
|
35
|
+
* await writeService({
|
|
36
|
+
* id: 'InventoryService',
|
|
37
|
+
* name: 'Inventory Service',
|
|
38
|
+
* version: '0.0.1',
|
|
39
|
+
* summary: 'Service that handles the inventory',
|
|
40
|
+
* markdown: '# Hello world',
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Write an event to the catalog but override the path
|
|
44
|
+
* // Event would be written to services/Inventory/InventoryService
|
|
45
|
+
* await writeService({
|
|
46
|
+
* id: 'InventoryService',
|
|
47
|
+
* name: 'Inventory Adjusted',
|
|
48
|
+
* version: '0.0.1',
|
|
49
|
+
* summary: 'This is a summary',
|
|
50
|
+
* markdown: '# Hello world',
|
|
51
|
+
* }, { path: "/Inventory/InventoryService"});
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare const writeService: (directory: string) => (service: Service, options?: {
|
|
55
|
+
path: string;
|
|
56
|
+
}) => Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Version a service by it's id.
|
|
59
|
+
*
|
|
60
|
+
* Takes the latest service and moves it to a versioned directory.
|
|
61
|
+
* All files with this service are also versioned. (e.g /services/InventoryService/openapi.yml)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import utils from '@eventcatalog/utils';
|
|
66
|
+
*
|
|
67
|
+
* const { versionService } = utils('/path/to/eventcatalog');
|
|
68
|
+
*
|
|
69
|
+
* // moves the latest InventoryService service to a versioned directory
|
|
70
|
+
* // the version within that service is used as the version number.
|
|
71
|
+
* await versionService('InventoryService');
|
|
72
|
+
*
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
declare const versionService: (directory: string) => (id: string) => Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Delete a service at it's given path.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import utils from '@eventcatalog/utils';
|
|
82
|
+
*
|
|
83
|
+
* const { rmService } = utils('/path/to/eventcatalog');
|
|
84
|
+
*
|
|
85
|
+
* // Removes the service at services/InventoryService
|
|
86
|
+
* await rmService('/InventoryService');
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare const rmService: (directory: string) => (path: string) => Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Delete a service by it's id.
|
|
92
|
+
*
|
|
93
|
+
* Optionally specify a version to delete a specific version of the service.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import utils from '@eventcatalog/utils';
|
|
98
|
+
*
|
|
99
|
+
* const { rmServiceById } = utils('/path/to/eventcatalog');
|
|
100
|
+
*
|
|
101
|
+
* // deletes the latest InventoryService event
|
|
102
|
+
* await rmServiceById('InventoryService');
|
|
103
|
+
*
|
|
104
|
+
* // deletes a specific version of the InventoryService event
|
|
105
|
+
* await rmServiceById('InventoryService', '0.0.1');
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare const rmServiceById: (directory: string) => (id: string, version?: string) => Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Add a file to a service by it's id.
|
|
111
|
+
*
|
|
112
|
+
* Optionally specify a version to add a file to a specific version of the service.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import utils from '@eventcatalog/utils';
|
|
117
|
+
*
|
|
118
|
+
* const { addFileToService } = utils('/path/to/eventcatalog');
|
|
119
|
+
*
|
|
120
|
+
* // adds a file to the latest InventoryService event
|
|
121
|
+
* await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' });
|
|
122
|
+
*
|
|
123
|
+
* // adds a file to a specific version of the InventoryService event
|
|
124
|
+
* await addFileToService('InventoryService', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');
|
|
125
|
+
*
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
declare const addFileToService: (directory: string) => (id: string, file: {
|
|
129
|
+
content: string;
|
|
130
|
+
fileName: string;
|
|
131
|
+
}, version?: string) => Promise<void>;
|
|
132
|
+
|
|
133
|
+
export { addFileToService, getService, rmService, rmServiceById, versionService, writeService };
|