forked from event-catalog/eventcatalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
65 lines (50 loc) · 1.74 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import path from 'node:path';
import { getEventCatalogConfigFile, cleanup } from './eventcatalog-config-file-utils.js';
function getDefaultExport(importedModule) {
if (importedModule === null || typeof importedModule !== 'object') {
throw new Error('Invalid module');
}
if (typeof importedModule.default === 'object' && importedModule.default !== null) {
return importedModule.default.default || importedModule.default;
}
if (typeof importedModule.default !== 'undefined') {
return importedModule.default;
}
return importedModule;
}
export const generate = async (PROJECT_DIRECTORY) => {
try {
const config = await getEventCatalogConfigFile(PROJECT_DIRECTORY);
const { generators = [] } = config;
if (!generators.length) {
console.log('No configured generators found, skipping generation');
return;
}
for (const generator of generators) {
let plugin = generator[0];
const pluginConfig = generator[1];
if (plugin.startsWith('./')) {
plugin = path.join(PROJECT_DIRECTORY, plugin);
}
if (plugin.includes('<rootDir>')) {
plugin = plugin.replace('<rootDir>', PROJECT_DIRECTORY);
}
try {
const importedGenerator = await import(plugin);
// TODO: Fix this...
const generator = getDefaultExport(importedGenerator);
await generator({ eventCatalogConfig: {} }, pluginConfig);
// Use importedGenerator here
} catch (error) {
console.error('Error loading plugin:', error);
await cleanup(PROJECT_DIRECTORY);
return;
}
}
await cleanup(PROJECT_DIRECTORY);
} catch (error) {
// Failed to generate clean up...
console.error(error);
await cleanup(PROJECT_DIRECTORY);
}
};