2020-09-27 15:10:11 +03:00
|
|
|
const allVersions = require('./all-versions')
|
2020-09-30 17:28:47 +03:00
|
|
|
const versionSatisfiesRange = require('./version-satisfies-range')
|
2021-03-22 19:20:27 +03:00
|
|
|
const checkIfNextVersionOnly = require('./check-if-next-version-only')
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
// 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}`)
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:36:07 +03:00
|
|
|
// all versions are applicable!
|
|
|
|
if (frontmatterVersions === '*') {
|
|
|
|
return Object.keys(allVersions)
|
2020-09-27 15:10:11 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:36:07 +03:00
|
|
|
// get an array like: [ 'free-pro-team@latest', 'enterprise-server@2.21', 'enterprise-cloud@latest' ]
|
|
|
|
const applicableVersions = []
|
|
|
|
|
2021-03-22 19:20:27 +03:00
|
|
|
let isNextVersionOnly = false
|
2021-03-17 17:29:16 +03:00
|
|
|
|
2020-09-29 20:36:07 +03:00
|
|
|
// where frontmatter is something like:
|
|
|
|
// free-pro-team: '*'
|
|
|
|
// enterprise-server: '>=2.19'
|
|
|
|
// enterprise-cloud: '*'
|
|
|
|
// private-instances: '*'
|
2021-01-05 18:53:05 +03:00
|
|
|
// ^ where each key corresponds to a plan
|
2020-09-29 20:36:07 +03:00
|
|
|
Object.entries(frontmatterVersions)
|
|
|
|
.forEach(([plan, planValue]) => {
|
2021-03-22 19:20:27 +03:00
|
|
|
// 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)
|
|
|
|
.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)) {
|
2020-09-27 15:10:11 +03:00
|
|
|
applicableVersions.push(relevantVersion.version)
|
|
|
|
}
|
2021-03-22 19:20:27 +03:00
|
|
|
})
|
2020-09-29 20:36:07 +03:00
|
|
|
})
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2021-03-22 19:20:27 +03:00
|
|
|
if (!applicableVersions.length && !isNextVersionOnly) {
|
2020-09-29 20:36:07 +03:00
|
|
|
throw new Error(`No applicable versions found for ${filepath}. Please double-check the page's \`versions\` frontmatter.`)
|
2020-09-27 15:10:11 +03:00
|
|
|
}
|
2020-09-29 20:36:07 +03:00
|
|
|
|
|
|
|
return applicableVersions
|
2020-09-27 15:10:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getApplicableVersions
|