2021-12-17 21:39:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-02-01 17:15:14 +01:00
|
|
|
"errors"
|
2021-12-17 21:39:46 +01:00
|
|
|
"fmt"
|
2022-04-18 23:55:46 +02:00
|
|
|
"io/ioutil"
|
2021-12-17 21:39:46 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-12-19 14:31:57 +01:00
|
|
|
"path/filepath"
|
2022-04-18 23:55:46 +02:00
|
|
|
"strings"
|
2021-12-17 21:39:46 +01:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
|
|
|
"github.com/goyek/goyek"
|
|
|
|
"github.com/jaschaephraim/lrserver"
|
2021-12-19 14:31:57 +01:00
|
|
|
"github.com/otiai10/copy"
|
2021-12-17 21:39:46 +01:00
|
|
|
"github.com/radovskyb/watcher"
|
|
|
|
)
|
|
|
|
|
|
|
|
var triggerReload = make(chan struct{})
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
ESBuild api.BuildOptions
|
|
|
|
Watch struct {
|
2022-02-01 17:15:14 +01:00
|
|
|
Path string
|
|
|
|
Exclude []string
|
2021-12-17 21:39:46 +01:00
|
|
|
}
|
2021-12-19 14:31:57 +01:00
|
|
|
Copy []struct {
|
|
|
|
Src string
|
|
|
|
Dest string
|
|
|
|
}
|
2022-04-18 23:55:46 +02:00
|
|
|
Replace []struct {
|
|
|
|
Pattern string
|
|
|
|
Search string
|
|
|
|
Replace string
|
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-02-01 17:15:14 +01:00
|
|
|
flow := &goyek.Flow{}
|
2021-12-17 21:39:46 +01:00
|
|
|
opts := options{}
|
|
|
|
|
2022-02-01 17:15:14 +01:00
|
|
|
cfgPathParam := flow.RegisterStringParam(goyek.StringParam{
|
|
|
|
Name: "c",
|
|
|
|
Usage: "Path to config file config file.",
|
|
|
|
Default: "./.gowebbuild.json",
|
|
|
|
})
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2022-04-18 23:55:46 +02:00
|
|
|
prodParam := flow.RegisterBoolParam(goyek.BoolParam{
|
|
|
|
Name: "p",
|
|
|
|
Usage: "Use production ready build settings",
|
|
|
|
Default: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
buildOnly := goyek.Task{
|
|
|
|
Name: "build",
|
|
|
|
Usage: "",
|
|
|
|
Params: goyek.Params{cfgPathParam, prodParam},
|
|
|
|
Action: func(tf *goyek.TF) {
|
|
|
|
cfgPath := cfgPathParam.Get(tf)
|
|
|
|
cfgContent, err := os.ReadFile(cfgPath)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
cp(opts)
|
|
|
|
replace(opts)
|
|
|
|
|
|
|
|
if prodParam.Get(tf) {
|
|
|
|
opts.ESBuild.MinifyIdentifiers = true
|
|
|
|
opts.ESBuild.MinifySyntax = true
|
|
|
|
opts.ESBuild.MinifyWhitespace = true
|
|
|
|
opts.ESBuild.Sourcemap = api.SourceMapNone
|
|
|
|
}
|
|
|
|
|
|
|
|
api.Build(opts.ESBuild)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:15:14 +01:00
|
|
|
watchFrontend := goyek.Task{
|
|
|
|
Name: "watch-frontend",
|
|
|
|
Usage: "",
|
|
|
|
Params: goyek.Params{cfgPathParam},
|
|
|
|
Action: func(tf *goyek.TF) {
|
|
|
|
cfgPath := cfgPathParam.Get(tf)
|
|
|
|
cfgContent, err := os.ReadFile(cfgPath)
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2022-02-01 17:15:14 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
|
|
|
|
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)
|
2022-02-01 17:15:14 +01:00
|
|
|
|
|
|
|
if len(opts.Watch.Exclude) > 0 {
|
|
|
|
w.Ignore(opts.Watch.Exclude...)
|
|
|
|
}
|
|
|
|
|
2021-12-17 21:39:46 +01:00
|
|
|
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())
|
2021-12-19 14:31:57 +01:00
|
|
|
cp(opts)
|
2021-12-17 21:39:46 +01:00
|
|
|
build(opts)
|
2022-04-18 23:55:46 +02:00
|
|
|
replace(opts)
|
2021-12-17 21:39:46 +01:00
|
|
|
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)
|
|
|
|
|
2022-02-01 17:15:14 +01:00
|
|
|
cp(opts)
|
|
|
|
build(opts)
|
2022-04-18 23:55:46 +02:00
|
|
|
replace(opts)
|
2022-02-01 17:15:14 +01:00
|
|
|
|
2021-12-17 21:39:46 +01:00
|
|
|
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)
|
|
|
|
},
|
2022-02-01 17:15:14 +01:00
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2022-02-01 17:15:14 +01:00
|
|
|
flow.DefaultTask = flow.Register(watchFrontend)
|
2022-04-18 23:55:46 +02:00
|
|
|
flow.Register(buildOnly)
|
2021-12-17 21:39:46 +01:00
|
|
|
flow.Main()
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
func cp(opts options) {
|
|
|
|
if len(opts.Copy) == 0 {
|
|
|
|
fmt.Println("Nothing to copy")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, op := range opts.Copy {
|
|
|
|
paths, err := filepath.Glob(op.Src)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Invalid glob pattern: %s\n", op.Src)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
destIsDir := isDir(op.Dest)
|
|
|
|
for _, p := range paths {
|
|
|
|
d := op.Dest
|
|
|
|
|
|
|
|
if destIsDir && isFile(p) {
|
|
|
|
d = filepath.Join(d, filepath.Base(p))
|
|
|
|
}
|
|
|
|
err := copy.Copy(p, d)
|
|
|
|
fmt.Printf("Copying %s to %s\n", p, d)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to copy %s: %v\n", p, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:55:46 +02:00
|
|
|
func replace(opts options) {
|
|
|
|
if len(opts.Replace) == 0 {
|
|
|
|
fmt.Println("Nothing to replace")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, op := range opts.Replace {
|
|
|
|
paths, err := filepath.Glob(op.Pattern)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Invalid glob pattern: %s\n", op.Pattern)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Paths: %+v\n", paths)
|
|
|
|
for _, p := range paths {
|
|
|
|
if !isFile(p) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := op.Replace
|
|
|
|
if strings.HasPrefix(op.Replace, "$") {
|
|
|
|
r = os.ExpandEnv(op.Replace)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(string(read), op.Search) {
|
|
|
|
fmt.Printf("Replacing '%s' with '%s' in %s\n", op.Search, r, p)
|
|
|
|
newContents := strings.Replace(string(read), op.Search, r, -1)
|
|
|
|
err = ioutil.WriteFile(p, []byte(newContents), 0)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
func isFile(path string) bool {
|
2022-02-01 17:15:14 +01:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
return !stat.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDir(path string) bool {
|
2022-02-01 17:15:14 +01:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
os.MkdirAll(path, 0755)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return err == nil && stat.IsDir()
|
2021-12-19 14:31:57 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 21:39:46 +01:00
|
|
|
func build(opts options) {
|
|
|
|
result := api.Build(opts.ESBuild)
|
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
if len(result.Errors) == 0 {
|
2021-12-17 21:39:46 +01:00
|
|
|
triggerReload <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|