Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
bea89e2a80 | |||
a0d81ca2be | |||
30ab451d3a | |||
8e7d4d2978 | |||
ea1d5ea911 | |||
12de91324e | |||
2bd82e1981 | |||
311339685c |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
vendor/
|
16
go.mod
16
go.mod
@ -1,17 +1,21 @@
|
|||||||
module gitlab.codeblob.work/pk/gowebbuild
|
module github.com/trading-peter/gowebbuild
|
||||||
|
|
||||||
go 1.17
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/evanw/esbuild v0.14.5
|
github.com/evanw/esbuild v0.14.50
|
||||||
github.com/goyek/goyek v0.6.0
|
github.com/goyek/goyek v0.6.3
|
||||||
github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71
|
github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71
|
||||||
|
github.com/otiai10/copy v1.7.0
|
||||||
github.com/radovskyb/watcher v1.0.7
|
github.com/radovskyb/watcher v1.0.7
|
||||||
|
github.com/tidwall/gjson v1.14.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/smartystreets/goconvey v1.7.2 // indirect
|
github.com/smartystreets/goconvey v1.7.2 // indirect
|
||||||
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
||||||
)
|
)
|
||||||
|
30
go.sum
30
go.sum
@ -1,26 +1,40 @@
|
|||||||
github.com/evanw/esbuild v0.14.5 h1:Gh/vGvDL/g++7erzQZofohZqFBzQblWfLdtYCf15zcQ=
|
github.com/evanw/esbuild v0.14.50 h1:h7sijkRPGB9ckpIOc6FMZ81/NMy/4g40LhsBAtPa3/I=
|
||||||
github.com/evanw/esbuild v0.14.5/go.mod h1:GG+zjdi59yh3ehDn4ZWfPcATxjPDUH53iU4ZJbp7dkY=
|
github.com/evanw/esbuild v0.14.50/go.mod h1:dkwI35DCMf0iR+tJDiCEiPKZ4A+AotmmeLpPEv3dl9k=
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
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/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.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/goyek/goyek v0.6.0 h1:2YQ4V3X7q+zFF98IBWMc1WRwfzs0TQ8jrwOKY3XRQRk=
|
github.com/goyek/goyek v0.6.3 h1:t0h3gWdlvGeSChltiyAyka9Mlcp3CEPDRssRf0XHDTM=
|
||||||
github.com/goyek/goyek v0.6.0/go.mod h1:UGjZz3juJL2l2eMqRbxQYjG8ieyKb7WMYPv0KB0KVxA=
|
github.com/goyek/goyek v0.6.3/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 h1:24NdJ5N6gtrcoeS4JwLMeruKFmg20QdF/5UnX5S/j18=
|
||||||
github.com/jaschaephraim/lrserver v0.0.0-20171129202958-50d19f603f71/go.mod h1:ozZLfjiLmXytkIUh200wMeuoQJ4ww06wN+KZtFP6j3g=
|
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 h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
|
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
|
||||||
|
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
|
||||||
|
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
|
||||||
|
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
|
||||||
|
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
||||||
|
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
|
||||||
|
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
|
||||||
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
|
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
|
||||||
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
|
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 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
|
||||||
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
|
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 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
|
||||||
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
|
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
|
||||||
|
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
|
||||||
|
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
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/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-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-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
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=
|
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 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
|
123
linker.go
Normal file
123
linker.go
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/otiai10/copy"
|
||||||
|
"github.com/radovskyb/watcher"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
)
|
||||||
|
|
||||||
|
func link(from, to string) chan struct{} {
|
||||||
|
requestBuildCh := make(chan struct{})
|
||||||
|
|
||||||
|
// Load package.json in destination.
|
||||||
|
destPkg := readFileContent(filepath.Join(to, "package.json"))
|
||||||
|
depsRaw := gjson.Get(destPkg, "dependencies").Map()
|
||||||
|
deps := map[string]bool{}
|
||||||
|
for k := range depsRaw {
|
||||||
|
deps[k] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
packages := map[string]string{}
|
||||||
|
packageFiles := findFiles(from, "package.json")
|
||||||
|
|
||||||
|
for i := range packageFiles {
|
||||||
|
content := readFileContent(packageFiles[i])
|
||||||
|
name := gjson.Get(content, "name").String()
|
||||||
|
|
||||||
|
if deps[name] {
|
||||||
|
packages[name] = filepath.Dir(packageFiles[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
w := watcher.New()
|
||||||
|
w.SetMaxEvents(1)
|
||||||
|
w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove)
|
||||||
|
|
||||||
|
if err := w.AddRecursive(from); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-w.Event:
|
||||||
|
fmt.Printf("File %s changed\n", event.Path)
|
||||||
|
for k, v := range packages {
|
||||||
|
if strings.HasPrefix(event.Path, v) {
|
||||||
|
src := filepath.Dir(event.Path)
|
||||||
|
dest := filepath.Join(to, "node_modules", k)
|
||||||
|
fmt.Printf("Copying %s to %s\n", src, dest)
|
||||||
|
err := copy.Copy(src, dest, copy.Options{
|
||||||
|
Skip: func(src string) (bool, error) {
|
||||||
|
ok, _ := filepath.Match("*.js", filepath.Base(src))
|
||||||
|
if ok && !strings.Contains(src, "node_modules") {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
},
|
||||||
|
Sync: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to copy %s: %v\n", k, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBuildCh <- struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-w.Error:
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
case <-w.Closed:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
fmt.Printf("Watching packages in %s\n", from)
|
||||||
|
|
||||||
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return requestBuildCh
|
||||||
|
}
|
||||||
|
|
||||||
|
func findFiles(root, name string) []string {
|
||||||
|
paths := []string{}
|
||||||
|
|
||||||
|
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !d.IsDir() && filepath.Base(path) == name && !strings.Contains(path, "node_modules") {
|
||||||
|
paths = append(paths, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
|
func readFileContent(path string) string {
|
||||||
|
pkgData, err := os.ReadFile(path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(pkgData)
|
||||||
|
}
|
261
main.go
261
main.go
@ -2,15 +2,21 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/evanw/esbuild/pkg/api"
|
"github.com/evanw/esbuild/pkg/api"
|
||||||
"github.com/goyek/goyek"
|
"github.com/goyek/goyek"
|
||||||
"github.com/jaschaephraim/lrserver"
|
"github.com/jaschaephraim/lrserver"
|
||||||
|
"github.com/otiai10/copy"
|
||||||
"github.com/radovskyb/watcher"
|
"github.com/radovskyb/watcher"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,39 +26,116 @@ type options struct {
|
|||||||
ESBuild api.BuildOptions
|
ESBuild api.BuildOptions
|
||||||
Watch struct {
|
Watch struct {
|
||||||
Path string
|
Path string
|
||||||
|
Exclude []string
|
||||||
}
|
}
|
||||||
|
Serve struct {
|
||||||
|
Path string
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
Copy []struct {
|
||||||
|
Src string
|
||||||
|
Dest string
|
||||||
|
}
|
||||||
|
Replace []struct {
|
||||||
|
Pattern string
|
||||||
|
Search string
|
||||||
|
Replace string
|
||||||
|
}
|
||||||
|
Link struct {
|
||||||
|
From string
|
||||||
|
To string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readCfg(cfgPath string) []options {
|
||||||
|
cfgContent, err := os.ReadFile(cfgPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
optsSetups := []options{}
|
||||||
|
|
||||||
|
err = json.Unmarshal(cfgContent, &optsSetups)
|
||||||
|
if err != nil {
|
||||||
|
opt := options{}
|
||||||
|
err = json.Unmarshal(cfgContent, &opt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
optsSetups = append(optsSetups, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return optsSetups
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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 := &goyek.Flow{}
|
||||||
|
|
||||||
flow.Register(goyek.Task{
|
cfgPathParam := flow.RegisterStringParam(goyek.StringParam{
|
||||||
Name: "watch-frontend",
|
Name: "c",
|
||||||
|
Usage: "Path to config file config file.",
|
||||||
|
Default: "./.gowebbuild.json",
|
||||||
|
})
|
||||||
|
|
||||||
|
prodParam := flow.RegisterBoolParam(goyek.BoolParam{
|
||||||
|
Name: "p",
|
||||||
|
Usage: "Use production ready build settings",
|
||||||
|
Default: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
buildOnly := goyek.Task{
|
||||||
|
Name: "build",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
|
Params: goyek.Params{cfgPathParam, prodParam},
|
||||||
Action: func(tf *goyek.TF) {
|
Action: func(tf *goyek.TF) {
|
||||||
|
cfgPath := cfgPathParam.Get(tf)
|
||||||
|
os.Chdir(filepath.Dir(cfgPath))
|
||||||
|
opts := readCfg(cfgPath)
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
cp(o)
|
||||||
|
|
||||||
|
if prodParam.Get(tf) {
|
||||||
|
o.ESBuild.MinifyIdentifiers = true
|
||||||
|
o.ESBuild.MinifySyntax = true
|
||||||
|
o.ESBuild.MinifyWhitespace = true
|
||||||
|
o.ESBuild.Sourcemap = api.SourceMapNone
|
||||||
|
}
|
||||||
|
|
||||||
|
api.Build(o.ESBuild)
|
||||||
|
replace(o)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
watch := goyek.Task{
|
||||||
|
Name: "watch",
|
||||||
|
Usage: "",
|
||||||
|
Params: goyek.Params{cfgPathParam},
|
||||||
|
Action: func(tf *goyek.TF) {
|
||||||
|
cfgPath := cfgPathParam.Get(tf)
|
||||||
|
os.Chdir(filepath.Dir(cfgPath))
|
||||||
|
optsSetups := readCfg(cfgPath)
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
fmt.Println("Starting live reload server")
|
for i := range optsSetups {
|
||||||
|
opts := optsSetups[i]
|
||||||
|
|
||||||
go func() {
|
go func(opts options) {
|
||||||
w := watcher.New()
|
w := watcher.New()
|
||||||
w.SetMaxEvents(1)
|
w.SetMaxEvents(1)
|
||||||
w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove)
|
w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove)
|
||||||
|
|
||||||
|
if len(opts.Watch.Exclude) > 0 {
|
||||||
|
w.Ignore(opts.Watch.Exclude...)
|
||||||
|
}
|
||||||
|
|
||||||
if err := w.AddRecursive(opts.Watch.Path); err != nil {
|
if err := w.AddRecursive(opts.Watch.Path); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -63,7 +146,9 @@ func main() {
|
|||||||
select {
|
select {
|
||||||
case event := <-w.Event:
|
case event := <-w.Event:
|
||||||
fmt.Printf("File %s changed\n", event.Name())
|
fmt.Printf("File %s changed\n", event.Name())
|
||||||
|
cp(opts)
|
||||||
build(opts)
|
build(opts)
|
||||||
|
replace(opts)
|
||||||
case err := <-w.Error:
|
case err := <-w.Error:
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
case <-w.Closed:
|
case <-w.Closed:
|
||||||
@ -74,12 +159,49 @@ func main() {
|
|||||||
|
|
||||||
fmt.Printf("Watching %d elements in %s\n", len(w.WatchedFiles()), opts.Watch.Path)
|
fmt.Printf("Watching %d elements in %s\n", len(w.WatchedFiles()), opts.Watch.Path)
|
||||||
|
|
||||||
|
cp(opts)
|
||||||
|
build(opts)
|
||||||
|
replace(opts)
|
||||||
|
|
||||||
if err := w.Start(time.Millisecond * 100); err != nil {
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
}(opts)
|
||||||
|
|
||||||
|
if opts.Serve.Path != "" {
|
||||||
|
go func() {
|
||||||
|
port := 8888
|
||||||
|
if opts.Serve.Port != 0 {
|
||||||
|
port = opts.Serve.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Handle("/", http.FileServer(http.Dir(opts.Serve.Path)))
|
||||||
|
|
||||||
|
fmt.Printf("Serving contents of %s at :%d\n", opts.Serve.Path, port)
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Link.From != "" {
|
||||||
|
reqBuildCh := link(opts.Link.From, opts.Link.To)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
for range reqBuildCh {
|
||||||
|
cp(opts)
|
||||||
|
build(opts)
|
||||||
|
replace(opts)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
fmt.Println("Starting live reload server")
|
||||||
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
|
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -100,17 +222,112 @@ func main() {
|
|||||||
fmt.Println("\nExit")
|
fmt.Println("\nExit")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
|
flow.DefaultTask = flow.Register(watch)
|
||||||
|
flow.Register(buildOnly)
|
||||||
flow.Main()
|
flow.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
count := strings.Count(string(read), op.Search)
|
||||||
|
|
||||||
|
if count > 0 {
|
||||||
|
fmt.Printf("Replacing %d occurrences of '%s' with '%s' in %s\n", count, op.Search, r, p)
|
||||||
|
newContents := strings.ReplaceAll(string(read), op.Search, r)
|
||||||
|
err = ioutil.WriteFile(p, []byte(newContents), 0)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isFile(path string) bool {
|
||||||
|
stat, err := os.Stat(path)
|
||||||
|
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return !stat.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isDir(path string) bool {
|
||||||
|
stat, err := os.Stat(path)
|
||||||
|
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
os.MkdirAll(path, 0755)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return err == nil && stat.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
func build(opts options) {
|
func build(opts options) {
|
||||||
result := api.Build(opts.ESBuild)
|
result := api.Build(opts.ESBuild)
|
||||||
|
|
||||||
if len(result.Errors) > 0 {
|
if len(result.Errors) == 0 {
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
triggerReload <- struct{}{}
|
triggerReload <- struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
49
sample.gowebbuild.json
Normal file
49
sample.gowebbuild.json
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
{
|
||||||
|
"Watch": {
|
||||||
|
"Path": "./frontend/src",
|
||||||
|
"Exclude": [ "./dist" ]
|
||||||
|
},
|
||||||
|
"Copy": [
|
||||||
|
{
|
||||||
|
"Src": "./frontend/index.html",
|
||||||
|
"Dest": "./api/frontend-dist"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Src": "./frontend/src/audio",
|
||||||
|
"Dest": "./api/frontend-dist/audio"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Src": "./frontend/src/icon-*.png",
|
||||||
|
"Dest": "./api/frontend-dist"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Src": "./frontend/src/manifest.webmanifest",
|
||||||
|
"Dest": "./api/frontend-dist"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Src": "./frontend/src/workbox-config.js",
|
||||||
|
"Dest": "./api/frontend-dist"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Replace": [
|
||||||
|
{
|
||||||
|
"Pattern": "*.go|*.js|*.html",
|
||||||
|
"Search": "Something",
|
||||||
|
"Replace": "This"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"ESBuild": {
|
||||||
|
"EntryPoints": [
|
||||||
|
"./frontend/src/the-app.js",
|
||||||
|
"./frontend/src/serviceworker.js"
|
||||||
|
],
|
||||||
|
"Outdir": "./api/frontend-dist",
|
||||||
|
"Sourcemap": 1,
|
||||||
|
"Format": 3,
|
||||||
|
"Splitting": true,
|
||||||
|
"Platform": 0,
|
||||||
|
"Bundle": true,
|
||||||
|
"Write": true,
|
||||||
|
"LogLevel": 3
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user