-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (76 loc) · 1.9 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
// The `webglrun` command provides a simple tool to rebuild wasm programs using
// the scripts/make.sh build helper.
//
// The program expects to be executed from the root project folder. A simple
// invocation can be executed with:
//
// go run cmd/webglrun/main.go
package main
import (
"io/fs"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func main() {
log.Print("Watching for file changes ... ")
go watchForChanges()
_, err := exec.Command("xdg-open", "http://localhost:8000/").CombinedOutput()
log.Printf("Launching browser (err=%v)", err)
log.Print("Starting server for wasmrun ...")
http.Handle("/", http.FileServer(http.Dir("./")))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func watchForChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
lastBuild := time.Now()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Printf("Event: %#v", event)
if strings.HasSuffix(event.Name, ".go") {
if time.Since(lastBuild) < 100*time.Millisecond {
log.Printf("Not rebuilding since lastBuild is %v ago", time.Since(lastBuild))
continue
}
b, err := exec.Command("scripts/make.sh", "js", "wasm").CombinedOutput()
if err != nil {
log.Printf("Error: %v", err)
} else {
log.Printf("Build output: %v", string(b))
}
lastBuild = time.Now()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("Error: %v", err)
}
}
}()
wd, err := os.Getwd()
if err != nil {
panic(err)
}
log.Printf("Watching for changes at '%v'", wd)
filepath.Walk(wd, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() && !strings.Contains(path, "/.git") && !strings.Contains(path, "/build") {
log.Printf("> Added %v", path)
return watcher.Add(path)
}
return nil
})
}