a little bit of live reload speedup

This commit is contained in:
maddalax 2024-09-25 10:16:05 -05:00
parent 59e98d8a52
commit bcf0c93e61
4 changed files with 43 additions and 15 deletions

View file

@ -14,6 +14,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"strings" "strings"
"sync"
) )
func main() { func main() {
@ -58,14 +59,33 @@ func main() {
fmt.Printf("Running in watch mode\n") fmt.Printf("Running in watch mode\n")
os.Setenv("ENV", "development") os.Setenv("ENV", "development")
os.Setenv("WATCH_MODE", "true") os.Setenv("WATCH_MODE", "true")
fmt.Printf("Starting processes...") fmt.Printf("Starting processes...\n")
copyassets.CopyAssets() copyassets.CopyAssets()
astgen.GenAst(process.ExitOnError)
fmt.Printf("Generating CSS...this may take a few seconds.\n")
css.GenerateCss(process.ExitOnError) 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() { go func() {
css.GenerateCssWatch(process.ExitOnError) css.GenerateCssWatch(process.ExitOnError)
}() }()
fmt.Printf("Starting server...\n") fmt.Printf("Starting server...\n")
go func() { go func() {
_ = run.Server() _ = run.Server()

View file

@ -76,7 +76,7 @@ func CopyAssets() {
if strings.HasSuffix(path, "tailwind.config.js") { if strings.HasSuffix(path, "tailwind.config.js") {
return false return false
} }
return true return !exists
}) })
if err != nil { if err != nil {
@ -101,5 +101,5 @@ func CopyAssets() {
log.Fatalf("Error: %v", err) 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)
} }

View file

@ -66,6 +66,7 @@ func OnFileChange(events []*fsnotify.Event) {
now := time.Now() now := time.Now()
tasks := Tasks{} tasks := Tasks{}
hasTask := false
for _, event := range events { for _, event := range events {
c := NewChange(event) c := NewChange(event)
@ -78,36 +79,47 @@ func OnFileChange(events []*fsnotify.Event) {
if c.IsGo() && c.HasAnyPrefix("pages/", "partials/") { if c.IsGo() && c.HasAnyPrefix("pages/", "partials/") {
tasks.AstGen = true tasks.AstGen = true
hasTask = true
} }
if c.IsGo() { if c.IsGo() {
tasks.Run = true tasks.Run = true
hasTask = true
} }
if c.HasAnySuffix(".md") { if c.HasAnySuffix(".md") {
tasks.Run = true tasks.Run = true
hasTask = true
} }
if c.HasAnySuffix("tailwind.config.js", ".css") { if c.HasAnySuffix("tailwind.config.js", ".css") {
tasks.Run = true tasks.Run = true
hasTask = true
} }
if c.HasAnyPrefix("ent/schema") { if c.HasAnyPrefix("ent/schema") {
tasks.Ent = true 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) deps := make([]func() any, 0)
if tasks.AstGen { if tasks.AstGen {
deps = append(deps, func() any { go func() {
return util.Trace("generate ast", func() any { util.Trace("generate ast", func() any {
astgen.GenAst() astgen.GenAst()
return nil return nil
}) })
}) }()
} }
if tasks.Ent { if tasks.Ent {

View file

@ -6,14 +6,12 @@ import (
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"time"
) )
var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"} var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"}
func startWatcher(cb func(file []*fsnotify.Event)) { func startWatcher(cb func(file []*fsnotify.Event)) {
events := make([]*fsnotify.Event, 0) events := make([]*fsnotify.Event, 0)
debouncer := NewDebouncer(100 * time.Millisecond)
defer func() { defer func() {
if r := recover(); r != nil { 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) { if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) {
events = append(events, &event) events = append(events, &event)
debouncer.Do(func() { cb(events)
cb(events) events = make([]*fsnotify.Event, 0)
events = make([]*fsnotify.Event, 0)
})
} }
case err, ok := <-watcher.Errors: case err, ok := <-watcher.Errors:
if !ok { if !ok {