Skip to content

Commit 2dbfef8

Browse files
mattpollardpeterbe
andauthored
Improve YAML file handling for script to toggle lightweight feature flags for GitHub AE (github#27170)
* Refactor for YAML files * Accommodate strings in YAML files * feedbacked Co-authored-by: Peter Bengtsson <[email protected]>
1 parent cd1d86a commit 2dbfef8

File tree

1 file changed

+75
-36
lines changed

1 file changed

+75
-36
lines changed

script/toggle-ghae-feature-flags.js

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,64 @@ const allFiles = [...markdownFiles, ...yamlFiles]
9898
// Liquid conditionals.
9999

100100
const allFlags = {}
101+
const liquidShowRegExp = new RegExp(`\\s+${plan}-([^\\s]+)`, 'g')
101102

102103
console.log(`Parsing all flags for ${plan} plan on ${currentBranch} branch...`)
103104

104105
allFiles.forEach((file) => {
105106
const fileContent = fs.readFileSync(file, 'utf8')
106-
const { data } = frontmatter(fileContent)
107+
const matches = []
107108

108-
// Process YAML front matter and data files.
109+
if (file.endsWith('.md')) {
110+
const { data } = frontmatter(fileContent)
109111

110-
if (data.versions && data.versions[plan] && data.versions[plan] !== '*') {
111-
if (!allFlags[data.versions[plan]]) {
112-
allFlags[data.versions[plan]] = []
113-
}
112+
// Process YAML front matter.
114113

115-
allFlags[data.versions[plan]].push(file)
116-
}
114+
if (data.versions && data.versions[plan] && data.versions[plan] !== '*') {
115+
if (!allFlags[data.versions[plan]]) {
116+
allFlags[data.versions[plan]] = []
117+
}
117118

118-
// Process Liquid conditionals in Markdown source.
119+
allFlags[data.versions[plan]].push(file)
120+
}
119121

120-
const liquidShowRegExp = new RegExp(`\\s${plan}-([^\\s]+)`, 'g')
121-
const deduplicatedMatches = [...new Set(fileContent.match(liquidShowRegExp))]
122+
// Process Liquid conditionals in Markdown source.
122123

123-
if (deduplicatedMatches.length > 0) {
124-
const matches = deduplicatedMatches.map((match) => match.trim().replace(plan + '-', ''))
124+
const deduplicatedMatches = [...new Set(fileContent.match(liquidShowRegExp))]
125125

126-
matches.forEach((match) => {
127-
if (!allFlags[match]) {
128-
allFlags[match] = []
129-
}
126+
if (deduplicatedMatches.length > 0) {
127+
matches.push(...deduplicatedMatches.map((match) => match.trim().replace(plan + '-', '')))
128+
}
129+
} else if (file.endsWith('.yml')) {
130+
// Process versions in YAML files for feature-based versioning.
131+
132+
const yamlShowRegExp = new RegExp(`${plan}: ['"]([A-Za-z0-9-_]+)['"]`, 'g')
133+
const deduplicatedLiquidMatches = [...new Set(fileContent.match(yamlShowRegExp))]
134+
const deduplicatedVersionMatches = [...new Set(fileContent.match(liquidShowRegExp))]
135+
136+
if (deduplicatedLiquidMatches.length > 0) {
137+
matches.push(
138+
...deduplicatedLiquidMatches.map((match) =>
139+
match.trim().replace(`${plan}: `, '').replace(/['"]+/g, '')
140+
)
141+
)
142+
}
130143

131-
allFlags[match].push(file)
132-
})
144+
if (deduplicatedVersionMatches.length > 0) {
145+
matches.push(
146+
...deduplicatedVersionMatches.map((match) => match.trim().replace(plan + '-', ''))
147+
)
148+
}
149+
} else {
150+
throw new Error(`Unrecognized file (${file}). Not a .md or .yml file.`)
133151
}
152+
153+
matches.forEach((match) => {
154+
if (!allFlags[match]) {
155+
allFlags[match] = []
156+
}
157+
allFlags[match].push(file)
158+
})
134159
})
135160

136161
// Output flags and lists of files that contain the flags.
@@ -165,34 +190,49 @@ if (options.showFlags) {
165190
console.log(`Toggling flag${flagCount > 1 ? 's' : ''} (${flagsToToggle.join(', ')})...`)
166191

167192
flagsToToggle.forEach((flag) => {
168-
allFiles.forEach((file) => {
193+
if (!(flag in allFlags)) {
194+
console.warn(`${flag} does not exist in source`)
195+
return
196+
}
197+
198+
allFlags[flag].forEach((file) => {
169199
const fileContent = fs.readFileSync(file, 'utf8')
170200
const { data } = frontmatter(fileContent)
171-
172201
const liquidReplacementRegExp = new RegExp(`${plan}-${flag}`, 'g')
202+
let newContent
173203

174-
// Update versions in Liquid conditionals.
204+
if (file.endsWith('.md')) {
205+
// Update versions in Liquid conditionals.
175206

176-
const newContent = fileContent.replace(liquidReplacementRegExp, plan)
207+
newContent = fileContent.replace(liquidReplacementRegExp, plan)
177208

178-
if (data.versions && data.versions[plan] === flag) {
179-
// Update versions in YAML files and content files with YAML front
180-
// matter.
209+
if (data.versions && data.versions[plan] === flag) {
210+
// Update versions in content files with YAML front matter.
181211

182-
data.versions[plan] = '*'
183-
}
212+
data.versions[plan] = '*'
213+
}
214+
fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
215+
} else if (file.endsWith('.yml')) {
216+
const yamlReplacementRegExp = new RegExp(`${plan}: ['"]+${flag}['"]+`, 'g')
217+
218+
// Update versions in YAML files for feature-based versioning.
219+
220+
newContent = fileContent.replace(yamlReplacementRegExp, `${plan}: '*'`)
184221

185-
fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
222+
// Update versions in Liquid conditionals.
223+
224+
newContent = newContent.replace(liquidReplacementRegExp, plan)
225+
fs.writeFileSync(file, newContent, 'utf-8')
226+
} else {
227+
throw new Error(`Unknown file to toggle (${file}). Not a .yml or .md file.`)
228+
}
186229
})
187230

188231
let filesUpdatedForFlag = 0
189232

190-
if (!(flag in allFlags)) {
191-
console.log(`Warning: ${flag} does not exist in source`)
192-
}
193-
194-
if (allFlags[flag]) {
233+
if (allFlags[flag].length) {
195234
console.log(`Toggled ${flag}. Committing changes...`)
235+
196236
allFlags[flag].forEach((fileToAdd) => {
197237
execSync(`git add ${fileToAdd}`)
198238
filesUpdatedForFlag++
@@ -219,9 +259,8 @@ if (options.showFlags) {
219259
execSync(`git checkout --quiet ${contentDir} ${reusablesDir} ${dataDir}`)
220260
})
221261

222-
console.log('Done!')
223-
224262
if (commitCount > 0) {
263+
console.log('Done!')
225264
console.log(' - Review commits:')
226265
console.log(` git log -n ${commitCount}`)
227266
console.log(' - Review changes in diffs:')

0 commit comments

Comments
 (0)