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