forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-applicable-versions.js
123 lines (106 loc) · 4.28 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { fileURLToPath } from 'url'
import path from 'path'
import { reduce, sortBy } from 'lodash-es'
import { allVersions } from './all-versions.js'
import versionSatisfiesRange from './version-satisfies-range.js'
import checkIfNextVersionOnly from './check-if-next-version-only.js'
import dataDirectory from './data-directory.js'
import encodeBracketedParentheses from './encode-bracketed-parentheses.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const featuresDir = path.posix.join(__dirname, '../data/features')
const featureData = dataDirectory(featuresDir, {
preprocess: (dataString) => encodeBracketedParentheses(dataString.trimEnd()),
ignorePatterns: [/README\.md$/],
})
// 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)
}
// Check for frontmatter that includes a feature name, like:
// fpt: '*'
// feature: 'foo'
// or multiple feature names, like:
// fpt: '*'
// feature: ['foo', 'bar']
// and add the versions affiliated with the feature (e.g., foo) to the frontmatter versions object:
// fpt: '*'
// ghes: '>=2.23'
// ghae: '*'
// where the feature is bringing the ghes and ghae versions into the mix.
const featureVersions = reduce(
frontmatterVersions,
(result, value, key) => {
if (key === 'feature') {
if (typeof value === 'string') {
Object.assign(result, { ...featureData[value].versions })
} else if (Array.isArray(value)) {
value.forEach((str) => {
Object.assign(result, { ...featureData[str].versions })
})
}
delete result[key]
}
return result
},
{}
)
// We will be evaluating feature versions separately, so we can remove this.
delete frontmatterVersions.feature
// Get available versions for frontmatter and for feature versions.
const foundFeatureVersions = evaluateVersions(featureVersions)
const foundFrontmatterVersions = evaluateVersions(frontmatterVersions)
// Combine them!
const applicableVersions = [
...new Set(foundFrontmatterVersions.versions.concat(foundFeatureVersions.versions)),
]
if (
!applicableVersions.length &&
!foundFrontmatterVersions.isNextVersionOnly &&
!foundFeatureVersions.isNextVersionOnly
) {
throw new Error(
`No applicable versions found for ${filepath}. Please double-check the page's \`versions\` frontmatter.`
)
}
// Sort them by the order in lib/all-versions.
const sortedVersions = sortBy(applicableVersions, (v) => {
return Object.keys(allVersions).indexOf(v)
})
return sortedVersions
}
function evaluateVersions(versionsObj) {
let isNextVersionOnly = false
// get an array like: [ 'free-pro-team@latest', '[email protected]', 'enterprise-cloud@latest' ]
const versions = []
// where versions obj is something like:
// fpt: '*'
// ghes: '>=2.19'
// ghae: '*'
// ^ where each key corresponds to a plan's short name (defined in lib/all-versions.js)
Object.entries(versionsObj).forEach(([plan, planValue]) => {
// Special handling for frontmatter that evalues to the next GHES release number or a hardcoded `next`.
isNextVersionOnly = checkIfNextVersionOnly(planValue)
// For each plan (e.g., enterprise-server), get matching versions from allVersions object
Object.values(allVersions)
.filter(
(relevantVersion) => relevantVersion.plan === plan || relevantVersion.shortName === plan
)
.forEach((relevantVersion) => {
// Use a dummy value of '1.0' for non-numbered versions like free-pro-team and github-ae
// This will evaluate to true against '*' but false against 'next', which is what we want.
const versionToCompare = relevantVersion.hasNumberedReleases
? relevantVersion.currentRelease
: '1.0'
if (versionSatisfiesRange(versionToCompare, planValue)) {
versions.push(relevantVersion.version)
}
})
})
return { versions, isNextVersionOnly }
}
export default getApplicableVersions