From bcf0c93e6195e4b0b8f6335e156ab0676b12e3bb Mon Sep 17 00:00:00 2001 From: maddalax Date: Wed, 25 Sep 2024 10:16:05 -0500 Subject: [PATCH] a little bit of live reload speedup --- cli/htmgo/runner.go | 26 +++++++++++++++++++++++--- cli/htmgo/tasks/copyassets/bundle.go | 4 ++-- cli/htmgo/tasks/reloader/reloader.go | 20 ++++++++++++++++---- cli/htmgo/watcher.go | 8 ++------ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/cli/htmgo/runner.go b/cli/htmgo/runner.go index d48dd5b..222f6fd 100644 --- a/cli/htmgo/runner.go +++ b/cli/htmgo/runner.go @@ -14,6 +14,7 @@ import ( "log/slog" "os" "strings" + "sync" ) func main() { @@ -58,14 +59,33 @@ func main() { fmt.Printf("Running in watch mode\n") os.Setenv("ENV", "development") os.Setenv("WATCH_MODE", "true") - fmt.Printf("Starting processes...") + fmt.Printf("Starting processes...\n") + copyassets.CopyAssets() - astgen.GenAst(process.ExitOnError) + + fmt.Printf("Generating CSS...this may take a few seconds.\n") css.GenerateCss(process.ExitOnError) - run.EntGenerate() + + wg := sync.WaitGroup{} + + wg.Add(1) + go func() { + defer wg.Done() + astgen.GenAst(process.ExitOnError) + }() + + wg.Add(1) + go func() { + defer wg.Done() + run.EntGenerate() + }() + + wg.Wait() + go func() { css.GenerateCssWatch(process.ExitOnError) }() + fmt.Printf("Starting server...\n") go func() { _ = run.Server() diff --git a/cli/htmgo/tasks/copyassets/bundle.go b/cli/htmgo/tasks/copyassets/bundle.go index 99430b8..45132d3 100644 --- a/cli/htmgo/tasks/copyassets/bundle.go +++ b/cli/htmgo/tasks/copyassets/bundle.go @@ -76,7 +76,7 @@ func CopyAssets() { if strings.HasSuffix(path, "tailwind.config.js") { return false } - return true + return !exists }) if err != nil { @@ -101,5 +101,5 @@ func CopyAssets() { log.Fatalf("Error: %v", err) } - process.Run(fmt.Sprintf("cd %s && git add .", destDirCss)) + process.Run(fmt.Sprintf("cd %s && git add .", destDirCss), process.Silent) } diff --git a/cli/htmgo/tasks/reloader/reloader.go b/cli/htmgo/tasks/reloader/reloader.go index 1878cb5..21eddd0 100644 --- a/cli/htmgo/tasks/reloader/reloader.go +++ b/cli/htmgo/tasks/reloader/reloader.go @@ -66,6 +66,7 @@ func OnFileChange(events []*fsnotify.Event) { now := time.Now() tasks := Tasks{} + hasTask := false for _, event := range events { c := NewChange(event) @@ -78,36 +79,47 @@ func OnFileChange(events []*fsnotify.Event) { if c.IsGo() && c.HasAnyPrefix("pages/", "partials/") { tasks.AstGen = true + hasTask = true } if c.IsGo() { tasks.Run = true + hasTask = true } if c.HasAnySuffix(".md") { tasks.Run = true + hasTask = true } if c.HasAnySuffix("tailwind.config.js", ".css") { tasks.Run = true + hasTask = true } if c.HasAnyPrefix("ent/schema") { tasks.Ent = true + hasTask = true } - slog.Info("file changed", slog.String("file", c.Name())) + if hasTask { + slog.Info("file changed", slog.String("file", c.Name())) + } + } + + if !hasTask { + return } deps := make([]func() any, 0) if tasks.AstGen { - deps = append(deps, func() any { - return util.Trace("generate ast", func() any { + go func() { + util.Trace("generate ast", func() any { astgen.GenAst() return nil }) - }) + }() } if tasks.Ent { diff --git a/cli/htmgo/watcher.go b/cli/htmgo/watcher.go index 930017f..87129eb 100644 --- a/cli/htmgo/watcher.go +++ b/cli/htmgo/watcher.go @@ -6,14 +6,12 @@ import ( "log/slog" "os" "path/filepath" - "time" ) var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"} func startWatcher(cb func(file []*fsnotify.Event)) { events := make([]*fsnotify.Event, 0) - debouncer := NewDebouncer(100 * time.Millisecond) defer func() { if r := recover(); r != nil { @@ -36,10 +34,8 @@ func startWatcher(cb func(file []*fsnotify.Event)) { } if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { events = append(events, &event) - debouncer.Do(func() { - cb(events) - events = make([]*fsnotify.Event, 0) - }) + cb(events) + events = make([]*fsnotify.Event, 0) } case err, ok := <-watcher.Errors: if !ok {