forked from gobuffalo/buffalo
/
dev.go
150 lines (131 loc) · 3.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/gobuffalo/buffalo/genny/assets/webpack"
rg "github.com/gobuffalo/buffalo/genny/refresh"
"github.com/gobuffalo/events"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/meta"
"github.com/markbates/refresh/refresh"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
func init() {
events.NamedListen("buffalo:dev", func(e events.Event) {
if strings.HasPrefix(e.Kind, "refresh:") {
e.Kind = strings.Replace(e.Kind, "refresh:", "buffalo:dev:", 1)
events.Emit(e)
}
})
}
var devOptions = struct {
Debug bool
}{}
// devCmd represents the dev command
var devCmd = &cobra.Command{
Use: "dev",
Short: "Run the Buffalo app in 'development' mode",
Long: `Run the Buffalo app in 'development' mode.
This includes rebuilding the application when files change.
This behavior can be changed in .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, args)
})
wg.Go(func() error {
app := meta.New(".")
if !app.WithNodeJs {
// No need to run dev script
return nil
}
return runDevScript(ctx, app)
})
err := wg.Wait()
if err != context.Canceled {
return err
}
return nil
},
}
func runDevScript(ctx context.Context, app meta.App) error {
tool := "yarnpkg"
if !app.WithYarn {
tool = "npm"
}
if _, err := exec.LookPath(tool); err != nil {
return fmt.Errorf("could not find %s tool", tool)
}
// make sure that the node_modules folder is properly "installed"
if _, err := os.Stat(filepath.Join(app.Root, "node_modules")); err != nil {
cmd := exec.CommandContext(ctx, tool, "install")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
}
cmd := exec.CommandContext(ctx, tool, "run", "dev")
if _, err := app.NodeScript("dev"); err != nil {
// Fallback on legacy runner
cmd = exec.CommandContext(ctx, webpack.BinPath, "--watch")
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func startDevServer(ctx context.Context, args []string) error {
app := meta.New(".")
cfgFile := "./.buffalo.dev.yml"
if _, err := os.Stat(cfgFile); err != nil {
run := genny.WetRunner(ctx)
err = run.WithNew(rg.New(&rg.Options{App: app}))
if err != nil {
return err
}
if err := run.Run(); err != nil {
return err
}
}
c := &refresh.Configuration{}
if err := c.Load(cfgFile); err != nil {
return err
}
c.Debug = devOptions.Debug
bt := app.BuildTags("development")
if len(bt) > 0 {
c.BuildFlags = append(c.BuildFlags, "-tags", bt.String())
}
r := refresh.NewWithContext(c, ctx)
r.CommandFlags = args
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)
}