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

ghes-to-ghae-versioning.js 6.5 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const walk = require('walk-sync')
  5. const program = require('commander')
  6. const frontmatter = require('../../lib/read-frontmatter')
  7. const contentPath = path.join(process.cwd(), 'content')
  8. const dataPath = path.join(process.cwd(), 'data')
  9. const translationsPath = path.join(process.cwd(), 'translations')
  10. const { getEnterpriseServerNumber } = require('../../lib/patterns')
  11. const versionSatisfiesRange = require('../../lib/version-satisfies-range')
  12. // [start-readme]
  13. //
  14. // Run this script to add versions frontmatter and Liquid conditionals for
  15. // GitHub AE, based on anything currently versioned for the specified release
  16. // of Enterprise Server. This script should be run as part of the Enterprise
  17. // Server release process.
  18. //
  19. // [end-readme]
  20. program
  21. .description('Add versions frontmatter and Liquid conditionals for GitHub AE based on a given Enterprise Server release. Runs on all content by default.')
  22. .option('-r, --ghes-release <RELEASE>', 'The Enterprise Server release to base AE versioning on. Example: 2.23')
  23. .option('-p, --products [OPTIONAL PRODUCT_IDS...]', 'Optional list of space-separated product IDs. Example: admin github developers')
  24. .option('-t, --translations', 'Run the script on content and data in translations too.')
  25. .parse(process.argv)
  26. const { ghesRelease, products, translations } = program.opts()
  27. if (!ghesRelease) {
  28. console.error('Must provide an Enterprise Server release number!')
  29. process.exit(1)
  30. }
  31. console.log(`✅ Adding AE versioning based on GHES ${ghesRelease} versioning`)
  32. if (products) {
  33. console.log(`✅ Running on the following products: ${products}`)
  34. } else {
  35. console.log('✅ Running on all products')
  36. }
  37. if (translations) {
  38. console.log('✅ Running on both English and translated content and data\n')
  39. } else {
  40. console.log('✅ Running on English content and data\n')
  41. }
  42. // The new conditional to add
  43. const githubAEConditional = 'currentVersion == "github-ae@latest"'
  44. // Match: currentVersion <operator> "enterprise-server@(\d+\.\d+)"
  45. // Example: currentVersion ver_gt "enterprise-server@2.21"
  46. const enterpriseServerConditionalRegex = new RegExp(`currentVersion (\\S+?) "${getEnterpriseServerNumber.source}"`)
  47. console.log('Working...\n')
  48. const englishContentFiles = walkContent(contentPath)
  49. const englishDataFiles = walkData(dataPath)
  50. function walkContent (dirPath) {
  51. const productArray = products || ['']
  52. return productArray.map(product => {
  53. dirPath = path.join(contentPath, product)
  54. return walk(dirPath, { includeBasePath: true, directories: false })
  55. .filter(file => file.includes('/content/'))
  56. .filter(file => file.endsWith('.md'))
  57. .filter(file => !file.endsWith('README.md'))
  58. }).flat()
  59. }
  60. function walkData (dirPath) {
  61. return walk(dirPath, { includeBasePath: true, directories: false })
  62. .filter(file => file.includes('/data/reusables') || file.includes('/data/variables'))
  63. .filter(file => !file.endsWith('README.md'))
  64. }
  65. let allContentFiles, allDataFiles
  66. if (translations) {
  67. const translatedContentFiles = walkContent(translationsPath)
  68. const translatedDataFiles = walkData(translationsPath)
  69. allContentFiles = englishContentFiles.concat(translatedContentFiles)
  70. allDataFiles = englishDataFiles.concat(translatedDataFiles)
  71. } else {
  72. allContentFiles = englishContentFiles
  73. allDataFiles = englishDataFiles
  74. }
  75. // Map Liquid operators to semver operators
  76. const operators = {
  77. ver_gt: '>',
  78. ver_lt: '<',
  79. '==': '='
  80. }
  81. // Update the data files
  82. allDataFiles
  83. .forEach(file => {
  84. const dataContent = fs.readFileSync(file, 'utf8')
  85. const conditionalsToUpdate = getConditionalsToUpdate(dataContent)
  86. if (!conditionalsToUpdate.length) return
  87. // Update Liquid in data files
  88. const newDataContent = updateLiquid(conditionalsToUpdate, dataContent)
  89. fs.writeFileSync(file, newDataContent)
  90. })
  91. // Update the content files
  92. allContentFiles
  93. .forEach(file => {
  94. const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
  95. // Return early if the current page frontmatter does not apply to either GHAE or the given GHES release
  96. if (!(data.versions['github-ae'] || versionSatisfiesRange(ghesRelease, data.versions['enterprise-server']))) return
  97. const conditionalsToUpdate = getConditionalsToUpdate(content)
  98. if (!conditionalsToUpdate.length) return
  99. // Update Liquid in content files
  100. const newContent = updateLiquid(conditionalsToUpdate, content)
  101. // Add frontmatter version
  102. data.versions['github-ae'] = '*'
  103. // Update Liquid in frontmatter props
  104. Object.keys(data)
  105. .filter(key => typeof data[key] === 'string')
  106. .forEach(key => {
  107. const conditionalsToUpdate = getConditionalsToUpdate(data[key])
  108. if (!conditionalsToUpdate.length) return
  109. data[key] = updateLiquid(conditionalsToUpdate, data[key])
  110. })
  111. fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
  112. })
  113. function getConditionalsToUpdate (content) {
  114. const allConditionals = content.match(/{% if .+?%}/g)
  115. return (allConditionals || [])
  116. .filter(conditional => !conditional.includes('github-ae'))
  117. .filter(conditional => doesReleaseSatisfyConditional(conditional.match(enterpriseServerConditionalRegex)))
  118. }
  119. function updateLiquid (conditionalsToUpdate, content) {
  120. let newContent = content
  121. conditionalsToUpdate.forEach(conditional => {
  122. let newConditional = conditional
  123. const enterpriseServerMatch = conditional.match(enterpriseServerConditionalRegex)
  124. // First do the replacement within the conditional
  125. // Old: {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.21" %}
  126. // New: {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.21" or currentVersion == "github-ae@latest" %}
  127. newConditional = newConditional.replace(enterpriseServerMatch[0], `${enterpriseServerMatch[0]} or ${githubAEConditional}`)
  128. // Then replace all instances of the conditional in the content
  129. newContent = newContent.replace(conditional, newConditional)
  130. })
  131. return newContent
  132. }
  133. console.log('Done!')
  134. function doesReleaseSatisfyConditional (enterpriseServerMatch) {
  135. if (!enterpriseServerMatch) return
  136. // Example liquid operator: ver_gt
  137. const liquidOperator = enterpriseServerMatch[1]
  138. // Example semver operator: >
  139. const semverOperator = operators[liquidOperator]
  140. // Example number: 2.21
  141. const number = enterpriseServerMatch[2]
  142. // Example range: >2.21
  143. const range = `${semverOperator}${number}`
  144. return versionSatisfiesRange(ghesRelease, range)
  145. }
Tip!

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

Comments

Loading...