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 7.8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  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 provided 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 [PRODUCT_IDS...]', 'List of space-separated product IDs. Example: admin github developers')
  24. .option('-c, --currentServerReleases', 'Also add AE versioning to conditionals for current Enterprise Server releases.')
  25. .option('-t, --translations', 'Run the script on content and data in translations, too.')
  26. .parse(process.argv)
  27. if (!program.ghesRelease) {
  28. console.log('Must provide an Enterprise Server release number!')
  29. process.exit()
  30. }
  31. if (program.products) {
  32. console.log(`✅ Running on the following products: ${program.products}`)
  33. } else {
  34. console.log('✅ Running on all products')
  35. }
  36. if (program.currentServerReleases) {
  37. console.log(`✅ Adding AE versioning based on GHES ${program.ghesRelease} versioning and all currently supported GHES versions`)
  38. } else {
  39. console.log(`✅ Adding AE versioning based only on GHES ${program.ghesRelease} versioning`)
  40. }
  41. if (program.translations) {
  42. console.log('✅ Running on both English and translated content and data\n')
  43. } else {
  44. console.log('✅ Running on English content and data\n')
  45. }
  46. // The new conditional to add
  47. const githubAEConditional = 'currentVersion == "github-ae@latest"'
  48. // Existing conditionals to hook on (if program.currentServerReleases is true)
  49. const notDotcomConditional = /currentVersion != "free-pro-team@latest" (or)?(?!and)/
  50. const allGHESVersionsConditional = /enterpriseServerVersions contains currentVersion (or)?/
  51. // Match: currentVersion <operator> "enterprise-server@(\d+\.\d+)"
  52. const getEnterpriseServerConditional = new RegExp(`currentVersion (\\S+?) "${getEnterpriseServerNumber.source}"`)
  53. console.log('Working...\n')
  54. const englishContentFiles = walkContent(contentPath)
  55. const englishDataFiles = walkData(dataPath, englishContentFiles)
  56. function walkContent (dirPath) {
  57. const products = program.products || ['']
  58. return products.map(product => {
  59. dirPath = path.join(contentPath, product)
  60. return walk(dirPath, { includeBasePath: true, directories: false })
  61. .filter(file => file.includes('/content/'))
  62. .filter(file => file.endsWith('.md'))
  63. .filter(file => !file.endsWith('README.md'))
  64. }).flat()
  65. }
  66. function walkData (dirPath, contentFiles) {
  67. return walk(dirPath, { includeBasePath: true, directories: false })
  68. .filter(file => file.includes('/data/reusables') || file.includes('/data/variables'))
  69. .filter(file => !file.endsWith('README.md'))
  70. }
  71. let allContentFiles, allDataFiles
  72. if (program.translations) {
  73. const translatedContentFiles = walkContent(translationsPath)
  74. const translatedDataFiles = walkData(translationsPath, translatedContentFiles)
  75. allContentFiles = englishContentFiles.concat(translatedContentFiles)
  76. allDataFiles = englishDataFiles.concat(translatedDataFiles)
  77. } else {
  78. allContentFiles = englishContentFiles
  79. allDataFiles = englishDataFiles
  80. }
  81. // Map Liquid operators to semver operators
  82. const operators = {
  83. ver_gt: '>',
  84. ver_lt: '<',
  85. '==': '='
  86. }
  87. allDataFiles
  88. .forEach(file => {
  89. const dataContent = fs.readFileSync(file, 'utf8')
  90. // Update Liquid in data files
  91. const newDataContent = updateLiquid(dataContent, file)
  92. fs.writeFileSync(file, newDataContent)
  93. })
  94. allContentFiles
  95. .forEach(file => {
  96. const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
  97. // Return early if the current page frontmatter does not apply to either GHAE or the given GHES release
  98. if (!(data.versions['github-ae'] || versionSatisfiesRange(program.ghesRelease, data.versions['enterprise-server']))) return
  99. // Add frontmatter version
  100. data.versions['github-ae'] = '*'
  101. // Update Liquid in content files
  102. const newContent = updateLiquid(content, file)
  103. // Update Liquid in frontmatter props
  104. Object.keys(data)
  105. .filter(key => typeof data[key] === 'string')
  106. .forEach(key => {
  107. data[key] = updateLiquid(data[key], file)
  108. })
  109. fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
  110. })
  111. function updateLiquid (content, file) {
  112. // We need to match on all conditionals first because we have to do replacements _within_ conditionals
  113. const allConditionals = content.match(/{% if .+?%}/g)
  114. if (!allConditionals) return content
  115. let newContent = content
  116. allConditionals.forEach(conditional => {
  117. // Do not process a conditional that already includes github-ae
  118. if (conditional.includes('github-ae')) return
  119. let newConditional = conditional
  120. // Example match: currentVersion ver_gt "enterprise-server@2.21"
  121. const enterpriseServerMatch = newConditional.match(getEnterpriseServerConditional)
  122. // Add AE conditional to any `currentVersion != "free-pro-team@latest"`
  123. if (program.currentServerReleases && newConditional.match(notDotcomConditional)) {
  124. if (enterpriseServerMatch && !doesReleaseSatisfyConditional(enterpriseServerMatch)) return
  125. newConditional = newConditional.replace(/( ?)%}/, `$1or ${githubAEConditional} %}`)
  126. newContent = newContent.replace(conditional, newConditional)
  127. return
  128. }
  129. // Add AE conditional to any `enterpriseServerVersions contains currentVersion`
  130. if (program.currentServerReleases && newConditional.match(allGHESVersionsConditional)) {
  131. if (enterpriseServerMatch && !doesReleaseSatisfyConditional(enterpriseServerMatch)) return
  132. newConditional = newConditional.replace(/( ?)%}/, `$1or ${githubAEConditional} %}`)
  133. newContent = newContent.replace(conditional, newConditional)
  134. return
  135. }
  136. // Add AE conditional to any conditional that applies to enterprise-server@<provided-release>
  137. if (!enterpriseServerMatch) return
  138. const releaseSatisfiesConditional = doesReleaseSatisfyConditional(enterpriseServerMatch)
  139. // Return early if the conditional does not apply to the given GHES release
  140. if (!releaseSatisfiesConditional) return
  141. // First do the replacement within the conditional
  142. // Old: {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.21" %}
  143. // New: {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.21" or currentVersion == "github-ae@latest" %}
  144. newConditional = newConditional.replace(enterpriseServerMatch[0], `${enterpriseServerMatch[0]} or ${githubAEConditional}`)
  145. // Then replace all instances of the conditional in the content
  146. newContent = newContent.replace(conditional, newConditional)
  147. })
  148. return newContent
  149. }
  150. console.log('Done!')
  151. function doesReleaseSatisfyConditional (enterpriseServerMatch) {
  152. // Example liquid operator: ver_gt
  153. const liquidOperator = enterpriseServerMatch[1]
  154. // Example semver operator: >
  155. const semverOperator = operators[liquidOperator]
  156. // Example number: 2.21
  157. const number = enterpriseServerMatch[2]
  158. // Example range: >2.21
  159. const range = `${semverOperator}${number}`
  160. return versionSatisfiesRange(program.ghesRelease, range)
  161. }
Tip!

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

Comments

Loading...