fix: watch file change event repeat trigger

fix: fault source copy path
master
xuthus5 4 months ago
parent baea60d195
commit ff5e291ad4
  1. 17
      output.go
  2. 22
      serve.go

@ -76,23 +76,23 @@ func (o *Outter) sourceCopy() {
var args []string
switch runtime.GOOS {
case "windows":
args = []string{"cmd.exe", "/C", "xcopy", "/e"}
args = []string{"cmd.exe", "/C", "xcopy", "/e", "/y"}
default:
args = []string{"cp", "-r"}
}
themePath := slash(fmt.Sprintf("./themes/%s/", o.Config.Theme))
destPath := slash("./dist/")
cmd := exec.Command(args[0], append(args, themePath+"css", destPath)...)
cmd := exec.Command(args[0], append(args, themePath+"css", slash(destPath+"css/"))...)
if err := cmd.Run(); err != nil {
sout(cmd.String())
sfault("copy theme css source failed: %v", err)
}
cmd = exec.Command(args[0], append(args, themePath+"js", destPath)...)
cmd = exec.Command(args[0], append(args, themePath+"js", slash(destPath+"js/"))...)
if err := cmd.Run(); err != nil {
sout(cmd.String())
sfault("copy theme js source failed: %v", err)
}
cmd = exec.Command(args[0], append(args, themePath+"images", destPath)...)
cmd = exec.Command(args[0], append(args, themePath+"images", slash(destPath+"images/"))...)
if err := cmd.Run(); err != nil {
sout(cmd.String())
sfault("copy theme images source failed: %v", err)
@ -191,7 +191,6 @@ func (o *Outter) generateIndex() {
}
buffer.Reset()
}
sout("index generate success...")
o.Posts = make([]Post, len(posts))
copy(o.Posts, posts)
}
@ -232,8 +231,6 @@ func (o *Outter) generatePost() {
}
postBuffer.Reset()
}
sout("post generate success...")
}
func (o *Outter) generatePage() {
@ -266,8 +263,6 @@ func (o *Outter) generatePage() {
}
pageBuffer.Reset()
}
sout("page generate success...")
}
func (o *Outter) generateArchives() {
@ -323,8 +318,6 @@ func (o *Outter) generateArchives() {
sfault("write archive page failed: %v", err)
}
archiveBuffer.Reset()
sout("archives generate success...")
}
func (o *Outter) generateTags() {
@ -401,8 +394,6 @@ func (o *Outter) generateTags() {
}
tagsBuffer.Reset()
}
sout("tags generate success...")
}
func getSource(data interface{}, key string) interface{} {

@ -1,7 +1,6 @@
package main
import (
"fmt"
"net/http"
"time"
@ -18,10 +17,19 @@ func serveCmd() *cobra.Command {
w := watcher.New()
w.SetMaxEvents(1)
w.FilterOps(watcher.Rename, watcher.Move, watcher.Write, watcher.Create)
if err := w.Ignore("./dist"); err != nil {
sfault("watch file failed: %v", err)
if err := w.AddRecursive("./data"); err != nil {
sfault("watch directory data failed: %v", err)
}
if err := w.AddRecursive("./posts"); err != nil {
sfault("watch directory posts failed: %v", err)
}
if err := w.AddRecursive("./pages"); err != nil {
sfault("watch directory pages failed: %v", err)
}
if err := w.AddRecursive("./themes"); err != nil {
sfault("watch directory pages failed: %v", err)
}
if err := w.AddRecursive("."); err != nil {
if err := w.Add("config.yaml"); err != nil {
sfault("watch file failed: %v", err)
}
// start http server
@ -41,12 +49,12 @@ func serveCmd() *cobra.Command {
go func() {
for {
select {
case <-w.Event:
case event := <-w.Event:
// 文件变更 更新文件
if err := deployCmd().Execute(); err != nil {
sfault("re generate website failed: %v", err)
}
sout("file change")
sout("file change: %v", event.String())
case err := <-w.Error:
sfault("watch file failed: %v", err)
case <-w.Closed:
@ -59,7 +67,7 @@ func serveCmd() *cobra.Command {
sfault("generate website failed: %v", err)
}
// 五秒一次
fmt.Println("http://127.0.0.1:8666")
sout("http://127.0.0.1:8666")
if err := w.Start(time.Second * 5); err != nil {
sfault("watch file failed: %v", err)
}

Loading…
Cancel
Save