Skip to content

Commit 8d84a90

Browse files
committed
do more path checking
1 parent 05ecaca commit 8d84a90

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

lib/path-utils.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const patterns = require('./patterns')
44
const { deprecated } = require('./enterprise-server-releases')
55
const allProducts = require('./all-products')
66
const allVersions = require('./all-versions')
7+
const supportedVersions = Object.keys(allVersions)
8+
const supportedPlans = Object.values(allVersions).map(v => v.plan)
79
const { getNewVersionedPath } = require('./old-versions-utils')
810

911
// construct appropriate versioned path for any given HREF
@@ -22,10 +24,13 @@ function getVersionedPathWithoutLanguage (href, version) {
2224
// example: enterprise-server@2.22 or free-pro-team@latest
2325
let versionFromPath = getVersionStringFromPath(href)
2426

25-
// if the version found is not a currently supported version...
27+
// update the path with the full version so we can go through the rest of the checks
28+
href = href.replace(href.split('/')[1], versionFromPath)
29+
30+
// if the version found is NOT a currently supported version...
2631
let productObjectFromPath
27-
if (!Object.keys(allVersions).includes(versionFromPath)) {
28-
// first check if the first segment is instead a current product;
32+
if (![...supportedPlans, ...supportedVersions, 'enterprise-server@latest'].includes(versionFromPath)) {
33+
// first check if the first segment is instead a current product;
2934
// example: /admin/foo or /desktop/foo
3035
productObjectFromPath = allProducts[versionFromPath]
3136

@@ -78,37 +83,57 @@ function getPathWithoutLanguage (href) {
7883
return slash(newHref)
7984
}
8085

86+
// Remove the version segment from the path
8187
function getPathWithoutVersion (href) {
8288
return href.replace(`/${getVersionStringFromPath(href)}`, '')
8389
}
8490

91+
// Return the version segment in a path
8592
function getVersionStringFromPath (href) {
8693
href = getPathWithoutLanguage(href)
87-
const versionString = href.split('/')[1]
8894

89-
return versionString || 'homepage'
95+
const versionFromPath = href.split('/')[1]
96+
97+
// return checkVersionFromPath(href.split('/')[1], href)
98+
if (allVersions[versionFromPath]) {
99+
return versionFromPath
100+
}
101+
102+
// if the version segment is the latest enterprise-server release, return the latest release
103+
if (versionFromPath === 'enterprise-server@latest') {
104+
const enterpriseServerObject = Object.values(allVersions).find(v => v.plan === 'enterprise-server')
105+
return allVersions[enterpriseServerObject.latestVersion].version
106+
}
107+
108+
// if it's just a plan with no @release, find the plan's latest release
109+
const planObject = Object.values(allVersions).find(v => v.plan === versionFromPath)
110+
if (planObject) {
111+
return allVersions[planObject.latestVersion].version
112+
}
113+
114+
return versionFromPath || 'homepage'
90115
}
91116

117+
// Return the corresponding object for the version segment in a path
92118
function getVersionObjectFromPath (href) {
93-
const versionId = getVersionStringFromPath(href)
94-
const version = allVersions[versionId]
119+
const versionFromPath = getVersionStringFromPath(href)
95120

96-
if (!version) throw new Error(`No version found for ${href}`)
97-
98-
return version
121+
return allVersions[versionFromPath]
99122
}
100123

124+
// Return the product segment from the path
101125
function getProductStringFromPath (href) {
102126
href = getPathWithoutLanguage(href)
103127
const productString = href.split('/')[2]
104128

105129
return productString || 'homepage'
106130
}
107131

132+
// Return the corresponding object for the product segment in a path
108133
function getProductObjectFromPath (href) {
109-
const productId = getProductStringFromPath(href)
110-
// Return undefined if product id derived from path can't be found in allProducts
111-
return allProducts[productId]
134+
const productFromPath = getProductStringFromPath(href)
135+
136+
return allProducts[productFromPath]
112137
}
113138

114139
module.exports = {

0 commit comments

Comments
 (0)