@@ -14,27 +14,34 @@ function checkPatch (text, patch) {
1414/**
1515 * @param {string } text
1616 * @param {Recipe } recipe
17- * @return {string? }
17+ * @return {?string }
1818 */
1919function applyRecipe ( text , recipe ) {
20- if ( ! text || checkPatch ( text , recipe . patch ) ) {
20+ if ( ! text ) {
21+ return text
22+ }
23+
24+ if ( checkPatch ( text , recipe . patch ) ) {
25+ debug ( 'already patched, skipped' )
2126 return null
2227 }
2328
2429 const matched = text . match ( recipe . pattern )
25- if ( matched && matched . length === 1 ) {
26- return text . replace ( matched [ 0 ] , `${ matched [ 0 ] } ${ recipe . patch } ` )
30+
31+ if ( ! matched ) {
32+ debug ( `not found ${ recipe . pattern } ` )
33+ return text
2734 }
2835
29- return text
36+ return text . replace ( matched [ 0 ] , ` ${ matched [ 0 ] } ${ recipe . patch } ` )
3037}
3138
3239/**
3340 * @param {string } file
3441 * @param {(Recipe|Recipe[]) } recipes
3542 */
3643function patch ( file , recipes ) {
37- debug ( `patching ${ file } ` )
44+ debug ( `patching ${ file } ... ` )
3845
3946 const path = glob . sync ( file , {
4047 ignore : [ 'node_modules/**' , '**/build/**' ]
@@ -43,7 +50,7 @@ function patch (file, recipes) {
4350 const init = fs . readFileSync ( path , 'utf8' )
4451 const text = recipes . reduce ( applyRecipe , init )
4552
46- if ( text ) {
53+ if ( text != null ) {
4754 fs . writeFileSync ( path , text )
4855 }
4956}
0 commit comments