Skip to content

Commit 4075a86

Browse files
committed
feat: add watcher
1 parent e19586f commit 4075a86

File tree

4 files changed

+107
-9
lines changed

4 files changed

+107
-9
lines changed

pkg/alvu/alvu.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,44 @@ type AlvuConfig struct {
5151
// Internals
5252
logger Logger
5353
hookHandler *Hooks
54+
watcher *Watcher
55+
}
56+
57+
func (ac *AlvuConfig) Rebuild(path string) {
58+
ac.logger.Info(fmt.Sprintf("Changed: %v, Recompiling.", path))
59+
err := ac.Build()
60+
ac.logger.Error(err.Error())
5461
}
5562

5663
func (ac *AlvuConfig) Run() error {
5764
ac.logger = Logger{
5865
logPrefix: "[alvu]",
5966
}
6067

68+
if ac.Serve {
69+
ac.watcher = NewWatcher()
70+
ac.watcher.logger = ac.logger
71+
go func(ac *AlvuConfig) {
72+
for path := range ac.watcher.recompile {
73+
ac.logger.Info(fmt.Sprintf("Changed: %v, recompiling...", path))
74+
ac.Build()
75+
}
76+
}(ac)
77+
}
78+
79+
err := ac.Build()
80+
if err != nil {
81+
return err
82+
}
83+
84+
if ac.Serve {
85+
ac.watcher.Start()
86+
}
87+
88+
return ac.StartServer()
89+
}
90+
91+
func (ac *AlvuConfig) Build() error {
6192
hooksHandler := Hooks{
6293
ac: *ac,
6394
}
@@ -77,18 +108,24 @@ func (ac *AlvuConfig) Run() error {
77108
},
78109
}
79110

80-
filesToProcess, err := ac.ReadDir(filepath.Join(ac.RootPath, "pages"))
111+
pageDir := filepath.Join(ac.RootPath, "pages")
112+
publicDir := filepath.Join(ac.RootPath, "public")
113+
114+
filesToProcess, err := ac.ReadDir(pageDir)
81115
if err != nil {
82116
return err
83117
}
84118

85119
ac.logger.Debug(fmt.Sprintf("filesToProcess: %v", filesToProcess))
86120

87-
publicFiles, err := ac.ReadDir(filepath.Join(ac.RootPath, "public"))
121+
publicFiles, err := ac.ReadDir(publicDir)
88122
if err != nil {
89123
return err
90124
}
91125

126+
ac.watcher.AddDir(pageDir)
127+
ac.watcher.AddDir(publicDir)
128+
92129
normalizedFiles, err := runTransfomers(filesToProcess, ac)
93130
if err != nil {
94131
return err
@@ -105,12 +142,7 @@ func (ac *AlvuConfig) Run() error {
105142
}
106143

107144
ac.HandlePublicFiles(publicFiles)
108-
err = ac.FlushFiles(processedFiles)
109-
if err != nil {
110-
return err
111-
}
112-
113-
return ac.StartServer()
145+
return ac.FlushFiles(processedFiles)
114146
}
115147

116148
func (ac *AlvuConfig) ReadLayout() string {

pkg/alvu/hooks.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Hooks struct {
3131
ac AlvuConfig
3232
collection []*HookSource
3333
forSpecificFiles map[string][]*HookSource
34+
35+
_legacyTransformLogSent bool
3436
}
3537

3638
type HookedFile struct {
@@ -232,7 +234,10 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
232234
if fromPlug["transform"] != nil {
233235
hookedFile.transform = fmt.Sprintf("%v", fromPlug["transform"])
234236
} else {
235-
h.ac.logger.Warning("Auto transformation of content returned from the hooks will be removed in v0.3,\n please return a `transform` property from the hooks instead.")
237+
if !h._legacyTransformLogSent {
238+
h.ac.logger.Warning("Auto transformation of content returned from the hooks will be removed in v0.3,\n please return a `transform` property from the hooks instead.")
239+
h._legacyTransformLogSent = true
240+
}
236241
hookedFile.transform = ".md"
237242
}
238243

pkg/alvu/watcher.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package alvu
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/barelyhuman/go/poller"
8+
)
9+
10+
type Watcher struct {
11+
poller *poller.Poller
12+
logger Logger
13+
recompile chan string
14+
}
15+
16+
type HookFn func(path string)
17+
18+
func NewWatcher() *Watcher {
19+
return &Watcher{
20+
poller: poller.NewPollWatcher(2000),
21+
recompile: make(chan string, 1),
22+
}
23+
}
24+
25+
func (p *Watcher) AddDir(path string) {
26+
p.poller.Add(path)
27+
}
28+
29+
func (p *Watcher) Start() {
30+
go p.poller.Start()
31+
go func() {
32+
for {
33+
select {
34+
case evt := <-p.poller.Events:
35+
_, err := os.Stat(evt.Path)
36+
37+
p.logger.Debug(fmt.Sprintf("Change Event: %v", evt))
38+
39+
// Do nothing if the file doesn't exit, just continue
40+
if err != nil {
41+
if os.IsNotExist(err) {
42+
continue
43+
}
44+
p.logger.Error(err.Error())
45+
}
46+
47+
p.recompile <- evt.Path
48+
continue
49+
case err := <-p.poller.Errors:
50+
p.logger.Error(err.Error())
51+
}
52+
}
53+
}()
54+
}

transformers/markdown/markdown.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ func (rlr *relativeLinkRewriter) Transform(doc *ast.Document, reader text.Reader
120120
})
121121
}
122122

123+
// TODO: remove in v0.3
124+
var _warningPrinted bool = false
125+
123126
// TODO: remove in v0.3
124127
func printMetaLinkWarning() {
128+
if _warningPrinted {
129+
return
130+
}
131+
_warningPrinted = true
125132
warning := "{{.Meta.BaseURL}} is no more needed in markdown files, links will be rewritten automatically.\n Use root first links, eg: pages/docs/some-topic.md would be linked as /docs/some-topic"
126133
cs := color.ColorString{}
127134
cs.Reset(" ").Yellow(warning)

0 commit comments

Comments
 (0)