diff --git a/commands/serve.go b/commands/serve.go index 3d2bcea..1572e77 100644 --- a/commands/serve.go +++ b/commands/serve.go @@ -14,17 +14,13 @@ import ( // Generate and serve the site, rebuilding when the source files change. func Serve() error { - site, err := site.Load(SRC_DIR, LAYOUTS_DIR) - if err != nil { - return err - } - if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + if err := rebuild(); err != nil { return err } // watch for changes in src and layouts, and trigger a rebuild - watcher, err := setupWatcher(site) + watcher, err := setupWatcher() if err != nil { return err } @@ -39,6 +35,19 @@ func Serve() error { return nil } +func rebuild() error { + site, err := site.Load(SRC_DIR, LAYOUTS_DIR) + if err != nil { + return err + } + + if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + return err + } + + return nil +} + // Tweaks the http file system to construct a server that hides the .html suffix from requests. // Based on https://stackoverflow.com/a/57281956/993769 type HTMLDir struct { @@ -57,7 +66,7 @@ func (d HTMLDir) Open(name string) (http.File, error) { return f, err } -func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) { +func setupWatcher() (*fsnotify.Watcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { return nil, err @@ -88,7 +97,7 @@ func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) { return } - if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil { + if err := rebuild(); err != nil { fmt.Println("error:", err) return }