forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-applicable-versions.js
52 lines (43 loc) · 1.89 KB
/
get-applicable-versions.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
const allVersions = require('./all-versions')
const versionSatisfiesRange = require('./version-satisfies-range')
// return an array of versions that an article's product versions encompasses
function getApplicableVersions (frontmatterVersions, filepath) {
if (typeof frontmatterVersions === 'undefined') {
throw new Error(`No \`versions\` frontmatter found in ${filepath}`)
}
// all versions are applicable!
if (frontmatterVersions === '*') {
return Object.keys(allVersions)
}
// get an array like: [ 'free-pro-team@latest', '[email protected]', 'enterprise-cloud@latest' ]
const applicableVersions = []
// where frontmatter is something like:
// free-pro-team: '*'
// enterprise-server: '>=2.19'
// enterprise-cloud: '*'
// private-instances: '*'
// ^ where each key correponds to a plan
Object.entries(frontmatterVersions)
.forEach(([plan, planValue]) => {
// for each plan (e.g., enterprise-server), get matching versions from allVersions object
const relevantVersions = Object.values(allVersions).filter(v => v.plan === plan)
if (!relevantVersions.length) {
throw new Error(`No applicable versions found for ${filepath}. Please double-check the page's \`versions\` frontmatter.`)
}
relevantVersions.forEach(relevantVersion => {
// special handling for versions with numbered releases
if (relevantVersion.hasNumberedReleases) {
if (versionSatisfiesRange(relevantVersion.currentRelease, planValue)) {
applicableVersions.push(relevantVersion.version)
}
} else {
applicableVersions.push(relevantVersion.version)
}
})
})
if (!applicableVersions.length) {
throw new Error(`No applicable versions found for ${filepath}. Please double-check the page's \`versions\` frontmatter.`)
}
return applicableVersions
}
module.exports = getApplicableVersions