代码拉取完成,页面将自动刷新
package watcher
import (
"fmt"
"github.com/radovskyb/watcher"
"harl/model"
"harl/utils"
"log"
"path"
"strings"
"time"
)
type FileWatch struct {
W *watcher.Watcher
Event chan model.Envent
config model.Config
}
func New(config model.Config) *FileWatch {
return &FileWatch{
W: watcher.New(),
config: config,
Event: make(chan model.Envent),
}
}
func (fw *FileWatch) Watcher() {
w := watcher.New()
defer w.Close()
w.FilterOps(watcher.Write, watcher.Remove)
go func() {
for {
select {
case event := <-w.Event:
fmt.Println(event) // Print the event's info.
if event.IsDir() {
continue
}
if !utils.IncludesString(fw.config.Build.Includes, path.Ext(event.Path)) {
continue
}
if strings.HasSuffix(event.Path, ".hap") {
fw.Event <- model.Envent{
Action: "reload",
Data: map[string]string{"bin": event.Path},
}
} else {
fw.Event <- model.Envent{
Action: "build",
Data: nil,
}
}
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Watch test_folder recursively for changes.
if err := w.AddRecursive(fw.config.Build.Project); err != nil {
log.Fatalln(err)
}
for _, exclude := range fw.config.Build.Excludes {
err := w.Ignore(fmt.Sprintf("%s/%s", fw.config.Build.Project, exclude))
if err != nil {
fmt.Errorf("exclude files error: %w", err)
}
}
w.IgnoreHiddenFiles(true)
delay, _ := time.ParseDuration(fmt.Sprintf("%dms", fw.config.Build.Delay))
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(delay); err != nil {
log.Fatalln(err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。