forked from cortesi/devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
74 lines (68 loc) · 1.91 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
63
64
65
66
67
68
69
70
71
72
73
74
package devd
import (
"time"
"github.com/cortesi/devd/livereload"
"github.com/cortesi/moddwatch"
"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 *moddwatch.Mod, 1)
_, err := moddwatch.Watch([]string{ep.Root + "/..."}, batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
filteredMod, err := mod.Filter([]string{"**/*"}, excludePatterns)
if err != nil {
log.Shout("Error filtering watches: %s", err)
}
if !filteredMod.Empty() {
ch <- filteredMod.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 *moddwatch.Mod, 1)
_, err := moddwatch.Watch([]string{path}, batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
filteredMod, err := mod.Filter([]string{"**/*"}, excludePatterns)
if err != nil {
log.Shout("Error filtering watches: %s", err)
}
if !filteredMod.Empty() {
ch <- filteredMod.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
}