|
1 | | -const { latest } = require('../enterprise-server-releases') |
| 1 | +const { latest, deprecated, lastReleaseWithLegacyFormat } = require('../enterprise-server-releases') |
2 | 2 | const { getPathWithoutLanguage, getPathWithLanguage, getVersionStringFromPath } = require('../path-utils') |
| 3 | +const patterns = require('../patterns') |
| 4 | +const versionSatisfiesRange = require('../version-satisfies-range') |
3 | 5 | const currentlySupportedVersions = Object.keys(require('../all-versions')) |
4 | 6 | const nonEnterpriseDefaultVersion = require('../non-enterprise-default-version') |
5 | 7 |
|
6 | 8 | // This function takes a current path, applies what we know about historically |
7 | 9 | // supported paths, and returns an array of ALL possible associated old |
8 | 10 | // paths that users might try to hit. |
9 | | -module.exports = function getOldPathsFromPath (currentPath, languageCode) { |
| 11 | +module.exports = function getOldPathsFromPath (currentPath, languageCode, currentVersion) { |
10 | 12 | const oldPaths = new Set() |
11 | 13 |
|
12 | 14 | const versionFromPath = getVersionStringFromPath(currentPath) |
13 | 15 |
|
14 | | - // The following only applies to Dotcom paths. |
15 | | - // Create old path /free-pro-team@latest/github from new path /github (or from a frontmatter `redirect_from` path like /articles) |
16 | | - if (versionFromPath === 'homepage' || !currentlySupportedVersions.includes(versionFromPath) || (versionFromPath === nonEnterpriseDefaultVersion && !currentPath.includes(nonEnterpriseDefaultVersion))) { |
| 16 | + // This only applies to Dotcom paths, so no need to determine whether the version is deprecated |
| 17 | + // create old path /free-pro-team@latest/github from new path /github (or from a frontmatter `redirect_from` path like /articles) |
| 18 | + if (versionFromPath === 'homepage' || !(currentlySupportedVersions.includes(versionFromPath) || deprecated.includes(versionFromPath)) || (versionFromPath === nonEnterpriseDefaultVersion && !currentPath.includes(nonEnterpriseDefaultVersion))) { |
17 | 19 | oldPaths.add(currentPath |
18 | 20 | .replace(`/${languageCode}`, `/${languageCode}/${nonEnterpriseDefaultVersion}`)) |
19 | 21 | } |
20 | 22 |
|
21 | | - oldPaths.add(currentPath |
22 | | - .replace(/\/enterprise-server@(\d)/, '/enterprise/$1')) |
| 23 | + // ------ BEGIN LEGACY VERSION FORMAT REPLACEMENTS ------// |
| 24 | + // These remain relevant to handle legacy-formatted frontmatter redirects |
| 25 | + // and archived versions paths. |
23 | 26 |
|
24 | | - // create old path /enterprise/<version>/user from new path /enterprise-server@<version>/github |
| 27 | + // create old path /insights from current path /enterprise/version/insights |
25 | 28 | oldPaths.add(currentPath |
26 | | - .replace(/\/enterprise-server@(\d.+?)\/github/, '/enterprise/$1/user')) |
| 29 | + .replace(`/${languageCode}/enterprise/${latest}/user/insights`, '/insights')) |
27 | 30 |
|
28 | | - // create old path /insights from new path /enterprise-server@<latest>/insights |
29 | | - oldPaths.add(currentPath |
30 | | - .replace(`/enterprise-server@${latest}/insights`, '/insights')) |
| 31 | + // create old path /desktop/guides from current path /desktop |
| 32 | + // create old path /admin/guides from current path /admin |
| 33 | + if (!currentPath.includes('/guides')) { |
| 34 | + oldPaths.add(currentPath |
| 35 | + .replace('/desktop', '/desktop/guides') |
| 36 | + .replace('/admin', '/admin/guides')) |
| 37 | + } |
| 38 | + |
| 39 | + // create old path /user from current path /user/github on 2.16+ only |
| 40 | + if (currentlySupportedVersions.includes(currentVersion) || versionSatisfiesRange(currentVersion, '>2.15')) { |
| 41 | + oldPaths.add(currentPath |
| 42 | + .replace('/user/github', '/user')) |
| 43 | + } |
31 | 44 |
|
32 | | - // create old path /admin from new path /enterprise-server@<latest>/admin |
| 45 | + // create old path /enterprise from current path /enterprise/latest |
33 | 46 | oldPaths.add(currentPath |
34 | | - .replace(`/enterprise-server@${latest}/admin`, '/admin')) |
| 47 | + .replace(`/enterprise/${latest}`, '/enterprise')) |
| 48 | + |
| 49 | + // create old path /enterprise/foo from current path /enterprise/user/foo |
| 50 | + // this supports old developer paths like /enterprise/webhooks with no /user in them |
| 51 | + if (currentPath.includes('/enterprise/')) { |
| 52 | + oldPaths.add(currentPath |
| 53 | + .replace('/user/', '/')) |
| 54 | + } |
35 | 55 |
|
36 | | - // create old path /enterprise from new path /enterprise-server@<latest> |
| 56 | + // create old path /enterprise/foo from current path /enterprise/latest/user/foo |
37 | 57 | oldPaths.add(currentPath |
38 | | - .replace(`/enterprise-server@${latest}`, '/enterprise')) |
| 58 | + .replace(`/enterprise/${latest}/user/`, '/enterprise/')) |
| 59 | + |
| 60 | + // ------ END LEGACY VERSION FORMAT REPLACEMENTS ------// |
| 61 | + |
| 62 | + // ------ BEGIN MODERN VERSION FORMAT REPLACEMENTS ------// |
| 63 | + if (currentlySupportedVersions.includes(currentVersion) || versionSatisfiesRange(currentVersion, `>${lastReleaseWithLegacyFormat}`)) { |
| 64 | + (new Set(oldPaths)).forEach(oldPath => { |
| 65 | + // create old path /enterprise/<version> from new path /enterprise-server@<version> |
| 66 | + oldPaths.add(oldPath |
| 67 | + .replace(/\/enterprise-server@(\d)/, '/enterprise/$1')) |
| 68 | + |
| 69 | + // create old path /enterprise/<version>/user from new path /enterprise-server@<version>/github |
| 70 | + oldPaths.add(oldPath |
| 71 | + .replace(/\/enterprise-server@(\d.+?)\/github/, '/enterprise/$1/user')) |
| 72 | + |
| 73 | + // create old path /insights from new path /enterprise-server@<latest>/insights |
| 74 | + oldPaths.add(oldPath |
| 75 | + .replace(`/enterprise-server@${latest}/insights`, '/insights')) |
| 76 | + |
| 77 | + // create old path /admin from new path /enterprise-server@<latest>/admin |
| 78 | + oldPaths.add(oldPath |
| 79 | + .replace(`/enterprise-server@${latest}/admin`, '/admin')) |
| 80 | + |
| 81 | + // create old path /enterprise from new path /enterprise-server@<latest> |
| 82 | + oldPaths.add(oldPath |
| 83 | + .replace(`/enterprise-server@${latest}`, '/enterprise')) |
| 84 | + |
| 85 | + // create old path /enterprise-server from new path /enterprise-server@<latest> |
| 86 | + oldPaths.add(oldPath |
| 87 | + .replace(`/enterprise-server@${latest}`, '/enterprise-server')) |
| 88 | + |
| 89 | + if (!patterns.adminProduct.test(oldPath)) { |
| 90 | + // create old path /enterprise/<version>/user/foo from new path /enterprise-server@<version>/foo |
| 91 | + oldPaths.add(currentPath |
| 92 | + .replace(/\/enterprise-server@(\d.+?)\//, '/enterprise/$1/user/')) |
| 93 | + |
| 94 | + // create old path /enterprise/user/foo from new path /enterprise-server@<latest>/foo |
| 95 | + oldPaths.add(currentPath |
| 96 | + .replace(`/enterprise-server@${latest}/`, '/enterprise/user/')) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | + // ------ END MODERN VERSION FORMAT REPLACEMENTS ------// |
39 | 101 |
|
40 | 102 | // For each old path added to the set above, do the following... |
41 | | - oldPaths.forEach(oldPath => { |
| 103 | + (new Set(oldPaths)).forEach(oldPath => { |
42 | 104 | // for English only, remove language code |
43 | 105 | if (languageCode === 'en') { |
44 | 106 | oldPaths.add(getPathWithoutLanguage(oldPath)) |
|
0 commit comments