github.com/singlemusic/buffalo@v0.16.30/buffalo/cmd/fix/webpack.go (about)

     1  package fix
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"html/template"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/gobuffalo/buffalo/genny/assets/webpack"
    11  )
    12  
    13  // WebpackCheck will compare the current default Buffalo
    14  // webpack.config.js against the applications webpack.config.js. If they are
    15  // different you have the option to overwrite the existing webpack.config.js
    16  // file with the new one.
    17  func WebpackCheck(r *Runner) error {
    18  	fmt.Println("~~~ Checking webpack.config.js ~~~")
    19  
    20  	if !r.App.WithWebpack {
    21  		return nil
    22  	}
    23  
    24  	box := webpack.Templates
    25  
    26  	f, err := box.FindString("webpack.config.js.tmpl")
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	tmpl, err := template.New("webpack").Parse(f)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	bb := &bytes.Buffer{}
    37  	err = tmpl.Execute(bb, map[string]interface{}{
    38  		"opts": &webpack.Options{
    39  			App: r.App,
    40  		},
    41  	})
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	b, err := ioutil.ReadFile("webpack.config.js")
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	if string(b) == bb.String() {
    52  		return nil
    53  	}
    54  
    55  	if !ask("Your webpack.config.js file is different from the latest Buffalo template.\nWould you like to replace yours with the latest template?") {
    56  		fmt.Println("\tSkipping webpack.config.js")
    57  		return nil
    58  	}
    59  
    60  	wf, err := os.Create("webpack.config.js")
    61  	if err != nil {
    62  		return err
    63  	}
    64  	_, err = wf.Write(bb.Bytes())
    65  	if err != nil {
    66  		return err
    67  	}
    68  	return wf.Close()
    69  }