@@ -40,11 +40,15 @@ export type PatchCodeFn = (args: {
4040 manifests : ReturnType < typeof getManifests > ;
4141} ) => Promise < string > ;
4242
43+ interface IndividualPatch {
44+ pathFilter : RegExp ;
45+ contentFilter ?: RegExp ;
46+ patchCode : PatchCodeFn ;
47+ }
48+
4349export interface CodePatcher {
4450 name : string ;
45- pathFilter : RegExp | VersionedField < RegExp > [ ] ;
46- contentFilter ?: RegExp | VersionedField < RegExp > [ ] ;
47- patchCode : PatchCodeFn | VersionedField < PatchCodeFn > [ ] ;
51+ patches : IndividualPatch | VersionedField < IndividualPatch > [ ] ;
4852}
4953
5054export function extractVersionedField < T > (
@@ -83,31 +87,34 @@ export async function applyCodePatches(
8387) {
8488 const nextVersion = buildOptions . nextVersion ;
8589 logger . time ( "Applying code patches" ) ;
90+
91+ // We first filter against the version
92+ // We also flatten the array of patches so that we get both the name and all the necessary patches
93+ const patchesToApply = codePatcher . flatMap ( ( { name, patches } ) =>
94+ Array . isArray ( patches )
95+ ? extractVersionedField ( patches , nextVersion ) . map ( ( patch ) => ( {
96+ name,
97+ patch,
98+ } ) )
99+ : [ { name, patch : patches } ] ,
100+ ) ;
101+
86102 await Promise . all (
87103 tracedFiles . map ( async ( filePath ) => {
88104 // We check the filename against the filter to see if we should apply the patch
89- const patchMatchingPath = codePatcher . filter ( ( patch ) => {
90- const filters = Array . isArray ( patch . pathFilter )
91- ? extractVersionedField ( patch . pathFilter , nextVersion )
92- : [ patch . pathFilter ] ;
93- return filters . some ( ( filter ) => filePath . match ( filter ) ) ;
105+ const patchMatchingPath = patchesToApply . filter ( ( { patch } ) => {
106+ return filePath . match ( patch . pathFilter ) ;
94107 } ) ;
95108 if ( patchMatchingPath . length === 0 ) {
96109 return ;
97110 }
98111 const content = await fs . readFile ( filePath , "utf-8" ) ;
99112 // We filter a last time against the content this time
100- const patchToApply = patchMatchingPath . filter ( ( patch ) => {
113+ const patchToApply = patchMatchingPath . filter ( ( { patch } ) => {
101114 if ( ! patch . contentFilter ) {
102115 return true ;
103116 }
104- const contentFilters = Array . isArray ( patch . contentFilter )
105- ? extractVersionedField ( patch . contentFilter , nextVersion )
106- : [ patch . contentFilter ] ;
107- return contentFilters . some ( ( filter ) =>
108- // If there is no filter, we just return true to apply the patch
109- filter ? content . match ( filter ) : true ,
110- ) ;
117+ return content . match ( patch . contentFilter ) ;
111118 } ) ;
112119 if ( patchToApply . length === 0 ) {
113120 return ;
@@ -116,21 +123,14 @@ export async function applyCodePatches(
116123 // We apply the patches
117124 let patchedContent = content ;
118125
119- for ( const patch of patchToApply ) {
120- const patchCodeFns = Array . isArray ( patch . patchCode )
121- ? extractVersionedField ( patch . patchCode , nextVersion )
122- : [ patch . patchCode ] ;
123- logger . debug (
124- `Applying ${ patchCodeFns . length } patches to ${ filePath } for ${ patch . name } ` ,
125- ) ;
126- for ( const patchCodeFn of patchCodeFns ) {
127- patchedContent = await patchCodeFn ( {
128- code : patchedContent ,
129- filePath,
130- tracedFiles,
131- manifests,
132- } ) ;
133- }
126+ for ( const { patch, name } of patchToApply ) {
127+ logger . debug ( `Applying code patch: ${ name } to ${ filePath } ` ) ;
128+ patchedContent = await patch . patchCode ( {
129+ code : patchedContent ,
130+ filePath,
131+ tracedFiles,
132+ manifests,
133+ } ) ;
134134 }
135135 await fs . writeFile ( filePath , patchedContent ) ;
136136 } ) ,
0 commit comments