forked from gobuffalo/buffalo
/
dev.go
98 lines (92 loc) · 2.09 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
package cmd
import (
"context"
"fmt"
"log"
"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/spf13/cobra"
)
// 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.`,
Run: func(c *cobra.Command, args []string) {
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()
}
}
fmt.Printf(msg, cause)
}()
os.Setenv("GO_ENV", "development")
ctx := context.Background()
ctx, cancelFunc := context.WithCancel(ctx)
go func() {
err := startDevServer(ctx)
if err != nil {
cancelFunc()
log.Fatal(err)
}
}()
go func() {
err := startWebpack(ctx)
if err != nil {
cancelFunc()
log.Fatal(err)
}
}()
// wait for the ctx to finish
<-ctx.Done()
},
}
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.Command(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 {
g, err := rg.New()
if err != nil {
return err
}
err = g.Run("./", map[string]interface{}{
"name": "buffalo",
})
}
c := &refresh.Configuration{}
err = c.Load(cfgFile)
if err != nil {
return err
}
r := refresh.NewWithContext(c, ctx)
return r.Start()
}
func init() {
RootCmd.AddCommand(devCmd)
}