diff --git a/lib/path-utils.js b/lib/path-utils.js index 02e75aab1d..cd5077a170 100644 --- a/lib/path-utils.js +++ b/lib/path-utils.js @@ -4,6 +4,8 @@ const patterns = require('./patterns') const { deprecated } = require('./enterprise-server-releases') const allProducts = require('./all-products') const allVersions = require('./all-versions') +const supportedVersions = Object.keys(allVersions) +const supportedPlans = Object.values(allVersions).map(v => v.plan) const { getNewVersionedPath } = require('./old-versions-utils') // construct appropriate versioned path for any given HREF @@ -22,10 +24,13 @@ function getVersionedPathWithoutLanguage (href, version) { // example: enterprise-server@2.22 or free-pro-team@latest let versionFromPath = getVersionStringFromPath(href) - // if the version found is not a currently supported version... + // update the path with the full version so we can go through the rest of the checks + href = href.replace(href.split('/')[1], versionFromPath) + + // if the version found is NOT a currently supported version... let productObjectFromPath - if (!Object.keys(allVersions).includes(versionFromPath)) { - // first check if the first segment is instead a current product; + if (![...supportedPlans, ...supportedVersions, 'enterprise-server@latest'].includes(versionFromPath)) { + // first check if the first segment is instead a current product; // example: /admin/foo or /desktop/foo productObjectFromPath = allProducts[versionFromPath] @@ -78,26 +83,45 @@ function getPathWithoutLanguage (href) { return slash(newHref) } +// Remove the version segment from the path function getPathWithoutVersion (href) { return href.replace(`/${getVersionStringFromPath(href)}`, '') } +// Return the version segment in a path function getVersionStringFromPath (href) { href = getPathWithoutLanguage(href) - const versionString = href.split('/')[1] - return versionString || 'homepage' + const versionFromPath = href.split('/')[1] + + // return checkVersionFromPath(href.split('/')[1], href) + if (allVersions[versionFromPath]) { + return versionFromPath + } + + // if the version segment is the latest enterprise-server release, return the latest release + if (versionFromPath === 'enterprise-server@latest') { + const enterpriseServerObject = Object.values(allVersions).find(v => v.plan === 'enterprise-server') + return allVersions[enterpriseServerObject.latestVersion].version + } + + // if it's just a plan with no @release, find the plan's latest release + const planObject = Object.values(allVersions).find(v => v.plan === versionFromPath) + if (planObject) { + return allVersions[planObject.latestVersion].version + } + + return versionFromPath || 'homepage' } +// Return the corresponding object for the version segment in a path function getVersionObjectFromPath (href) { - const versionId = getVersionStringFromPath(href) - const version = allVersions[versionId] + const versionFromPath = getVersionStringFromPath(href) - if (!version) throw new Error(`No version found for ${href}`) - - return version + return allVersions[versionFromPath] } +// Return the product segment from the path function getProductStringFromPath (href) { href = getPathWithoutLanguage(href) const productString = href.split('/')[2] @@ -105,10 +129,11 @@ function getProductStringFromPath (href) { return productString || 'homepage' } +// Return the corresponding object for the product segment in a path function getProductObjectFromPath (href) { - const productId = getProductStringFromPath(href) - // Return undefined if product id derived from path can't be found in allProducts - return allProducts[productId] + const productFromPath = getProductStringFromPath(href) + + return allProducts[productFromPath] } module.exports = {