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