/
index.js
174 lines (148 loc) · 4.25 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
import assert from "node:assert/strict";
import contentful from "contentful";
import graphviz from "graphviz";
import * as dotenv from "dotenv";
dotenv.config();
const {
SPACE_ID = "",
ENVIRONMENT = "",
CONTENT_DELIVERY_ACCESS_TOKEN = "",
} = process.env;
assert(SPACE_ID, "Contentful SPACE_ID is required");
assert(ENVIRONMENT, "Contentful ENVIRONMENT is required");
assert(
CONTENT_DELIVERY_ACCESS_TOKEN,
"Contentful CONTENT_DELIVERY_ACCESS_TOKEN is required"
);
function generateColor(index = 0) {
const colors = [
"#e6194B",
"#3cb44b",
"#ffe119",
"#4363d8",
"#f58231",
"#911eb4",
"#42d4f4",
"#f032e6",
"#bfef45",
"#fabebe",
"#469990",
"#e6beff",
"#9A6324",
"#fffac8",
"#800000",
"#aaffc3",
"#808000",
"#ffd8b1",
"#000075",
"#a9a9a9",
];
return colors[index % colors.length];
}
function extractLinks(contentType, items, links = [], visited = new Set()) {
if (visited.has(contentType.sys.id)) {
return links;
}
visited.add(contentType.sys.id);
const linkedContentTypes = contentType.fields
.flatMap((field) => {
if (field.type === "Link" && field.linkType === "Entry") {
return {
source: contentType.sys.id,
target: field.validations?.find((v) => v.linkContentType)
?.linkContentType[0],
fieldId: field.id,
fieldName: field.name,
};
} else if (field.type === "Array" && field.items.linkType === "Entry") {
return field.items.validations
?.find((v) => v.linkContentType)
.linkContentType.map((target) => ({
source: contentType.sys.id,
target,
fieldId: field.id,
fieldName: field.name,
}));
}
return [];
})
.filter((link) => link.target);
links.push(...linkedContentTypes);
linkedContentTypes.forEach((link) => {
const targetContentType = items.find((item) => item.sys.id === link.target);
if (!targetContentType) {
console.log(`Could not find content type ${link.target}`);
return;
}
extractLinks(targetContentType, items, links, visited);
});
return links;
}
async function fetchContentTypes() {
const client = contentful.createClient({
space: SPACE_ID,
environment: ENVIRONMENT,
accessToken: CONTENT_DELIVERY_ACCESS_TOKEN,
});
const contentTypes = await client.getContentTypes();
const items = contentTypes.items;
const links = items
.flatMap((contentType) => extractLinks(contentType, items))
.filter((item, index, self) => {
return (
index ===
self.findIndex((t) => JSON.stringify(t) === JSON.stringify(item))
);
});
return { contentTypes: items, links };
}
function createDiagram(data) {
const { contentTypes, links } = data;
// Create a new directed graph
const g = graphviz.digraph("G");
const fieldColors = new Map();
// Set graph attributes
g.set("rankdir", "LR");
g.set("splines", "polyline");
contentTypes.forEach((contentType) => {
g.addNode(contentType.sys.id, { label: contentType.name });
contentType.fields.forEach((field) => {
const fieldId = `${contentType.sys.id}.${field.id}`;
fieldColors.set(fieldId, generateColor(fieldColors.size));
});
});
// Add content type nodes
contentTypes.forEach((contentType) => {
const label = `${contentType.name}\n ${"-".repeat(
contentType.name.length
)} \n${contentType.fields
.map((field) => `${field.id} (${field.type})`)
.join("\n")}`;
const node = g.addNode(contentType.sys.id, { label: label, shape: "box" });
node.set("style", "filled");
node.set("fillcolor", "lightblue");
node.set("fontsize", 10);
});
// Add links between content types
links.forEach((link) => {
if (link.target) {
const color = fieldColors.get(`${link.source}.${link.fieldId}`);
const fieldLabel = link.fieldName;
g.addEdge(link.source, link.target, {
color,
fontsize: 8,
label: fieldLabel,
samehead: true,
});
}
});
// Generate and save the Graphviz DOT file
return g.to_dot();
}
async function main() {
const data = await fetchContentTypes();
const output = createDiagram(data);
console.log(output);
}
main();