2020-09-27 15:10:11 +03:00
|
|
|
const allVersions = require('./all-versions')
|
2021-03-17 17:29:16 +03:00
|
|
|
const { next } = require('./enterprise-server-releases')
|
2020-09-30 17:28:47 +03:00
|
|
|
const versionSatisfiesRange = require('./version-satisfies-range')
|
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-17 17:29:16 +03:00
|
|
|
let nextVersion = false
|
|
|
|
|
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-17 17:29:16 +03:00
|
|
|
// Special handling for frontmatter that evalues to the next GHES release number or a hardcoded `next`:
|
|
|
|
// we don't want to return it in the applicable versions array or it will become a permalink,
|
|
|
|
// but we also don't want to throw an error if no other versions are found.
|
|
|
|
if (planValue !== '*') {
|
|
|
|
if (versionSatisfiesRange(next, planValue) || planValue === 'next') {
|
|
|
|
nextVersion = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:36:07 +03:00
|
|
|
// 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.`)
|
|
|
|
}
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-09-29 20:36:07 +03:00
|
|
|
relevantVersions.forEach(relevantVersion => {
|
|
|
|
// special handling for versions with numbered releases
|
|
|
|
if (relevantVersion.hasNumberedReleases) {
|
2020-09-30 17:28:47 +03:00
|
|
|
if (versionSatisfiesRange(relevantVersion.currentRelease, planValue)) {
|
2020-09-27 15:10:11 +03:00
|
|
|
applicableVersions.push(relevantVersion.version)
|
|
|
|
}
|
2020-09-29 20:36:07 +03:00
|
|
|
} else {
|
|
|
|
applicableVersions.push(relevantVersion.version)
|
|
|
|
}
|
2020-09-27 15:10:11 +03:00
|
|
|
})
|
2020-09-29 20:36:07 +03:00
|
|
|
})
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2021-03-17 17:29:16 +03:00
|
|
|
if (!applicableVersions.length && !nextVersion) {
|
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
|