Repository navigation
Expand file tree
/
Copy pathgenerate-readme.js
More file actions
237 lines (197 loc) · 6.91 KB
/
Copy pathgenerate-readme.js
File metadata and controls
237 lines (197 loc) · 6.91 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const ROOT = __dirname;
const TOOLS_DIR = path.join(ROOT, "tools", "operate");
const README_PATH = path.join(ROOT, "README.md");
const ICON_ASSETS = {
website: "assets/icons/website.svg",
oss: "assets/icons/oss.svg",
github: "assets/icons/github.svg",
linkedin: "assets/icons/linkedin.svg",
x: "assets/icons/x.svg",
producthunt: "assets/icons/producthunt.svg",
};
const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
const TAG_ORDER = [
"Incident Response",
"Observability",
"AIOps",
"IDP",
"IaC",
"FinOps",
"Security",
"Deployment",
"Other",
];
function escapeMarkdown(value) {
return String(value).replace(/\|/g, "\\|");
}
function isValidDate(value) {
if (typeof value !== "string" || !ISO_DATE_RE.test(value)) {
return false;
}
return !Number.isNaN(new Date(`${value}T00:00:00Z`).getTime());
}
function normalizeDeployment(value) {
if (Array.isArray(value)) {
return value.map((item) => String(item));
}
if (value) {
return [String(value)];
}
return [];
}
function getYamlFiles(dir) {
if (!fs.existsSync(dir)) return [];
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.isFile())
.map((entry) => entry.name)
.filter((name) => !name.startsWith("_") && (name.endsWith(".yaml") || name.endsWith(".yml")))
.sort((a, b) => a.localeCompare(b, "en", { sensitivity: "base" }))
.map((name) => path.join(dir, name));
}
function loadTool(filePath) {
const raw = fs.readFileSync(filePath, "utf8");
const tool = yaml.load(raw);
if (!tool || typeof tool !== "object" || Array.isArray(tool)) {
throw new Error(`Invalid YAML object in ${filePath}`);
}
tool.deployment = normalizeDeployment(tool.deployment);
tool.tags = Array.isArray(tool.tags) ? tool.tags.map((tag) => String(tag)) : [];
return tool;
}
function deploymentForReadme(deployments) {
if (!Array.isArray(deployments) || deployments.length === 0) return "-";
const unique = Array.from(new Set(deployments.map((item) => String(item).trim())));
if (unique.length > 1) return "Multi";
const value = unique[0].toLowerCase();
if (value === "saas") return "SaaS";
if (value === "on-prem") return "On-Prem";
if (value === "hybrid") return "Hybrid";
return unique[0];
}
function buildIconLinks(tool) {
const icon = (alt, src, url) => `[<img alt="${alt}" src="${src}" width="16" />](${url})`;
const hasIcon = (key) => fs.existsSync(path.join(ROOT, ICON_ASSETS[key]));
const renderIconLink = (key, alt, url) => {
if (!url || !hasIcon(key)) return null;
return icon(alt, ICON_ASSETS[key], url);
};
const ordered = [
renderIconLink("website", "Website", tool.url),
renderIconLink("github", "GitHub", tool.github),
renderIconLink("linkedin", "LinkedIn", tool.linkedin),
renderIconLink("x", "X", tool.x),
renderIconLink("producthunt", "Product Hunt", tool.producthunt),
];
return ordered.filter(Boolean).join(" ") || "-";
}
function buildNameCell(tool) {
const nameLink = `[${escapeMarkdown(tool.name)}](${tool.url})`;
return tool.opensource ? `:green_heart:${nameLink}` : nameLink;
}
function parseAddedDate(value) {
if (!isValidDate(value)) {
return null;
}
return new Date(`${value}T00:00:00Z`);
}
function daysSince(date, now = new Date()) {
const utcNow = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
const utcDate = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
return Math.floor((utcNow - utcDate) / 86400000);
}
function selectRecentTools(tools, now = new Date()) {
const datedTools = tools
.filter((tool) => parseAddedDate(tool.dateAdded))
.sort((a, b) => b.dateAdded.localeCompare(a.dateAdded) || a.name.localeCompare(b.name, "en", { sensitivity: "base" }));
const withinDays = (dayLimit) =>
datedTools.filter((tool) => {
const parsed = parseAddedDate(tool.dateAdded);
if (!parsed) return false;
const age = daysSince(parsed, now);
return age >= 0 && age <= dayLimit;
});
const lastWeek = withinDays(7);
const expanded = lastWeek.length >= 1 && lastWeek.length <= 2 ? withinDays(14) : lastWeek;
return {
tools: expanded.slice(0, 5),
rangeDays: lastWeek.length >= 1 && lastWeek.length <= 2 ? 14 : 7,
};
}
function anchorForTag(tag) {
return tag.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");
}
function buildReadme(tools) {
const groups = new Map(TAG_ORDER.map((tag) => [tag, []]));
for (const tool of tools) {
const primaryTag = tool.tags[0] || "Other";
const bucket = groups.get(primaryTag) || groups.get("Other");
bucket.push(tool);
}
const jumpLinks = TAG_ORDER
.filter((tag) => groups.get(tag).length > 0)
.map((tag) => `[${tag}](#${anchorForTag(tag)})`)
.join(" | ");
const lines = [
"",
"",
"If this repository is useful, please consider starring :star: it.",
"",
"## Tools",
"",
"Items with :green_heart: indicate open source projects.",
"",
"> AUTO-GENERATED FILE - DO NOT EDIT MANUALLY.",
"> Auto-generated by CI workflow or pre-commit hooks using `node generate-readme.js`.",
"",
];
if (tools.length === 0) {
lines.push("| Name | Summary | Deployment | Links |");
lines.push("| --- | --- | --- | --- |");
lines.push("| _No tools found_ | Add YAML files under `tools/operate/`. | - | - |");
return `${lines.join("\n")}\n`;
}
const recent = selectRecentTools(tools);
if (recent.tools.length > 0) {
lines.push(`### Recent Additions (Last ${recent.rangeDays} Days)`);
lines.push("");
for (const tool of recent.tools) {
lines.push(`- ${tool.dateAdded} - ${buildNameCell(tool)} (${tool.tags[0] || "Other"})`);
}
lines.push("");
}
lines.push(`Jump to: ${jumpLinks}`);
for (const tag of TAG_ORDER) {
const sectionTools = groups.get(tag);
if (!sectionTools || sectionTools.length === 0) continue;
lines.push("");
lines.push(`<a id="${anchorForTag(tag)}"></a>`);
lines.push(`### ${tag} (${sectionTools.length})`);
lines.push("");
lines.push("| Name | Summary | Deployment | Links |");
lines.push("| --- | --- | --- | --- |");
for (const tool of sectionTools) {
lines.push(
`| ${buildNameCell(tool)} | ${escapeMarkdown(tool.summary)} | ${escapeMarkdown(
deploymentForReadme(tool.deployment)
)} | ${buildIconLinks(tool)} |`
);
}
lines.push("");
lines.push('<p align="left"><a href="#tools">Back to top ↑</a></p>');
}
return `${lines.join("\n")}\n`;
}
function main() {
const tools = getYamlFiles(TOOLS_DIR)
.map(loadTool)
.sort((a, b) => a.name.localeCompare(b.name, "en", { sensitivity: "base" }));
const output = buildReadme(tools);
fs.writeFileSync(README_PATH, output, "utf8");
console.log(`README.md generated with ${tools.length} tool(s).`);
}
main();