-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.go
53 lines (46 loc) · 1.14 KB
/
reload.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
package main
import (
"log"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func setupReloading(conf *config, params *Parameters) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Printf("File %s was modified, rebuilding...", event.Name)
buildProject(conf, params)
log.Printf("Project was rebuilt")
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
project := params.ProjectName
dirs := []string{}
dirs = append(dirs, filepath.Join(project, "scipio.toml"))
dirs = append(dirs, filepath.Join(project, "source"))
dirs = append(dirs, filepath.Join(project, "source", "pages"))
dirs = append(dirs, filepath.Join(project, "source", "posts"))
dirs = append(dirs, filepath.Join(project, "themes", "default"))
dirs = append(dirs, filepath.Join(project, "themes", "default", "static"))
for _, dir := range dirs {
err = watcher.Add(dir)
if err != nil {
log.Fatal(err)
}
}
}