github.com/fasmat/buffalo@v0.11.0/generators/assets/webpack/webpack.go (about) 1 package webpack 2 3 import ( 4 "os/exec" 5 "path/filepath" 6 7 "github.com/gobuffalo/buffalo/generators" 8 "github.com/gobuffalo/buffalo/generators/assets" 9 "github.com/gobuffalo/buffalo/generators/assets/standard" 10 "github.com/gobuffalo/makr" 11 "github.com/gobuffalo/packr" 12 "github.com/pkg/errors" 13 "github.com/sirupsen/logrus" 14 ) 15 16 // TemplateBox contains all templates needed for the webpack generator 17 var TemplateBox = packr.NewBox("../webpack/templates") 18 19 var logo = &makr.RemoteFile{ 20 File: makr.NewFile("assets/images/logo.svg", ""), 21 RemotePath: assets.LogoURL, 22 } 23 24 // BinPath is the path to the local install of webpack 25 var BinPath = filepath.Join("node_modules", ".bin", "webpack") 26 27 // Run webpack generator 28 func (w Generator) Run(root string, data makr.Data) error { 29 g := makr.New() 30 31 // if there's no npm, return! 32 if _, err := exec.LookPath("npm"); err != nil { 33 logrus.Info("Could not find npm. Skipping webpack generation.") 34 35 return standard.Run(root, data) 36 } 37 38 command := "yarn" 39 40 if !w.WithYarn { 41 command = "npm" 42 } else { 43 err := installYarn(data) 44 if err != nil { 45 return errors.WithStack(err) 46 } 47 } 48 49 g.Add(logo) 50 51 files, err := generators.FindByBox(TemplateBox) 52 if err != nil { 53 return errors.WithStack(err) 54 } 55 56 for _, f := range files { 57 g.Add(makr.NewFile(f.WritePath, f.Body)) 58 } 59 60 args := []string{"install", "--no-progress", "--save"} 61 g.Add(makr.NewCommand(exec.Command(command, args...))) 62 data["opts"] = w 63 return g.Run(root, data) 64 } 65 66 func installYarn(data makr.Data) error { 67 // if there's no yarn, install it! 68 _, err := exec.LookPath("yarn") 69 // A new makr is necessary to have yarn available in path 70 if err != nil { 71 yg := makr.New() 72 yargs := []string{"install", "-g", "yarn"} 73 yg.Add(makr.NewCommand(exec.Command("npm", yargs...))) 74 err = yg.Run(".", data) 75 if err != nil { 76 return err 77 } 78 } 79 return nil 80 }