forked from cortesi/devd
/
watch.go
91 lines (83 loc) · 2.32 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package devd
import (
"time"
"github.com/bmatcuk/doublestar"
"github.com/cortesi/devd/livereload"
"github.com/cortesi/devd/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(ep), batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
files := filterFiles(mod.All(), excludePatterns, log)
ch <- files
}
}()
}
return nil
}
// Determine if a file should be included, based on the given exclude paths.
func shouldInclude(file string, excludePatterns []string, log termlog.Logger) bool {
for _, pattern := range excludePatterns {
match, err := doublestar.Match(pattern, file)
if err != nil {
log.Warn("Error matching pattern '%s': %s", pattern, err)
} else if match {
return false
}
}
return true
}
// Filter out the files that match the given exclude patterns.
func filterFiles(files, excludePatterns []string, log termlog.Logger) []string {
ret := []string{}
for _, file := range files {
if shouldInclude(file, excludePatterns, log) {
ret = append(ret, file)
}
}
return ret
}
// 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(path, batchTime, modchan)
if err != nil {
return err
}
go func() {
for mod := range modchan {
files := filterFiles(mod.All(), excludePatterns, log)
if len(files) > 0 {
ch <- files
}
}
}()
}
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
}