diff options
| author | Shulhan <ms@kilabit.info> | 2021-10-17 19:14:20 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-10-17 19:14:20 +0700 |
| commit | 36d729c71dd03303a2eef4cfefa64ac1a78a0ec1 (patch) | |
| tree | 451649d9eaecd20b466a4a4d56a393410fbf8713 | |
| parent | 56488e32c04c4561d06d3446b81bb726d5cbb6ff (diff) | |
| download | ciigo-36d729c71dd03303a2eef4cfefa64ac1a78a0ec1.tar.xz | |
all: fix empty fileMarkups on watcher
Previously, when user call ciigo.Watch(), and the markup file changes,
the onChangeFileMarkup method will print an error "xyz not found" which
cause the markup file not converted.
This is caused by watcher.fileMarkups is empty.
This changes fix this issue by initializing the fileMarkups field using
listFileMarkups, so the next callback to onChangeFileMarkup can detect
the changed file and convert it.
| -rw-r--r-- | ciigo.go | 2 | ||||
| -rw-r--r-- | server.go | 7 | ||||
| -rw-r--r-- | watcher.go | 46 | ||||
| -rw-r--r-- | watcher_test.go | 10 |
4 files changed, 43 insertions, 22 deletions
@@ -197,7 +197,7 @@ func Watch(opts *ConvertOptions) (err error) { return fmt.Errorf("%s: %w", logp, err) } - w, err = newWatcher(htmlg, opts.Root, opts.Exclude) + w, err = newWatcher(htmlg, opts) if err != nil { return fmt.Errorf("%s: %w", logp, err) } @@ -71,12 +71,7 @@ func newServer(opts *ServeOptions) (srv *server, err error) { } if srv.opts.Development { - srv.watcher, err = newWatcher(srv.htmlg, opts.Root, opts.Exclude) - if err != nil { - return nil, fmt.Errorf("%s: %w", logp, err) - } - - srv.watcher.fileMarkups, err = listFileMarkups(opts.Root, opts.excRE) + srv.watcher, err = newWatcher(srv.htmlg, &opts.ConvertOptions) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -21,11 +21,16 @@ import ( // automatically to HTML. // type watcher struct { - dir string - htmlg *htmlGenerator - dw *libio.DirWatcher + dir string + htmlg *htmlGenerator + dw *libio.DirWatcher + + // fileMarkups contains all markup files found inside "dir". + // Its used to convert all markup files when the template file + // changes. fileMarkups map[string]*fileMarkup - changes *clise.Clise + + changes *clise.Clise } // @@ -43,16 +48,17 @@ type watcher struct { // | // +--> UPDATE --> htmlGenerated.htmlTemplateReload() // -func newWatcher(htmlg *htmlGenerator, dir, exclude string) (w *watcher, err error) { +func newWatcher(htmlg *htmlGenerator, convertOpts *ConvertOptions) (w *watcher, err error) { + logp := "newWatcher" + w = &watcher{ - dir: dir, - htmlg: htmlg, - fileMarkups: make(map[string]*fileMarkup), - changes: clise.New(1), + dir: convertOpts.Root, + htmlg: htmlg, + changes: clise.New(1), } w.dw = &libio.DirWatcher{ Options: memfs.Options{ - Root: dir, + Root: convertOpts.Root, Includes: []string{ `.*\.adoc$`, }, @@ -66,8 +72,13 @@ func newWatcher(htmlg *htmlGenerator, dir, exclude string) (w *watcher, err erro Callback: w.onChangeFileMarkup, } - if len(exclude) > 0 { - w.dw.Options.Excludes = append(w.dw.Options.Excludes, exclude) + if len(convertOpts.Exclude) > 0 { + w.dw.Options.Excludes = append(w.dw.Options.Excludes, convertOpts.Exclude) + } + + w.fileMarkups, err = listFileMarkups(convertOpts.Root, convertOpts.excRE) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) } return w, nil @@ -117,8 +128,15 @@ func (w *watcher) onChangeFileMarkup(ns *libio.NodeState) { fmt.Printf("%s: %s content updated\n", logp, ns.Node.SysPath) fmarkup = w.fileMarkups[ns.Node.SysPath] if fmarkup == nil { - fmt.Printf("%s: %s not found\n", logp, ns.Node.SysPath) - return + log.Printf("%s: %s not found\n", logp, ns.Node.SysPath) + + fmarkup, err = newFileMarkup(ns.Node.SysPath, nil) + if err != nil { + log.Printf("%s: %s\n", logp, err) + return + } + + w.fileMarkups[ns.Node.SysPath] = fmarkup } } diff --git a/watcher_test.go b/watcher_test.go index 3bc7c31..98b7eb1 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -42,7 +42,15 @@ func TestWatcher(t *testing.T) { t.Fatal(err) } - testWatcher, err = newWatcher(htmlg, testDir, "") + convertOpts := ConvertOptions{ + Root: testDir, + } + err = convertOpts.init() + if err != nil { + t.Fatal(err) + } + + testWatcher, err = newWatcher(htmlg, &convertOpts) if err != nil { t.Fatal(err) } |
