forked from event-catalog/eventcatalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventcatalog-config-file-utils.js
89 lines (74 loc) · 3.04 KB
/
eventcatalog-config-file-utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { readFile, writeFile, rm } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { copyFile } from 'node:fs/promises';
import path from 'node:path';
import { v4 as uuidV4 } from 'uuid';
import { pathToFileURL } from 'url';
import matter from 'gray-matter';
export async function cleanup(projectDirectory) {
const filePath = path.join(projectDirectory, 'eventcatalog.config.mjs');
if (existsSync(filePath)) {
await rm(filePath);
}
}
export const getEventCatalogConfigFile = async (projectDirectory) => {
try {
let configFilePath = path.join(projectDirectory, 'eventcatalog.config.js');
const filePath = path.join(projectDirectory, 'package.json');
const packageJson = JSON.parse(await readFile(filePath, 'utf-8'));
if (packageJson?.type !== 'module') {
await copyFile(configFilePath, path.join(projectDirectory, 'eventcatalog.config.mjs'));
configFilePath = path.join(projectDirectory, 'eventcatalog.config.mjs');
}
const configFileURL = pathToFileURL(configFilePath).href;
const config = await import(/* @vite-ignore */ configFileURL);
return config.default;
} finally {
await cleanup(projectDirectory);
}
};
export const writeEventCatalogConfigFile = async (projectDirectory, newConfig) => {
try {
const configFilePath = path.join(projectDirectory, 'eventcatalog.config.js');
let content = await readFile(configFilePath, 'utf8');
// Find the start of the config object
const startIndex = content.indexOf('export default {');
if (startIndex === -1) {
// Just fail silently if the config object is not found
return;
}
// Update or add each new config item
Object.entries(newConfig).forEach(([key, value]) => {
const valueString = JSON.stringify(value, null, 2).replace(/"/g, "'").replace(/\n/g, '\n ');
// Check if the key already exists
const keyRegex = new RegExp(`(${key}\\s*:)([^,}]+)`, 'g');
if (content.match(keyRegex)) {
// Update existing key
content = content.replace(keyRegex, `$1 ${valueString}`);
} else {
// Add new key-value pair
const insertPosition = content.indexOf('{', startIndex) + 1;
content = content.slice(0, insertPosition) + `\n ${key}: ${valueString},` + content.slice(insertPosition);
}
});
// Write the updated content back to the file
await writeFile(configFilePath, content);
} finally {
await cleanup(projectDirectory);
}
};
// Check the eventcatalog.config.js and add any missing required fields on it
export const verifyRequiredFieldsAreInCatalogConfigFile = async (projectDirectory) => {
try {
const config = await getEventCatalogConfigFile(projectDirectory);
if (!config.cId) {
await writeEventCatalogConfigFile(projectDirectory, { cId: uuidV4() });
}
} catch (error) {
// fail silently, it's overly important
}
};
export function addPropertyToFrontMatter(input, newProperty, newValue) {
const file = matter(input);
return matter.stringify(file.content, { ...file.data, [newProperty]: newValue });
}