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"
"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()

View file

@ -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)
}

View file

@ -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 {

View file

@ -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 {