github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/fix.go (about) 1 package fix 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "os/exec" 8 "strings" 9 10 "github.com/JarrahG/buffalocli/internal/runtime" 11 "github.com/gobuffalo/events" 12 "github.com/gobuffalo/genny/v2" 13 ) 14 15 func ask(q string) bool { 16 fmt.Printf("? %s [y/n] ", q) 17 18 reader := bufio.NewReader(os.Stdin) 19 text, _ := reader.ReadString('\n') 20 21 text = strings.ToLower(strings.TrimSpace(text)) 22 return text == "y" || text == "yes" 23 } 24 25 func printWarnings(opts *Options) genny.RunFn { 26 return func(r *genny.Runner) error { 27 if len(opts.warnings) == 0 { 28 return nil 29 } 30 31 fmt.Println("\n\n----------------------------") 32 fmt.Printf("!!! (%d) Warnings Were Found !!!\n\n", len(opts.warnings)) 33 for _, w := range opts.warnings { 34 fmt.Printf("[WARNING]: %s\n", w) 35 } 36 return nil 37 } 38 } 39 40 func tidyCmd() *exec.Cmd { 41 cmd := exec.Command("go", "mod", "tidy") 42 cmd.Stderr = os.Stderr 43 return cmd 44 } 45 46 func New(opts *Options) (*genny.Generator, error) { 47 g := genny.New() 48 49 if err := opts.Validate(); err != nil { 50 return g, err 51 } 52 53 fmt.Printf("! This updater will attempt to update your application to Buffalo version: %s\n", runtime.Version) 54 if !opts.YesToAll && !ask("Do you wish to continue?") { 55 fmt.Println("~~~ cancelling update ~~~") 56 return g, nil 57 } 58 59 g.ErrorFn = func(err error) { 60 events.EmitError(EvtFixStopErr, err, events.Payload{"opts": opts}) 61 } 62 63 g.RunFn(func(r *genny.Runner) error { 64 events.EmitPayload(EvtFixStart, events.Payload{"opts": opts}) 65 return nil 66 }) 67 68 // ExceptFS will not scan any files that the path starts with given strings 69 // excluded files include such as `.git/`, `.github/`, `.gitignore`, `.yarn/`, `.yarnrc`, etc. 70 if err := g.ExceptFS(os.DirFS(opts.App.Root), []string{"node_modules", ".git", ".yarn"}, nil); err != nil { 71 return nil, err 72 } 73 74 // structural fixes go first so the others can use the expected structure. 75 g.RunFn(MoveMain(opts)) 76 g.RunFn(Refresh(opts)) 77 78 g.RunFn(ReplaceAppOnce(opts)) 79 80 // replace old imports with new ones 81 g.RunFn(RewriteImports(opts)) 82 g.Command(tidyCmd()) 83 84 // check webpack.config.json and package.json for updates 85 g.RunFn(WebpackCheck(opts)) 86 g.RunFn(PackageJSONCheck(opts)) 87 g.RunFn(AddPackageJSONScripts(opts)) 88 89 // install required tools 90 g.RunFn(InstallTools(opts)) 91 g.Command(tidyCmd()) 92 93 // check for deprecations 94 g.RunFn(DeprecationsCheck(opts)) 95 96 // add embed.go files 97 g.RunFn(FixEmbed(opts)) 98 99 // fix Docker file 100 g.RunFn(FixDocker(opts)) 101 102 g.RunFn(EncodeAppToml(opts)) 103 104 // update plugins 105 g.RunFn(RemoveOldPlugins(opts)) 106 g.RunFn(CleanPluginCache) 107 g.RunFn(ReinstallPlugins(opts)) 108 109 // update plush templates 110 g.RunFn(UpdatePlushTemplates(opts)) 111 112 // print all warnings that were captured 113 g.RunFn(printWarnings(opts)) 114 115 g.RunFn(func(r *genny.Runner) error { 116 events.EmitPayload(EvtFixStop, events.Payload{"opts": opts}) 117 return nil 118 }) 119 return g, nil 120 }