From 161cb79b8824be82263e10719f25871234038ddf Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 17 Dec 2021 21:39:46 +0100 Subject: [PATCH] Working version --- go.mod | 17 +++++++++ go.sum | 27 +++++++++++++ main.go | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3f755af --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module gitlab.codeblob.work/pk/gowebbuild + +go 1.17 + +require ( + github.com/evanw/esbuild v0.14.5 + github.com/goyek/goyek v0.6.0 + github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71 + github.com/radovskyb/watcher v1.0.7 +) + +require ( + github.com/gorilla/websocket v1.4.2 // indirect + github.com/smartystreets/goconvey v1.7.2 // indirect + golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365 // indirect + gopkg.in/fsnotify.v1 v1.4.7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..74854b2 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/evanw/esbuild v0.14.5 h1:Gh/vGvDL/g++7erzQZofohZqFBzQblWfLdtYCf15zcQ= +github.com/evanw/esbuild v0.14.5/go.mod h1:GG+zjdi59yh3ehDn4ZWfPcATxjPDUH53iU4ZJbp7dkY= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/goyek/goyek v0.6.0 h1:2YQ4V3X7q+zFF98IBWMc1WRwfzs0TQ8jrwOKY3XRQRk= +github.com/goyek/goyek v0.6.0/go.mod h1:UGjZz3juJL2l2eMqRbxQYjG8ieyKb7WMYPv0KB0KVxA= +github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71 h1:24NdJ5N6gtrcoeS4JwLMeruKFmg20QdF/5UnX5S/j18= +github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71/go.mod h1:ozZLfjiLmXytkIUh200wMeuoQJ4ww06wN+KZtFP6j3g= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= +github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= +github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365 h1:6wSTsvPddg9gc/mVEEyk9oOAoxn+bT4Z9q1zx+4RwA4= +golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/main.go b/main.go new file mode 100644 index 0000000..130d482 --- /dev/null +++ b/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "os/signal" + "syscall" + "time" + + "github.com/evanw/esbuild/pkg/api" + "github.com/goyek/goyek" + "github.com/jaschaephraim/lrserver" + "github.com/radovskyb/watcher" +) + +var triggerReload = make(chan struct{}) + +type options struct { + ESBuild api.BuildOptions + Watch struct { + Path string + } +} + +func main() { + opts := options{} + cfgContent, err := os.ReadFile("./.gowebbuild.json") + + if err != nil { + fmt.Printf("%+v\n", err) + os.Exit(1) + } + + err = json.Unmarshal(cfgContent, &opts) + if err != nil { + fmt.Printf("%+v\n", err) + os.Exit(1) + } + + flow := &goyek.Flow{} + + flow.Register(goyek.Task{ + Name: "watch-frontend", + Usage: "", + Action: func(tf *goyek.TF) { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + + fmt.Println("Starting live reload server") + + go func() { + w := watcher.New() + w.SetMaxEvents(1) + w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove) + if err := w.AddRecursive(opts.Watch.Path); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + go func() { + for { + select { + case event := <-w.Event: + fmt.Printf("File %s changed\n", event.Name()) + build(opts) + case err := <-w.Error: + fmt.Println(err.Error()) + case <-w.Closed: + return + } + } + }() + + fmt.Printf("Watching %d elements in %s\n", len(w.WatchedFiles()), opts.Watch.Path) + + if err := w.Start(time.Millisecond * 100); err != nil { + fmt.Println(err.Error()) + } + }() + + go func() { + lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort) + + go func() { + for { + <-triggerReload + lr.Reload("") + } + }() + + lr.SetStatusLog(nil) + err := lr.ListenAndServe() + if err != nil { + panic(err) + } + }() + + <-c + fmt.Println("\nExit") + os.Exit(0) + }, + }) + + flow.Main() +} + +func build(opts options) { + result := api.Build(opts.ESBuild) + + if len(result.Errors) > 0 { + os.Exit(1) + } else { + triggerReload <- struct{}{} + } +}