Skip to content

Commit 92eb393

Browse files
committed
Refactor: Sort versions using semver comparison
1 parent 36d0233 commit 92eb393

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

.github/scripts/update_index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const stylesPath = join(webDir, "styles.css");
1313
const indexDest = join(buildDir, "index.html");
1414
const stylesDest = join(buildDir, "styles.css");
1515

16+
const sortSemVer = (a, b) => {
17+
const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
18+
const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
19+
if (aMajor !== bMajor) return bMajor - aMajor;
20+
if (aMinor !== bMinor) return bMinor - aMinor;
21+
return bPatch - aPatch;
22+
};
23+
1624
readFile(templatePath, "utf8", (err, templateData) => {
1725
if (err) {
1826
console.error("Error reading template file:", err);
@@ -29,13 +37,7 @@ readFile(templatePath, "utf8", (err, templateData) => {
2937
const versionDirs = files
3038
.filter((dir) => dir.isDirectory() && /^\d+\.\d+\.\d+$/.test(dir.name))
3139
.map((dir) => dir.name)
32-
.sort((a, b) => {
33-
const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
34-
const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
35-
if (aMajor !== bMajor) return aMajor - bMajor;
36-
if (aMinor !== bMinor) return aMinor - bMinor;
37-
return bPatch - aPatch;
38-
});
40+
.sort(sortSemVer);
3941

4042
// Generate HTML list items for each version
4143
const listItems = versionDirs
@@ -58,7 +60,7 @@ readFile(templatePath, "utf8", (err, templateData) => {
5860
// Replace the placeholder with the generated list items
5961
const updatedContent = templateData.replace(
6062
"<!-- VERSIONS_PLACEHOLDER -->",
61-
listItems
63+
listItems,
6264
);
6365

6466
// Write the updated content to index.html
@@ -68,7 +70,7 @@ readFile(templatePath, "utf8", (err, templateData) => {
6870
process.exit(1);
6971
}
7072
console.log(
71-
"build/index.html has been successfully updated with available versions."
73+
"build/index.html has been successfully updated with available versions.",
7274
);
7375
});
7476
});
@@ -86,7 +88,7 @@ readFile(stylesPath, "utf8", (err, content) => {
8688
process.exit(1);
8789
}
8890
console.log(
89-
"build/styles.css has been successfully updated at" + stylesDest
91+
"build/styles.css has been successfully updated at" + stylesDest,
9092
);
9193
});
9294
});

0 commit comments

Comments
 (0)