forked from crossplane/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.js
executable file
·54 lines (47 loc) · 1.49 KB
/
preprocess.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
"use strict";
// on publish this script will check all docs versions and prepare static data
// so it does not have to be generated using complicated logic from liquid templates / jekyll
const fs = require("fs");
const jsonfile = require("jsonfile");
const path = require("path");
const semver = require("semver");
function getDirectories(srcpath) {
return fs
.readdirSync(srcpath)
.filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory());
}
const ROOT_DIR = `${__dirname}`;
// This version map and version function allow versions that do not follow semver syntax to also
// be included in the version selection sorting for the site. "local" is the developer version
// used when testing docs changes in a local development environment. We set this "local"
// version as 7.7.7 (a high value) so that it will show up as the "latest" version in the site's
// version selection dropdown.
const versionMap = new Map([
["local", "7.7.7"]
])
function version(v) {
if (versionMap.has(v)){
return versionMap.get(v)
}
return semver.coerce(v).version
}
// collect all docs versions (forcing master to the end)
const data = [];
const versions = [
...getDirectories(`${ROOT_DIR}/docs`)
.filter(v => v !== "master")
.sort((a, b) =>
semver.rcompare(version(a), version(b))
),
"master"
];
// write json for jekyll (and browser)
jsonfile.writeFileSync(
"./_data/versions.json",
versions.map(pv => {
return {
version: pv,
path: `/docs/${pv}`
};
})
);