@eventcatalog/core 2.5.2 → 2.5.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @eventcatalog/core
2
2
 
3
+ ## 2.5.3
4
+
5
+ ### Patch Changes
6
+
7
+ - a7f1bd0: fix(core): fixed generate script for plugins
8
+
3
9
  ## 2.5.2
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -5,7 +5,8 @@
5
5
  [![MIT License][license-badge]][license]
6
6
  [![PRs Welcome][prs-badge]][prs]
7
7
  <img src="https://img.shields.io/github/actions/workflow/status/event-catalog/eventcatalog/verify-build.yml"/>
8
- [![Star on GitHub][github-star-badge]][github-star]
8
+ [![](https://dcbadge.limes.pink/api/server/https://discord.gg/3rjaZMmrAm?style=flat)](https://discord.gg/3rjaZMmrAm) [<img src="https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white" height="20px" />](https://www.linkedin.com/in/david-boyne/) [![blog](https://img.shields.io/badge/blog-EDA--Visuals-brightgreen)](https://eda-visuals.boyney.io/?utm_source=event-catalog-gihub)
9
+
9
10
 
10
11
 
11
12
 
@@ -233,7 +234,6 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
233
234
 
234
235
  This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
235
236
 
236
-
237
237
  # License
238
238
 
239
239
  MIT.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eventcatalog/core",
3
3
  "type": "module",
4
- "version": "2.5.2",
4
+ "version": "2.5.3",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -1,53 +1,109 @@
1
- import { readFile, writeFile } from 'node:fs/promises';
2
- import { createRequire } from 'module';
1
+ import { readFile, writeFile, rm } from 'node:fs/promises';
3
2
  import path from 'node:path';
4
3
 
5
- const generate = async () => {
6
- // Fix for the file
7
- const rawFile = await readFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), 'utf8');
4
+ /**
5
+ * Very strange behaviour when importing ESM files from catalogs into core.
6
+ * Core (node) does not know how to handle ESM files, so we have to try and convert them.
7
+ *
8
+ * This needs sorting out! Sorry if you are reading this, but it unblocked me for now!
9
+ * @param {*} content
10
+ * @returns
11
+ */
12
+ function convertESMtoCJS(content) {
13
+ // Replace import statements with require
14
+ content = content.replace(/import\s+([a-zA-Z0-9{},\s*]+)\s+from\s+['"]([^'"]+)['"];/g, (match, imports, modulePath) => {
15
+ return `const ${imports.trim()} = require('${modulePath}');`;
16
+ });
8
17
 
9
- const require = createRequire(process.env.PROJECT_DIR);
18
+ // Replace export default with module.exports
19
+ content = content.replace(/export\s+default\s+/g, 'module.exports = ');
10
20
 
11
- // Convert export default to module.exports (Needed for dynamic require)
12
- if (rawFile.includes('export default')) {
13
- const fixedFile = rawFile.replace('export default', 'module.exports =');
14
- await writeFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), fixedFile);
15
- }
21
+ // Replace named exports with module.exports
22
+ content = content.replace(/export\s+{([^}]+)}/g, (match, exports) => {
23
+ return `module.exports = {${exports.trim()}};`;
24
+ });
25
+
26
+ // Remove declarations of __filename and __dirname
27
+ content = content.replace(/^\s*(const|let|var)\s+__(filename|dirname)\s+=\s+.*;?\s*$/gm, '');
16
28
 
17
- const config = require(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'));
29
+ return content;
30
+ }
18
31
 
19
- const { generators = [] } = config;
32
+ // TODO: Do we actually need this? Can we clean this up
33
+ function getDefaultExport(importedModule) {
34
+ if (importedModule === null || typeof importedModule !== 'object') {
35
+ throw new Error('Invalid module');
36
+ }
20
37
 
21
- if (!generators.length) {
22
- console.log('No configured generators found, skipping generation');
23
- return;
38
+ if (typeof importedModule.default === 'object' && importedModule.default !== null) {
39
+ return importedModule.default.default || importedModule.default;
24
40
  }
25
41
 
26
- console.log('Running generators...');
42
+ if (typeof importedModule.default !== 'undefined') {
43
+ return importedModule.default;
44
+ }
27
45
 
28
- // Tidy up
29
- await writeFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), rawFile);
46
+ return importedModule;
47
+ }
30
48
 
31
- const plugins = generators.map((generator) => {
32
- let plugin = generator[0];
33
- const pluginConfig = generator[1];
49
+ async function cleanup() {
50
+ await rm(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.cjs'));
51
+ }
34
52
 
35
- if (plugin.startsWith('./')) {
36
- plugin = path.join(process.env.PROJECT_DIR, plugin);
37
- }
53
+ const generate = async () => {
54
+ try {
55
+ // Fix for the file
56
+ const rawFile = await readFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), 'utf8');
57
+
58
+ // Have to conver the ESM to CJS...
59
+ const configAsCommonJS = convertESMtoCJS(rawFile);
60
+ await writeFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.cjs'), configAsCommonJS);
38
61
 
39
- if (plugin.includes('<rootDir>')) {
40
- plugin = plugin.replace('<rootDir>', process.env.PROJECT_DIR);
62
+ const configAsCJS = await import(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.cjs'));
63
+ const config = configAsCJS.default;
64
+
65
+ const { generators = [] } = config;
66
+
67
+ if (!generators.length) {
68
+ console.log('No configured generators found, skipping generation');
69
+ return;
41
70
  }
42
71
 
43
- const importedGenerator = require(plugin);
72
+ // Tidy up
73
+ await writeFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), rawFile);
44
74
 
45
- console.log(`Generating EventCatalog docs using: ${plugin}`);
75
+ for (const generator of generators) {
76
+ let plugin = generator[0];
77
+ const pluginConfig = generator[1];
46
78
 
47
- return importedGenerator({ eventCatalogConfig: config }, pluginConfig);
48
- });
79
+ if (plugin.startsWith('./')) {
80
+ plugin = path.join(process.env.PROJECT_DIR, plugin);
81
+ }
82
+
83
+ if (plugin.includes('<rootDir>')) {
84
+ plugin = plugin.replace('<rootDir>', process.env.PROJECT_DIR);
85
+ }
86
+
87
+ try {
88
+ const importedGenerator = await import(plugin);
49
89
 
50
- await Promise.all(plugins);
90
+ // TODO: Fix this...
91
+ const generator = getDefaultExport(importedGenerator);
92
+
93
+ await generator({ eventCatalogConfig: {} }, pluginConfig);
94
+
95
+ // Use importedGenerator here
96
+ } catch (error) {
97
+ console.error('Error loading plugin:', error);
98
+ await cleanup();
99
+ return;
100
+ }
101
+ }
102
+ } catch (error) {
103
+ // Failed to generate clean up...
104
+ console.error(error);
105
+ await cleanup();
106
+ }
51
107
  };
52
108
 
53
109
  generate();