1- const fs = require ( 'fs' )
2- const glob = require ( 'glob' )
1+ const patch = require ( 'mpatch' )
32const inquirer = require ( 'inquirer' )
43const makeRecipes = require ( './recipes' )
54
6- const debug = console . log
7-
8- function checkPatch ( text , patch ) {
9- return [ text , patch ]
10- . map ( x => x . replace ( / \s + / g, ' ' ) )
11- . reduce ( ( t , p ) => t . includes ( p ) )
12- }
13-
14- /**
15- * @param {string } text
16- * @param {Recipe } recipe
17- * @return {?string }
18- */
19- function applyRecipe ( text , recipe ) {
20- if ( ! text ) {
21- return text
22- }
23-
24- if ( checkPatch ( text , recipe . patch ) ) {
25- debug ( 'already patched, skipped' )
26- return null
27- }
28-
29- const matched = text . match ( recipe . pattern )
30-
31- if ( ! matched ) {
32- debug ( `not found ${ recipe . pattern } ` )
33- return text
34- }
35-
36- return text . replace ( matched [ 0 ] , `${ matched [ 0 ] } ${ recipe . patch } ` )
37- }
38-
39- /**
40- * @param {string } file
41- * @param {Recipe[] } recipes
42- */
43- function patch ( file , recipes ) {
44- debug ( `patching ${ file } ...` )
45-
46- const path = glob . sync ( file , {
47- ignore : [ 'node_modules/**' , '**/build/**' ]
48- } ) [ 0 ]
49-
50- const init = fs . readFileSync ( path , 'utf8' )
51- const text = recipes . reduce ( applyRecipe , init )
52-
53- if ( text != null ) {
54- fs . writeFileSync ( path , text )
55- }
56- }
57-
585const questions = [
596 {
607 type : 'input' ,
@@ -69,5 +16,5 @@ inquirer.prompt(questions).then(answers => {
6916 patch ( file , [ ] . concat ( recipes [ file ] ) )
7017 } )
7118
72- debug ( 'patch done!')
19+ console . log ( ' done!')
7320} )
0 commit comments