This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_symlinks.js
74 lines (63 loc) · 2 KB
/
update_symlinks.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
const fs = require("fs");
const path = require("path");
const semver = require("semver");
const { cmpKeys, compare } = require("./cmpVer");
const docRoot = path.join(__dirname, "../../docs");
process.chdir(docRoot);
console.log(`Moved to ${docRoot}`);
const versions = {};
function updateVersion(key, nv) {
const old = versions[key] || "0.0.0";
if (compare(old, nv) < 0) {
versions[key] = nv;
}
}
function isSL(pth) {
return fs.existsSync(pth) && fs.lstatSync(pth).isSymbolicLink();
}
function isDir(pth) {
return fs.existsSync(pth) && fs.lstatSync(pth).isDirectory();
}
for (const name of fs.readdirSync(docRoot)) {
console.log(`Checking file ${docRoot}/${name}`);
if (isSL(name)) continue;
if (!isDir(name)) continue;
versions[name] = name;
if (!semver.valid(name)) continue;
const { major, minor, patch, prerelease } = semver.parse(name);
updateVersion("latest", name);
if (prerelease.length > 0) {
const prType = prerelease[0];
updateVersion(`v${major}.${minor}.${patch}-${prType}`, name);
updateVersion(prType, name);
} else {
updateVersion(`v${major}.${minor}.x`, name);
updateVersion(`v${major}.x.x`, name);
updateVersion("stable", name);
}
}
console.log("Found versions:");
console.log(JSON.stringify(versions, null, 2));
function ln(from, to) {
if (fs.existsSync(to)) {
if (!isSL(to)) return;
fs.unlinkSync(to);
}
console.log(`Linking: ${to} -> ${from}`);
fs.symlinkSync(from, to);
}
let versionLinks = "";
let keys = Object.keys(versions).sort(cmpKeys);
for (const key of keys) {
ln(versions[key], key);
versionLinks += `\n<li><a href="${key}">${key}</a></li>`;
}
// Update version links.
console.log("Updating version links in index.html...");
let index = fs.readFileSync("./index.html", { encoding: "utf-8" });
index = index.replace(
/<!-- START:VERSIONS -->[\s\S]*?<!-- END:VERSIONS -->/,
`<!-- START:VERSIONS -->${versionLinks}\n<!-- END:VERSIONS -->`
);
fs.writeFileSync("./index.html", index);
console.log("index.html updated successfully!");