drafts seem to be working again

pull/8/head
Stephen Searles 7 years ago
parent f9fbc5590f
commit 95f3e41f1e
  1. 6
      caddyhugo.go
  2. 12
      client.go
  3. 11
      http.go
  4. 14
      hugo.go

@ -75,6 +75,12 @@ func (ch *CaddyHugo) docFilename(orig string) string {
// Publish really renders new content into the public directory // Publish really renders new content into the public directory
func (ch *CaddyHugo) Publish() error { func (ch *CaddyHugo) Publish() error {
ch.mtx.Lock()
for _, es := range ch.docs {
ch.persistEdits(es)
}
ch.mtx.Unlock()
cmd := exec.Command("hugo") cmd := exec.Command("hugo")
cmd.Dir = ch.Dir cmd.Dir = ch.Dir
_, err := cmd.CombinedOutput() _, err := cmd.CombinedOutput()

@ -43,11 +43,9 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
filename: filename, filename: filename,
doc: acedoc.NewString(string(contents)), doc: acedoc.NewString(string(contents)),
// tmpfs: afero.NewCopyOnWriteFs(afero.NewBasePathFs(afero.NewOsFs(), ch.Dir+"/"), afero.NewMemMapFs()), // tmpfs: afero.NewCopyOnWriteFs(afero.NewBasePathFs(afero.NewOsFs(), ch.Dir+"/"), afero.NewMemMapFs()),
tmpfs: afero.NewCopyOnWriteFs(afero.NewReadOnlyFs(afero.NewBasePathFs(afero.NewOsFs(), ch.Dir)), afero.NewMemMapFs()), tmpfs: afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()),
} }
printTree(es.tmpfs)
err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -82,6 +80,14 @@ func (ch *CaddyHugo) renderDraft(es *editSession) error {
return proc.Start() return proc.Start()
} }
func (ch *CaddyHugo) persistEdits(es *editSession) error {
err := afero.WriteFile(afero.NewOsFs(), es.filename, []byte(es.doc.Contents()), 0644)
if err != nil {
return err
}
return nil
}
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) { func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) {
dr, ok := ch.docs[ch.docFilename(docName)] dr, ok := ch.docs[ch.docFilename(docName)]
return dr, ok return dr, ok

@ -210,7 +210,7 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
} }
r.URL.Path = page.RelPermalink() r.URL.Path = page.RelPermalink()
http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, "public")}).ServeHTTP(w, r) http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r)
return 200, nil return 200, nil
} }
@ -219,9 +219,6 @@ func printTree(fs afero.Fs) {
const tab = " " const tab = " "
afero.Walk(fs, "/", filepath.WalkFunc(func(path string, info os.FileInfo, err error) error { afero.Walk(fs, "/", filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
if path == "/" {
return nil
}
if filepath.Base(path) == ".git" { if filepath.Base(path) == ".git" {
return filepath.SkipDir return filepath.SkipDir
} }
@ -253,5 +250,11 @@ type aferoHTTP struct {
func (a aferoHTTP) Open(name string) (http.File, error) { func (a aferoHTTP) Open(name string) (http.File, error) {
af, err := a.Fs.Open(name) af, err := a.Fs.Open(name)
if os.IsExist(err) {
err = os.ErrExist
}
if os.IsNotExist(err) {
err = os.ErrNotExist
}
return af, err return af, err
} }

@ -2,6 +2,7 @@ package caddyhugo
import ( import (
"fmt" "fmt"
"path"
"time" "time"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
@ -29,15 +30,15 @@ type HugoRenderer interface {
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
printTree(es.tmpfs)
var err error var err error
hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)} hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)}
hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "/", "config.toml") fmt.Println(ch.Dir)
hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "", path.Join(ch.Dir, "config.toml"))
if err != nil { if err != nil {
return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
} }
hugoCfg.Cfg.Set("workingdir", "/") hugoCfg.Cfg.Set("workingDir", ch.Dir)
hugoSites, err := hugolib.NewHugoSites(*hugoCfg) hugoSites, err := hugolib.NewHugoSites(*hugoCfg)
if err != nil { if err != nil {
return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err) return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err)
@ -53,15 +54,20 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idles
TickDuration: WebsocketFileTicker, TickDuration: WebsocketFileTicker,
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1, MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1,
Stop: func() error { Stop: func() error {
ch.persistEdits(es)
es.doc.Close() es.doc.Close()
ch.mtx.Lock()
defer ch.mtx.Unlock()
delete(ch.docs, es.filename) delete(ch.docs, es.filename)
return nil return nil
}, },
TickError: func(err error) { TickError: func(err error) {
fmt.Println("error processing draft:", err) fmt.Println("error processing draft:", err)
}, },
Tick: func() error { Tick: func() error {
fmt.Println("TICK")
err := afero.WriteFile(es.tmpfs, es.filename, []byte(es.doc.Contents()), 0644) err := afero.WriteFile(es.tmpfs, es.filename, []byte(es.doc.Contents()), 0644)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save