github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/updater/webpack.go (about)

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