-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
328 lines (277 loc) · 10.3 KB
/
index.ts
File metadata and controls
328 lines (277 loc) · 10.3 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const fs = require("fs");
const path = require("path");
const matter = require("gray-matter");
const { execSync } = require("child_process");
const repoRoot = path.resolve(__dirname, "modules");
const refArchRoot = path.resolve(__dirname, "reference-architectures");
const assetsDir = path.resolve(__dirname, "website/public/assets/logos");
const hubRef = getHubRef();
function getHubRef() {
try {
return execSync("git rev-parse HEAD")
.toString()
.trim();
} catch {
return "main";
}
}
function getGitHubRemoteUrl() {
try {
const remoteUrl = execSync("git config --get remote.origin.url")
.toString()
.trim()
.replace(/https?:\/\/.*?@github\.com\//, "https://github.com/");
return remoteUrl.replace(/\.git$/, "");
} catch (error) {
console.error("Error getting GitHub remote URL:", error.message);
return null;
}
}
function getBuildingBlockFolderUrl(filePath) {
const remoteUrl = getGitHubRemoteUrl();
if (!remoteUrl) return null;
const relativePath = filePath
.replace(path.resolve(__dirname, "modules"), "")
.replace(/\/README\.md$/, "");
return `${remoteUrl}/tree/${hubRef}/modules${relativePath}`;
}
function findReadmes(dir){
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((file) => {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
return file.name === ".github" ? [] : findReadmes(fullPath);
}
return file.name === "README.md" && dir.includes("buildingblock")
? [fullPath]
: [];
});
}
function findPlatforms(): Platform[] {
fs.mkdirSync(assetsDir, { recursive: true });
return fs.readdirSync(repoRoot, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && dirent.name !== ".github")
.map((dir) => {
const platformDir: string = path.join(repoRoot, dir.name);
const platformLogo = getPlatformLogoOrThrow(platformDir, dir.name);
const platformReadme = getPlatformReadmeOrThrow(platformDir);
const { name, description, category, benefits, content, official } = extractReadmeFrontMatter(platformReadme);
const terraformSnippet = getTerraformSnippet(platformDir);
const integrationSourceUrl = getPlatformIntegrationSourceUrl(platformDir);
return {
platformType: dir.name,
name,
description,
category,
benefits,
logo: platformLogo,
readme: content,
integrationSourceUrl,
terraformSnippet,
official
};
});
}
function getPlatformIntegrationSourceUrl(platformDir: string): string | null {
const remoteUrl = getGitHubRemoteUrl();
if (!remoteUrl) return null;
const tfFile = path.join(platformDir, "meshstack_integration.tf");
if (!fs.existsSync(tfFile)) return null;
const relativePath = platformDir
.replace(path.resolve(__dirname, "modules"), "")
.replace(/\\/g, "/");
return `${remoteUrl}/blob/${hubRef}/modules${relativePath}/meshstack_integration.tf`;
}
// Finds the logo, copies it to website assets and returns the path.
function getPlatformLogoOrThrow(platformDir: string, platformType: string): string {
const logoFile = fs.readdirSync(platformDir).find(f => f.endsWith('.png') || f.endsWith('.svg'));
if (logoFile) {
const sourcePath = path.join(platformDir, logoFile);
const destPath = path.join(assetsDir, `${platformType}${path.extname(logoFile)}`);
fs.copyFileSync(sourcePath, destPath);
return destPath.replace(path.resolve(__dirname, "website/public"), "").replace(/^\/+/g, "");
}
throw new Error(`Logo file not found for platform: ${platformType} in directory: ${platformDir}. Each platform should have a logo.`);
}
function getPlatformReadmeOrThrow(platformDir: string) {
try {
return fs.readFileSync(path.join(platformDir, "README.md"), "utf-8");
} catch {
throw new Error(`Platform README.md not found for ${platformDir}. Each platform should have a README.md file.`);
}
}
function extractReadmeFrontMatter(platformReadme: string): { name: string; description: string; category?: string; benefits?: string[]; content: string; official: boolean } {
const { data, content } = matter(platformReadme);
const name = data.name;
if (!name) {
throw new Error('Property "name" is missing in the front matter of the platform README.md. Each platform README.md should have a name defined in the front matter.');
}
const description = data.description;
if (!description) {
throw new Error('Property "description" is missing in the front matter of the platform README.md. Each platform README.md should have a description defined in the front matter.');
}
const category = data.category;
const benefits = data.benefits;
return {
name,
description,
content,
category,
benefits,
official: data.official === true
}
}
function getTerraformSnippet(platformDir: string): string | null {
const tfFile = path.join(platformDir, "meshstack_integration.tf");
if (!fs.existsSync(tfFile)) return null;
try {
const renderTool = path.resolve(__dirname, "tools/render-meshstack-integration-tf/render-meshstack-integration-tf");
return execSync(`${renderTool} ${tfFile}`, { encoding: "utf-8" });
} catch (error) {
console.error(`Error rendering terraform snippet for ${platformDir}:`, error.message);
return null;
}
}
function copyBuildingBlockLogoToAssets(buildingBlockDir) {
const assetsDir = path.resolve(
__dirname,
"website/public/assets/building-block-logos"
);
const logoFile = fs
.readdirSync(buildingBlockDir)
.find((file) => file.endsWith(".png"));
if (!logoFile) return null;
const { id } = getIdAndPlatform(buildingBlockDir);
const sourcePath = path.join(buildingBlockDir, logoFile);
const destinationPath = path.join(assetsDir, `${id}${path.extname(logoFile)}`);
fs.mkdirSync(assetsDir, { recursive: true });
fs.copyFileSync(sourcePath, destinationPath);
return destinationPath
.replace(path.resolve(__dirname, "website/public"), "")
.replace(/^\/+/g, "");
}
function parseReadme(filePath) {
const buildingBlockDir = path.dirname(filePath);
const content = fs.readFileSync(filePath, "utf-8");
const { data, content: body } = matter(content);
const { id, platform } = getIdAndPlatform(buildingBlockDir);
const extractSection = (regex) =>
body.match(regex)?.[1]?.trim() || null;
const buildingBlockUrl = getBuildingBlockFolderUrl(filePath);
const buildingBlockLogoPath = copyBuildingBlockLogoToAssets(buildingBlockDir);
const backplaneDir = path.join(buildingBlockDir, "../backplane");
const backplaneUrl =
fs.existsSync(backplaneDir) && fs.statSync(backplaneDir).isDirectory()
? getBuildingBlockFolderUrl(backplaneDir)
: null;
const terraformSnippetDir = path.join(buildingBlockDir, "..");
const terraformSnippet = getTerraformSnippet(terraformSnippetDir);
return {
id,
platformType: platform,
logo: buildingBlockLogoPath,
buildingBlockUrl,
backplaneUrl,
terraformSnippet,
...data,
howToUse: extractSection(/## How to Use([\s\S]*?)(##|$)/),
};
}
function getIdAndPlatform(filePath) {
const relativePath = filePath
.replace(process.cwd(), "")
.replace(/\\/g, "/");
const pathParts = relativePath.split(path.sep).filter(Boolean);
const id = pathParts.slice(1, pathParts.length - 1).join("-");
const platform = pathParts[1] || "unknown";
return { id, platform };
}
// --- Reference Architectures ---
interface ReferenceArchitectureBuildingBlock {
path: string;
role: string;
}
export interface ReferenceArchitecture {
id: string;
name: string;
description: string;
cloudProviders: string[];
buildingBlocks: ReferenceArchitectureBuildingBlock[];
body: string;
sourceUrl: string | null;
}
function findReferenceArchitectures(): ReferenceArchitecture[] {
if (!fs.existsSync(refArchRoot)) return [];
return fs.readdirSync(refArchRoot)
.filter((f: string) => f.endsWith(".md") && f !== "README.md")
.map((file: string) => {
const filePath = path.join(refArchRoot, file);
const raw = fs.readFileSync(filePath, "utf-8");
const { data, content: body } = matter(raw);
const id = file.replace(/\.md$/, "");
const remoteUrl = getGitHubRemoteUrl();
const sourceUrl = remoteUrl
? `${remoteUrl}/blob/${hubRef}/reference-architectures/${file}`
: null;
if (!data.name) {
throw new Error(`Reference architecture ${file} is missing "name" in front-matter.`);
}
if (!data.description) {
throw new Error(`Reference architecture ${file} is missing "description" in front-matter.`);
}
if (!data.buildingBlocks || !Array.isArray(data.buildingBlocks)) {
throw new Error(`Reference architecture ${file} is missing "buildingBlocks" list in front-matter.`);
}
return {
id,
name: data.name,
description: data.description,
cloudProviders: data.cloudProviders || [],
buildingBlocks: data.buildingBlocks,
body,
sourceUrl,
} as ReferenceArchitecture;
});
}
// Main execution
function main() {
const generatedDir = "website/src/generated";
fs.mkdirSync(generatedDir, { recursive: true });
const platforms = findPlatforms();
fs.writeFileSync(
`${generatedDir}/platform.json`,
JSON.stringify(platforms, null, 2)
);
console.log(
`✅ Successfully processed ${platforms.length} platforms. Output saved to ${generatedDir}/platform.json`
);
const readmeFiles = findReadmes(repoRoot);
const jsonData = readmeFiles.map(parseReadme);
fs.writeFileSync(
`${generatedDir}/templates.json`,
JSON.stringify({ templates: jsonData }, null, 2)
);
console.log(
`✅ Successfully processed ${readmeFiles.length} README.md files. Output saved to ${generatedDir}/templates.json`
);
const refArchs = findReferenceArchitectures();
fs.writeFileSync(
`${generatedDir}/reference-architectures.json`,
JSON.stringify({ referenceArchitectures: refArchs }, null, 2)
);
console.log(
`✅ Successfully processed ${refArchs.length} reference architectures. Output saved to ${generatedDir}/reference-architectures.json`
);
}
main();
export interface Platform {
platformType: string;
name: string;
description: string;
logo: string;
readme: string;
category?: string;
benefits?: string[];
integrationSourceUrl?: string | null;
terraformSnippet?: string;
official?: boolean;
}