@@ -193,7 +193,7 @@ export default class GenerateTemplateFiles {
193
193
* Create every variation for the for the replacement keys
194
194
*/
195
195
private _getReplacers ( replacers : IReplacer [ ] , defaultCase : CaseConverterEnum ) : IReplacer [ ] {
196
- const caseTypes : string [ ] = Object . values ( CaseConverterEnum ) ;
196
+ const caseTypes : CaseConverterEnum [ ] = Object . values ( CaseConverterEnum ) ;
197
197
198
198
return replacers . reduce (
199
199
( previousReplacers : IReplacer [ ] , answeredReplacer : IReplacer ) : IReplacer [ ] => {
@@ -202,10 +202,10 @@ export default class GenerateTemplateFiles {
202
202
return [
203
203
...previousReplacers ,
204
204
...caseTypes . map (
205
- ( caseType : string ) : IReplacer => {
205
+ ( caseType : CaseConverterEnum ) : IReplacer => {
206
206
return {
207
207
slot : `${ slot } ${ caseType } ` ,
208
- slotValue : StringUtility . toCase ( slotValue , caseType as CaseConverterEnum ) ,
208
+ slotValue : StringUtility . toCase ( slotValue , caseType ) ,
209
209
} ;
210
210
}
211
211
) ,
@@ -291,6 +291,19 @@ export default class GenerateTemplateFiles {
291
291
) : Promise < string [ ] > {
292
292
const outputtedFilesAndFolders : string [ ] = [ ] ;
293
293
294
+ // Create a function to apply the transformations in one go
295
+ const regexEscape = ( text : string ) => text . replace ( / ( [ ^ a - z A - Z 0 - 9 _ ] ) / g, '\\$1' ) ;
296
+ const replacerLookup : Record < string , string > = { } ;
297
+ const replacerRegexBase = replacers
298
+ . map ( ( replacer : IReplacer ) => {
299
+ replacerLookup [ replacer . slot ] = replacer . slotValue ;
300
+ return regexEscape ( replacer . slot ) ;
301
+ } )
302
+ . join ( '|' ) ;
303
+ const replacerRegex = new RegExp ( `^${ replacerRegexBase } $` , 'g' ) ;
304
+ const replacer = ( text : string ) => text . replace ( replacerRegex , ( slot ) => replacerLookup [ slot ] ) ;
305
+
306
+ // Apply the transformations on all files recursively
294
307
const recursiveCopyOptions : any = {
295
308
overwrite : true ,
296
309
expand : false ,
@@ -313,12 +326,7 @@ export default class GenerateTemplateFiles {
313
326
} ,
314
327
transform : ( src : string , dest : string , stats : unknown ) => {
315
328
return through ( ( chunk : any , enc : any , done : any ) => {
316
- let output : string = chunk . toString ( ) ;
317
-
318
- replacers . forEach ( ( replacer : IReplacer ) => {
319
- output = replaceString ( output , replacer . slot , replacer . slotValue ) ;
320
- } ) ;
321
-
329
+ let output : string = replacer ( chunk . toString ( ) ) ;
322
330
done ( null , output ) ;
323
331
} ) ;
324
332
} ,
0 commit comments