-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
87 lines (74 loc) · 1.64 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/sergei-svistunov/go-ssr/internal/boilerplate"
"github.com/sergei-svistunov/go-ssr/internal/config"
"github.com/sergei-svistunov/go-ssr/internal/generator"
)
var (
fInit = flag.Bool("init", false, "Initialize GoSSR config")
fPackageName = flag.String("pkg-name", "gossr/app", "Using with -init flag")
fWatch = flag.Bool("watch", false, "Watch project files for changes and rebuild the project")
fProd = flag.Bool("prod", false, "Build static files for production")
)
func main() {
flag.Parse()
if *fInit {
if err := boilerplate.Init(*fPackageName, "internal/web"); err != nil {
fatal(err)
}
fmt.Println("GoSSR project was initialized")
}
cfg, err := config.Read()
if err != nil {
fatal(err)
}
if *fProd {
cfg.Prod = true
}
g := generator.New(cfg)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := g.Webpack(); err != nil {
fatal(err)
}
if err := g.Analyze(); err != nil {
fatal(err)
}
if err := g.Generate(); err != nil {
fatal(err)
}
if *fInit {
cmd := exec.Command("go", "mod", "tidy")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fatal(err)
}
}
if *fWatch {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigChan
g.Shutdown()
cancel()
}()
if err := g.Watch(ctx); err != nil {
cancel()
fatal(err)
}
<-ctx.Done()
}
}
func fatal(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}