mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
Add support for dynamic reloading of config file when watching
This commit is contained in:
@@ -17,7 +17,6 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/hugo/parser"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -27,6 +26,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/hugo/parser"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/fsync"
|
||||
"github.com/spf13/hugo/helpers"
|
||||
@@ -53,6 +54,7 @@ built with love by spf13 and friends in Go.
|
||||
Complete documentation is available at http://gohugo.io/.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
InitializeConfig()
|
||||
watchConfig()
|
||||
build()
|
||||
},
|
||||
}
|
||||
@@ -312,6 +314,18 @@ func InitializeConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
func watchConfig() {
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("Config file changed:", e.Name)
|
||||
utils.CheckErr(buildSite(true))
|
||||
if !viper.GetBool("DisableLiveReload") {
|
||||
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
|
||||
livereload.ForceRefresh()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func build(watches ...bool) {
|
||||
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
|
||||
watch := false
|
||||
|
@@ -93,6 +93,7 @@ func server(cmd *cobra.Command, args []string) {
|
||||
|
||||
if viper.GetBool("watch") {
|
||||
serverWatch = true
|
||||
watchConfig()
|
||||
}
|
||||
|
||||
l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(serverPort)))
|
||||
|
Reference in New Issue
Block a user