@@ -223,7 +223,7 @@ export default class GenerateTemplateFiles {
223
223
* Create every variation for the for the replacement keys
224
224
*/
225
225
private _getReplacers ( replacers : IReplacer [ ] , defaultCase : CaseConverterEnum ) : IReplacer [ ] {
226
- const caseTypes : string [ ] = Object . values ( CaseConverterEnum ) ;
226
+ const caseTypes : CaseConverterEnum [ ] = Object . values ( CaseConverterEnum ) ;
227
227
228
228
return replacers . reduce (
229
229
( previousReplacers : IReplacer [ ] , answeredReplacer : IReplacer ) : IReplacer [ ] => {
@@ -232,10 +232,10 @@ export default class GenerateTemplateFiles {
232
232
return [
233
233
...previousReplacers ,
234
234
...caseTypes . map (
235
- ( caseType : string ) : IReplacer => {
235
+ ( caseType : CaseConverterEnum ) : IReplacer => {
236
236
return {
237
237
slot : `${ slot } ${ caseType } ` ,
238
- slotValue : StringUtility . toCase ( slotValue , caseType as CaseConverterEnum ) ,
238
+ slotValue : StringUtility . toCase ( slotValue , caseType ) ,
239
239
} ;
240
240
}
241
241
) ,
@@ -329,6 +329,19 @@ export default class GenerateTemplateFiles {
329
329
) : Promise < string [ ] > {
330
330
const outputtedFilesAndFolders : string [ ] = [ ] ;
331
331
332
+ // Create a function to apply the transformations in one go
333
+ const regexEscape = ( text : string ) => text . replace ( / ( [ ^ a - z A - Z 0 - 9 _ ] ) / g, '\\$1' ) ;
334
+ const replacerLookup : Record < string , string > = { } ;
335
+ const replacerRegexBase = replacers
336
+ . map ( ( replacer : IReplacer ) => {
337
+ replacerLookup [ replacer . slot ] = replacer . slotValue ;
338
+ return regexEscape ( replacer . slot ) ;
339
+ } )
340
+ . join ( '|' ) ;
341
+ const replacerRegex = new RegExp ( `^${ replacerRegexBase } $` , 'g' ) ;
342
+ const replacer = ( text : string ) => text . replace ( replacerRegex , ( slot ) => replacerLookup [ slot ] ) ;
343
+
344
+ // Apply the transformations on all files recursively
332
345
const recursiveCopyOptions : any = {
333
346
overwrite : true ,
334
347
expand : false ,
@@ -351,12 +364,7 @@ export default class GenerateTemplateFiles {
351
364
} ,
352
365
transform : ( src : string , dest : string , stats : unknown ) => {
353
366
return through ( ( chunk : any , enc : any , done : any ) => {
354
- let output : string = chunk . toString ( ) ;
355
-
356
- replacers . forEach ( ( replacer : IReplacer ) => {
357
- output = replaceString ( output , replacer . slot , replacer . slotValue ) ;
358
- } ) ;
359
-
367
+ let output : string = replacer ( chunk . toString ( ) ) ;
360
368
done ( null , output ) ;
361
369
} ) ;
362
370
} ,
0 commit comments