forked from gravityblast/fresh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
152 lines (130 loc) · 2.72 KB
/
start.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
151
152
package runner
import (
"fmt"
"os"
"runtime"
"strings"
"syscall"
"time"
"github.com/jaschaephraim/lrserver"
)
var (
startChannel chan string
stopChannel chan bool
mainLog logFunc
watcherLog logFunc
runnerLog logFunc
buildLog logFunc
appLog logFunc
)
const html = `<!doctype html>
<html>
<head>
<title>Example</title>
</head>
<body>
<script src="http://localhost:35729/livereload.js"></script>
</body>
</html>`
func flushEvents() {
for {
select {
case eventName := <-startChannel:
mainLog("receiving event %s", eventName)
default:
return
}
}
}
func liveReload(e string, lr *lrserver.Server) {
if strings.Contains(e, ":"){
fname := strings.Split(e,":")[0]
es := fname[1:len(fname)-1]
mainLog("===========>LiveReload: %s", es)
lr.Reload(es)
}
}
func start() {
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
go lr.ListenAndServe()
loopIndex := 0
buildDelay := buildDelay()
started := false
go func() {
for {
loopIndex++
mainLog("Waiting (loop %d)...", loopIndex)
eventName := <-startChannel
mainLog("receiving first event %s", eventName)
mainLog("sleeping for %d milliseconds", buildDelay)
time.Sleep(buildDelay * time.Millisecond)
mainLog("flushing events")
flushEvents()
mainLog("Started! (%d Goroutines)", runtime.NumGoroutine())
err := removeBuildErrorsLog()
if err != nil {
mainLog(err.Error())
}
errorMessage, ok := build()
if !ok {
mainLog("Build Failed: \n %s", errorMessage)
if !started {
os.Exit(1)
}
createBuildErrorsLog(errorMessage)
} else {
if started {
stopChannel <- true
}
run()
liveReload(eventName, lr)
}
started = true
mainLog(strings.Repeat("-", 20))
}
}()
}
func init() {
startChannel = make(chan string, 1000)
stopChannel = make(chan bool)
}
func initLogFuncs() {
mainLog = newLogFunc("main")
watcherLog = newLogFunc("watcher")
runnerLog = newLogFunc("runner")
buildLog = newLogFunc("build")
appLog = newLogFunc("app")
}
func initLimit() {
var rLimit syscall.Rlimit
rLimit.Max = 10000
rLimit.Cur = 10000
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting Rlimit ", err)
}
}
func setEnvVars() {
os.Setenv("DEV_RUNNER", "1")
wd, err := os.Getwd()
if err == nil {
os.Setenv("RUNNER_WD", wd)
}
for k, v := range settings {
key := strings.ToUpper(fmt.Sprintf("%s%s", envSettingsPrefix, k))
os.Setenv(key, v)
}
}
// Watches for file changes in the root directory.
// After each file system event it builds and (re)starts the application.
func Start() {
initLimit()
initSettings()
initLogFuncs()
initFolders()
setEnvVars()
watch()
start()
startChannel <- "/"
<-make(chan int)
}