forked from cortesi/devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
62 lines (56 loc) · 1.53 KB
/
watch.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
package devd
import (
"time"
"github.com/cortesi/devd/livereload"
"github.com/cortesi/modd"
"github.com/cortesi/termlog"
)
const batchTime = time.Millisecond * 200
// Watch watches an endpoint for changes, if it supports them.
func (r Route) Watch(ch chan []string, excludePatterns []string, log termlog.Logger) error {
switch r.Endpoint.(type) {
case *filesystemEndpoint:
ep := *r.Endpoint.(*filesystemEndpoint)
modchan := make(chan modd.Mod, 1)
err := modd.Watch([]string{string(ep)}, excludePatterns, batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
ch <- mod.All()
}
}()
}
return nil
}
// WatchPaths watches a set of paths, and broadcasts changes through reloader.
func WatchPaths(paths, excludePatterns []string, reloader livereload.Reloader, log termlog.Logger) error {
ch := make(chan []string, 1)
for _, path := range paths {
modchan := make(chan modd.Mod, 1)
err := modd.Watch([]string{path}, excludePatterns, batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
ch <- mod.All()
}
}()
}
go reloader.Watch(ch)
return nil
}
// WatchRoutes watches the route collection, and broadcasts changes through reloader.
func WatchRoutes(routes RouteCollection, reloader livereload.Reloader, excludePatterns []string, log termlog.Logger) error {
c := make(chan []string, 1)
for i := range routes {
err := routes[i].Watch(c, excludePatterns, log)
if err != nil {
return err
}
}
go reloader.Watch(c)
return nil
}