Skip to content

Commit

Permalink
task: adding options on the watch command
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Sep 21, 2023
1 parent 4d81c9a commit 661eb00
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
33 changes: 0 additions & 33 deletions tailo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package tailo

import (
"fmt"
"log"
"os"
"path/filepath"

Expand All @@ -24,38 +23,6 @@ func (e extensions) Has(ext string) bool {
return false
}

func Watch() {
Build()

watcher, err := buildWatcher()
if err != nil {
panic(err)
}

// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}

if event.Has(fsnotify.Write) {
Build()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()

<-make(chan struct{})
}

// Extensions to watch for changes.
var watchExtensions = extensions{".html", ".css"}

Expand Down
39 changes: 39 additions & 0 deletions watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tailo

import (
"log"

"github.com/fsnotify/fsnotify"
)

func Watch(options ...option) {
Build(options...)

watcher, err := buildWatcher()
if err != nil {
panic(err)
}

// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}

if event.Has(fsnotify.Write) {
Build(options...)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()

<-make(chan struct{})
}

0 comments on commit 661eb00

Please sign in to comment.