-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (88 loc) · 2.79 KB
/
Copy pathindex.js
File metadata and controls
107 lines (88 loc) · 2.79 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
import fs from "node:fs";
import path from "node:path";
import { markdownTable } from "markdown-table";
const verboseRuntimes = {
cpp: "C++",
dart: "Dart",
deno: "Deno",
dotnet: ".NET",
java: "Java",
kotlin: "Kotlin",
node: "Node.js",
php: "PHP",
python: "Python",
ruby: "Ruby",
swift: "Swift",
};
const folderDenylist = [".github", ".git"];
const generateUniqueTemplates = (runtimes) => {
let templates = [];
for (const runtime of runtimes) {
const folders = fs
.readdirSync(path.join(".", `../../../${runtime}`), {
withFileTypes: true,
})
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
templates.push(...folders);
}
return [...new Set(templates)];
};
const generateTableRows = (templates, runtimes) => {
return templates.map((template) => {
const languagesSupport = runtimes.map((runtime) => {
return fs.existsSync(path.join(".", `../../../${runtime}/${template}`))
? `[✅](/${runtime}/${template})`
: "🏗️";
});
return [template, ...languagesSupport];
});
};
const sortRuntimesBySupport = (runtimes, uniqueTemplates) => {
return runtimes.sort((a, b) => {
const aTemplates = uniqueTemplates.filter((template) =>
fs.existsSync(path.join(".", `../../../${a}/${template}`))
);
const bTemplates = uniqueTemplates.filter((template) =>
fs.existsSync(path.join(".", `../../../${b}/${template}`))
);
return bTemplates.length - aTemplates.length;
});
};
const updateReadmeFile = (readmePath, table) => {
const readme = fs.readFileSync(readmePath).toString();
if (
readme.includes("<!-- TABLE:START -->") &&
readme.includes("<!-- TABLE:END -->")
) {
const newReadme = `${
readme.split("<!-- TABLE:START -->")[0]
}<!-- TABLE:START -->\n${table}\n<!-- TABLE:END -->${
readme.split("<!-- TABLE:END -->")[1]
}`;
fs.writeFileSync(readmePath, newReadme);
}
};
let runtimes = fs
.readdirSync(path.join(".", "../../../"), { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.filter((folder) => !folderDenylist.includes(folder))
.sort();
const uniqueTemplates = generateUniqueTemplates(runtimes);
runtimes = sortRuntimesBySupport(runtimes, uniqueTemplates);
const tableRows = generateTableRows(uniqueTemplates, runtimes);
const sortedTableRows = tableRows.sort((a, b) => {
const aCount = a.filter((column) => column !== "").length;
const bCount = b.filter((column) => column !== "").length;
return aCount > bCount ? -1 : 1;
});
const table = markdownTable([
[
"Template",
...runtimes.map((r) => (verboseRuntimes[r] ? verboseRuntimes[r] : r)),
],
...sortedTableRows,
]);
const readmePath = path.join(".", "../../../README.md");
updateReadmeFile(readmePath, table);