github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/buffalo/cmd/dev.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"os/exec"
     9  	"runtime"
    10  
    11  	"github.com/fatih/color"
    12  	"github.com/gobuffalo/buffalo/generators/assets/webpack"
    13  	rg "github.com/gobuffalo/buffalo/generators/refresh"
    14  	"github.com/markbates/refresh/refresh"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  // devCmd represents the dev command
    19  var devCmd = &cobra.Command{
    20  	Use:   "dev",
    21  	Short: "Runs your Buffalo app in 'development' mode",
    22  	Long: `Runs your Buffalo app in 'development' mode.
    23  This includes rebuilding your application when files change.
    24  This behavior can be changed in your .buffalo.dev.yml file.`,
    25  	Run: func(c *cobra.Command, args []string) {
    26  		if runtime.GOOS == "windows" {
    27  			color.NoColor = true
    28  		}
    29  		defer func() {
    30  			msg := "There was a problem starting the dev server, Please review the troubleshooting docs: %s\n"
    31  			cause := "Unknown"
    32  			if r := recover(); r != nil {
    33  				if err, ok := r.(error); ok {
    34  					cause = err.Error()
    35  				}
    36  			}
    37  			fmt.Printf(msg, cause)
    38  		}()
    39  		os.Setenv("GO_ENV", "development")
    40  		ctx := context.Background()
    41  		ctx, cancelFunc := context.WithCancel(ctx)
    42  		go func() {
    43  			err := startDevServer(ctx)
    44  			if err != nil {
    45  				cancelFunc()
    46  				log.Fatal(err)
    47  			}
    48  		}()
    49  		go func() {
    50  			err := startWebpack(ctx)
    51  			if err != nil {
    52  				cancelFunc()
    53  				log.Fatal(err)
    54  			}
    55  		}()
    56  		// wait for the ctx to finish
    57  		<-ctx.Done()
    58  	},
    59  }
    60  
    61  func startWebpack(ctx context.Context) error {
    62  	cfgFile := "./webpack.config.js"
    63  	_, err := os.Stat(cfgFile)
    64  	if err != nil {
    65  		// there's no webpack, so don't do anything
    66  		return nil
    67  	}
    68  	cmd := exec.Command(webpack.BinPath, "--watch")
    69  	cmd.Stdin = os.Stdin
    70  	cmd.Stderr = os.Stderr
    71  	cmd.Stdout = os.Stdout
    72  	return cmd.Run()
    73  }
    74  
    75  func startDevServer(ctx context.Context) error {
    76  	cfgFile := "./.buffalo.dev.yml"
    77  	_, err := os.Stat(cfgFile)
    78  	if err != nil {
    79  		g, err := rg.New()
    80  		if err != nil {
    81  			return err
    82  		}
    83  		err = g.Run("./", map[string]interface{}{
    84  			"name": "buffalo",
    85  		})
    86  	}
    87  	c := &refresh.Configuration{}
    88  	err = c.Load(cfgFile)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	r := refresh.NewWithContext(c, ctx)
    93  	return r.Start()
    94  }
    95  
    96  func init() {
    97  	RootCmd.AddCommand(devCmd)
    98  }