MCPcopy Index your code
hub / github.com/github/docs / getLiquidReplacements

Function getLiquidReplacements

script/content-migrations/use-short-versions.js:144–267  ·  view source on GitHub ↗
(content, file)

Source from the content-addressed store, hash-verified

142// if currentVersion ver_lt "myVersion@myRelease -> ifversion myVersionShort < myRelease
143// if enterpriseServerVersions contains currentVersion -> ifversion ghes
144function getLiquidReplacements(content, file) {
145 const replacements = {}
146
147 const tokenizer = new Tokenizer(content)
148 const tokens = removeInputProps(tokenizer.readTopLevelTokens())
149
150 tokens
151 .filter(
152 (token) =>
153 (token.name === 'if' || token.name === 'elsif') && token.content.includes('currentVersion')
154 )
155 .map((token) => token.content)
156 .forEach((token) => {
157 const newToken = token.startsWith('if') ? ['ifversion'] : ['elsif']
158 // Everything from here on pushes to the `newToken` array to construct the new conditional.
159 token
160 .replace(/(if|elsif) /, '')
161 .split(/ (or|and) /)
162 .forEach((op) => {
163 if (op === 'or' || op === 'and') {
164 newToken.push(op)
165 return
166 }
167
168 // This string will always resolve to `ifversion ghes`.
169 if (op.includes('enterpriseServerVersions contains currentVersion')) {
170 newToken.push('ghes')
171 return
172 }
173
174 // For the rest, we need to check the release string.
175
176 // E.g., [ 'currentVersion', '==', '"enterprise-server@3.0"'].
177 const opParts = op.split(' ')
178
179 if (!(opParts.length === 3 && opParts[0] === 'currentVersion')) {
180 console.error(`Something went wrong with ${token} in ${file}`)
181 process.exit(1)
182 }
183
184 const operator = opParts[1]
185 // Remove quotes around the version and then split it on the at sign.
186 const [plan, release] = opParts[2].slice(1, -1).split('@')
187
188 // Find the relevant version from the master list so we can access the short name.
189 const versionObj = allVersionKeys.find((version) => version.plan === plan)
190
191 if (!versionObj) {
192 console.error(`Couldn't find a version for ${plan} in "${token}" in ${file}`)
193 process.exit(1)
194 }
195
196 // Handle numbered releases!
197 if (versionObj.hasNumberedReleases) {
198 const newOperator = operatorsMap[operator]
199 if (!newOperator) {
200 console.error(
201 `Couldn't find an operator that corresponds to ${operator} in "${token} in "${file}`

Callers 1

mainFunction · 0.85

Calls 1

removeInputPropsFunction · 0.85

Tested by

no test coverage detected