forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
77 lines (73 loc) · 1.78 KB
/
watcher.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
package watcher
import (
"github.com/kabukky/journey/helpers"
"gopkg.in/fsnotify.v1"
"log"
"os"
"path/filepath"
)
var watcher *fsnotify.Watcher
var watchedDirectories []string
func Watch(paths []string, extensionsFunctions map[string]func() error) error {
// Prepare watcher to generate the theme on changes to the files
if watcher == nil {
var err error
watcher, err = createWatcher(extensionsFunctions)
if err != nil {
return err
}
} else {
// Remove all current directories from watcher
for _, dir := range watchedDirectories {
err := watcher.Remove(dir)
if err != nil {
return err
}
}
}
watchedDirectories = make([]string, 0)
// Watch all subdirectories in the given paths
for _, path := range paths {
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
err := watcher.Add(filePath)
if err != nil {
return err
}
watchedDirectories = append(watchedDirectories, filePath)
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func createWatcher(extensionsFunctions map[string]func() error) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
for key, value := range extensionsFunctions {
if !helpers.IsDirectory(event.Name) && filepath.Ext(event.Name) == key {
// Call the function associated with this file extension
err := value()
if err != nil {
log.Panic("Error while reloading theme or plugins:", err)
}
}
}
}
case err := <-watcher.Errors:
log.Println("Error while watching directory:", err)
}
}
}()
return watcher, nil
}