[fix] watch the script file changes

This commit is contained in:
Max 2022-08-20 22:10:49 +08:00
parent c0402149ce
commit 9d19d32fef

View file

@ -76,13 +76,18 @@ func watchStart(cfg config.Config) error {
} }
name := dir.Name() name := dir.Name()
if _, has := handlers[name]; !has { if _, has := excludes[name]; has {
continue
}
if strings.HasPrefix(name, ".") {
continue continue
} }
filename := filepath.Join(root, name) filename := filepath.Join(root, name)
err := watcher.Add(filename) err := watcher.Add(filename)
if err != nil { if err != nil {
log.Error("[Watch] %s", err.Error())
return err return err
} }
@ -90,7 +95,13 @@ func watchStart(cfg config.Config) error {
log.Info("[Watch] Watching: %s", filename) log.Info("[Watch] Watching: %s", filename)
// sub dir // sub dir
depth := 0
err = filepath.WalkDir(filename, func(path string, d fs.DirEntry, err error) error { err = filepath.WalkDir(filename, func(path string, d fs.DirEntry, err error) error {
depth = depth + 1
if depth == 1 {
return nil
}
if !d.IsDir() { if !d.IsDir() {
return nil return nil
} }
@ -98,13 +109,13 @@ func watchStart(cfg config.Config) error {
log.Info("[Watch] Watching: %s", path) log.Info("[Watch] Watching: %s", path)
err = watcher.Add(path) err = watcher.Add(path)
if err != nil { if err != nil {
return err log.Error("[Watch] %s", err.Error())
} }
return nil return nil
}) })
if err != nil { if err != nil {
return err log.Error("[Watch] %s", err.Error())
} }
} }
@ -136,7 +147,7 @@ func watchStart(cfg config.Config) error {
if _, has := excludes[widget]; !has { if _, has := excludes[widget]; !has {
base := filepath.Base(event.Name) base := filepath.Base(event.Name)
isdir := true isdir := true
if strings.HasSuffix(base, ".yao") || strings.HasSuffix(base, ".json") { if strings.HasSuffix(base, ".yao") || strings.HasSuffix(base, ".json") || strings.HasSuffix(base, ".js") {
isdir = false isdir = false
} }
@ -237,17 +248,21 @@ func watchModel(root string, file string, event string, cfg config.Config) {
} }
func watchReload(root string, file string, event string, cfg config.Config) { func watchReload(root string, file string, event string, cfg config.Config) {
err := engine.Load(config.Conf) // 加载脚本等
if err != nil { switch event {
fmt.Println(color.RedString("[Watch] Reload: %s", err.Error())) case "CREATE", "WRITE", "REMOVE":
err := engine.Load(config.Conf) // 加载脚本等
if err != nil {
fmt.Println(color.RedString("[Watch] Reload: %s", err.Error()))
}
// Restart Server
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
StopWithContext(ctx, func() {
go Start()
fmt.Println(color.GreenString("[Watch] Reload Completed"))
})
} }
// Restart Server
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
StopWithContext(ctx, func() {
go Start()
fmt.Println(color.GreenString("[Watch] Reload Completed"))
})
} }