@eventcatalog/core 2.7.6 → 2.7.8
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 +12 -0
- package/package.json +1 -1
- package/scripts/eventcatalog-config-file-utils.js +96 -0
- package/scripts/generate.js +5 -47
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @eventcatalog/core
|
|
2
2
|
|
|
3
|
+
## 2.7.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c67052f: fix(core): fixed generate command
|
|
8
|
+
|
|
9
|
+
## 2.7.7
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- f699aed: feat(core): added utils to read and write catalog files
|
|
14
|
+
|
|
3
15
|
## 2.7.6
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { readFile, writeFile, rm } from 'node:fs/promises';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import path from 'node:path';
|
|
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
|
+
});
|
|
17
|
+
|
|
18
|
+
// Replace export default with module.exports
|
|
19
|
+
content = content.replace(/export\s+default\s+/g, 'module.exports = ');
|
|
20
|
+
|
|
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, '');
|
|
28
|
+
|
|
29
|
+
return content;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function cleanup(projectDirectory) {
|
|
33
|
+
const filePath = path.join(projectDirectory, 'eventcatalog.config.cjs');
|
|
34
|
+
if (existsSync(filePath)) {
|
|
35
|
+
await rm(filePath);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const getEventCatalogConfigFile = async (projectDirectory) => {
|
|
40
|
+
try {
|
|
41
|
+
const rawFile = await readFile(path.join(projectDirectory, 'eventcatalog.config.js'), 'utf8');
|
|
42
|
+
|
|
43
|
+
// Have to conver the ESM to CJS...
|
|
44
|
+
const configAsCommonJS = convertESMtoCJS(rawFile);
|
|
45
|
+
|
|
46
|
+
await writeFile(path.join(projectDirectory, 'eventcatalog.config.cjs'), configAsCommonJS);
|
|
47
|
+
|
|
48
|
+
const configAsCJS = await import(/* @vite-ignore */ path.join(projectDirectory, 'eventcatalog.config.cjs'));
|
|
49
|
+
|
|
50
|
+
// Clean up?
|
|
51
|
+
await writeFile(path.join(projectDirectory, 'eventcatalog.config.js'), rawFile);
|
|
52
|
+
|
|
53
|
+
await cleanup(projectDirectory);
|
|
54
|
+
|
|
55
|
+
return configAsCJS.default;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
await cleanup(projectDirectory);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const writeEventCatalogConfigFile = async (projectDirectory, newConfig) => {
|
|
62
|
+
try {
|
|
63
|
+
const configFilePath = path.join(projectDirectory, 'eventcatalog.config.js');
|
|
64
|
+
let content = await readFile(configFilePath, 'utf8');
|
|
65
|
+
|
|
66
|
+
// Find the start of the config object
|
|
67
|
+
const startIndex = content.indexOf('export default {');
|
|
68
|
+
if (startIndex === -1) {
|
|
69
|
+
// Just fail silently if the config object is not found
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Update or add each new config item
|
|
74
|
+
Object.entries(newConfig).forEach(([key, value]) => {
|
|
75
|
+
const valueString = JSON.stringify(value, null, 2).replace(/"/g, "'").replace(/\n/g, '\n ');
|
|
76
|
+
|
|
77
|
+
// Check if the key already exists
|
|
78
|
+
const keyRegex = new RegExp(`(${key}\\s*:)([^,}]+)`, 'g');
|
|
79
|
+
if (content.match(keyRegex)) {
|
|
80
|
+
// Update existing key
|
|
81
|
+
content = content.replace(keyRegex, `$1 ${valueString}`);
|
|
82
|
+
} else {
|
|
83
|
+
// Add new key-value pair
|
|
84
|
+
const insertPosition = content.indexOf('{', startIndex) + 1;
|
|
85
|
+
content = content.slice(0, insertPosition) + `\n ${key}: ${valueString},` + content.slice(insertPosition);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Write the updated content back to the file
|
|
90
|
+
await writeFile(configFilePath, content);
|
|
91
|
+
|
|
92
|
+
await cleanup(projectDirectory);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
await cleanup(projectDirectory);
|
|
95
|
+
}
|
|
96
|
+
};
|
package/scripts/generate.js
CHANGED
|
@@ -1,35 +1,6 @@
|
|
|
1
|
-
import { readFile, writeFile, rm } from 'node:fs/promises';
|
|
2
1
|
import path from 'node:path';
|
|
2
|
+
import { getEventCatalogConfigFile, cleanup } from './eventcatalog-config-file-utils.js';
|
|
3
3
|
|
|
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
|
-
});
|
|
17
|
-
|
|
18
|
-
// Replace export default with module.exports
|
|
19
|
-
content = content.replace(/export\s+default\s+/g, 'module.exports = ');
|
|
20
|
-
|
|
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, '');
|
|
28
|
-
|
|
29
|
-
return content;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// TODO: Do we actually need this? Can we clean this up
|
|
33
4
|
function getDefaultExport(importedModule) {
|
|
34
5
|
if (importedModule === null || typeof importedModule !== 'object') {
|
|
35
6
|
throw new Error('Invalid module');
|
|
@@ -46,21 +17,11 @@ function getDefaultExport(importedModule) {
|
|
|
46
17
|
return importedModule;
|
|
47
18
|
}
|
|
48
19
|
|
|
49
|
-
async function cleanup() {
|
|
50
|
-
await rm(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.cjs'));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
20
|
const generate = async () => {
|
|
54
21
|
try {
|
|
55
|
-
|
|
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);
|
|
22
|
+
const PROJECT_DIRECTORY = process.env.PROJECT_DIR;
|
|
61
23
|
|
|
62
|
-
const
|
|
63
|
-
const config = configAsCJS.default;
|
|
24
|
+
const config = await getEventCatalogConfigFile(PROJECT_DIRECTORY);
|
|
64
25
|
|
|
65
26
|
const { generators = [] } = config;
|
|
66
27
|
|
|
@@ -69,19 +30,16 @@ const generate = async () => {
|
|
|
69
30
|
return;
|
|
70
31
|
}
|
|
71
32
|
|
|
72
|
-
// Tidy up
|
|
73
|
-
await writeFile(path.join(process.env.PROJECT_DIR, 'eventcatalog.config.js'), rawFile);
|
|
74
|
-
|
|
75
33
|
for (const generator of generators) {
|
|
76
34
|
let plugin = generator[0];
|
|
77
35
|
const pluginConfig = generator[1];
|
|
78
36
|
|
|
79
37
|
if (plugin.startsWith('./')) {
|
|
80
|
-
plugin = path.join(
|
|
38
|
+
plugin = path.join(PROJECT_DIRECTORY, plugin);
|
|
81
39
|
}
|
|
82
40
|
|
|
83
41
|
if (plugin.includes('<rootDir>')) {
|
|
84
|
-
plugin = plugin.replace('<rootDir>',
|
|
42
|
+
plugin = plugin.replace('<rootDir>', PROJECT_DIRECTORY);
|
|
85
43
|
}
|
|
86
44
|
|
|
87
45
|
try {
|