@@ -98,39 +98,64 @@ const allFiles = [...markdownFiles, ...yamlFiles]
98
98
// Liquid conditionals.
99
99
100
100
const allFlags = { }
101
+ const liquidShowRegExp = new RegExp ( `\\s+${ plan } -([^\\s]+)` , 'g' )
101
102
102
103
console . log ( `Parsing all flags for ${ plan } plan on ${ currentBranch } branch...` )
103
104
104
105
allFiles . forEach ( ( file ) => {
105
106
const fileContent = fs . readFileSync ( file , 'utf8' )
106
- const { data } = frontmatter ( fileContent )
107
+ const matches = [ ]
107
108
108
- // Process YAML front matter and data files.
109
+ if ( file . endsWith ( '.md' ) ) {
110
+ const { data } = frontmatter ( fileContent )
109
111
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.
114
113
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
+ }
117
118
118
- // Process Liquid conditionals in Markdown source.
119
+ allFlags [ data . versions [ plan ] ] . push ( file )
120
+ }
119
121
120
- const liquidShowRegExp = new RegExp ( `\\s${ plan } -([^\\s]+)` , 'g' )
121
- const deduplicatedMatches = [ ...new Set ( fileContent . match ( liquidShowRegExp ) ) ]
122
+ // Process Liquid conditionals in Markdown source.
122
123
123
- if ( deduplicatedMatches . length > 0 ) {
124
- const matches = deduplicatedMatches . map ( ( match ) => match . trim ( ) . replace ( plan + '-' , '' ) )
124
+ const deduplicatedMatches = [ ...new Set ( fileContent . match ( liquidShowRegExp ) ) ]
125
125
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
+ }
130
143
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.` )
133
151
}
152
+
153
+ matches . forEach ( ( match ) => {
154
+ if ( ! allFlags [ match ] ) {
155
+ allFlags [ match ] = [ ]
156
+ }
157
+ allFlags [ match ] . push ( file )
158
+ } )
134
159
} )
135
160
136
161
// Output flags and lists of files that contain the flags.
@@ -165,34 +190,49 @@ if (options.showFlags) {
165
190
console . log ( `Toggling flag${ flagCount > 1 ? 's' : '' } (${ flagsToToggle . join ( ', ' ) } )...` )
166
191
167
192
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 ) => {
169
199
const fileContent = fs . readFileSync ( file , 'utf8' )
170
200
const { data } = frontmatter ( fileContent )
171
-
172
201
const liquidReplacementRegExp = new RegExp ( `${ plan } -${ flag } ` , 'g' )
202
+ let newContent
173
203
174
- // Update versions in Liquid conditionals.
204
+ if ( file . endsWith ( '.md' ) ) {
205
+ // Update versions in Liquid conditionals.
175
206
176
- const newContent = fileContent . replace ( liquidReplacementRegExp , plan )
207
+ newContent = fileContent . replace ( liquidReplacementRegExp , plan )
177
208
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.
181
211
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 } : '*'` )
184
221
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
+ }
186
229
} )
187
230
188
231
let filesUpdatedForFlag = 0
189
232
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 ) {
195
234
console . log ( `Toggled ${ flag } . Committing changes...` )
235
+
196
236
allFlags [ flag ] . forEach ( ( fileToAdd ) => {
197
237
execSync ( `git add ${ fileToAdd } ` )
198
238
filesUpdatedForFlag ++
@@ -219,9 +259,8 @@ if (options.showFlags) {
219
259
execSync ( `git checkout --quiet ${ contentDir } ${ reusablesDir } ${ dataDir } ` )
220
260
} )
221
261
222
- console . log ( 'Done!' )
223
-
224
262
if ( commitCount > 0 ) {
263
+ console . log ( 'Done!' )
225
264
console . log ( ' - Review commits:' )
226
265
console . log ( ` git log -n ${ commitCount } ` )
227
266
console . log ( ' - Review changes in diffs:' )
0 commit comments