Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

old-versions-utils.js 3.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  1. const path = require('path')
  2. const { supported, latest } = require('./enterprise-server-releases')
  3. const latestNewVersion = `enterprise-server@${latest}`
  4. const patterns = require('./patterns')
  5. const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
  6. const oldVersions = ['dotcom'].concat(supported)
  7. const newVersions = Object.keys(require('./all-versions'))
  8. // Utility functions for converting between old version paths and new version paths.
  9. // See lib/path-utils.js for utility functions based on new paths.
  10. // Examples:
  11. // OLD /github/category/article to NEW /free-pro-team@latest/github/category/article
  12. // OLD /enterprise/2.21/user/github/category/article to NEW /enterprise-server@2.21/github/category/article
  13. // OLD /enterprise/user/github/category/article to NEW /enterprise-server@<latest>/github/category/article
  14. // Given a new version like enterprise-server@2.21,
  15. // return an old version like 2.21.
  16. // Fall back to latest GHES version if one can't be found,
  17. // for example, if the new version is private-instances@latest.
  18. function getOldVersionFromNewVersion (newVersion) {
  19. return newVersion === nonEnterpriseDefaultVersion
  20. ? 'dotcom'
  21. : oldVersions.find(oldVersion => newVersion.includes(oldVersion)) || latest
  22. }
  23. // Given an old version like 2.21,
  24. // return a new version like enterprise-server@2.21.
  25. // Fall back to latest GHES version if one can't be found.
  26. function getNewVersionFromOldVersion (oldVersion) {
  27. return oldVersion === 'dotcom'
  28. ? nonEnterpriseDefaultVersion
  29. : newVersions.find(newVersion => newVersion.includes(oldVersion)) || latestNewVersion
  30. }
  31. // Given an old path like /enterprise/2.21/user/github/category/article,
  32. // return an old version like 2.21.
  33. function getOldVersionFromOldPath (oldPath, languageCode) {
  34. // We should never be calling this function on a path that starts with a new version,
  35. // so we can assume the path either uses the old /enterprise format or it's dotcom.
  36. if (!patterns.enterprise.test(oldPath)) return 'dotcom'
  37. const ghesNumber = oldPath.match(patterns.getEnterpriseVersionNumber)
  38. return ghesNumber ? ghesNumber[1] : latest
  39. }
  40. // Given an old path like /en/enterprise/2.21/user/github/category/article,
  41. // return a new path like /en/enterprise-server@2.21/github/category/article.
  42. function getNewVersionedPath (oldPath, languageCode = '') {
  43. // It's possible a new version has been injected into an old path
  44. // via syntax like: /en/enterprise/{{ currentVersion }}/admin/category/article
  45. // which could resolve to /en/enterprise/private-instances@latest/admin/category/article,
  46. // in which case the new version is the `private-instances@latest` segment.
  47. // Get the second or third segment depending on whether there is a lang code.
  48. const pathParts = oldPath.split('/')
  49. const possibleVersion = languageCode ? pathParts[3] : pathParts[2]
  50. let newVersion = newVersions.includes(possibleVersion) ? possibleVersion : ''
  51. // If no new version was found, assume path contains an old version, like 2.21
  52. if (!newVersion) {
  53. const oldVersion = getOldVersionFromOldPath(oldPath, languageCode)
  54. newVersion = getNewVersionFromOldVersion(oldVersion)
  55. }
  56. // Remove /<lang>?/enterprise?/<version>?/user? if present.
  57. // This leaves only the part of the string that starts with the product.
  58. // Example: /github/category/article
  59. const restOfString = oldPath.replace(patterns.oldEnterprisePath, '')
  60. // Add the language and new version to the product part of the string
  61. return path.posix.join('/', languageCode, newVersion, restOfString)
  62. }
  63. module.exports = {
  64. oldVersions,
  65. getOldVersionFromOldPath,
  66. getOldVersionFromNewVersion,
  67. getNewVersionFromOldVersion,
  68. getNewVersionedPath
  69. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...