Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
8433170681 | |||
bea89e2a80 | |||
a0d81ca2be | |||
30ab451d3a | |||
8e7d4d2978 | |||
ea1d5ea911 | |||
12de91324e | |||
2bd82e1981 | |||
311339685c |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
vendor/
|
31
build.go
Normal file
31
build.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/evanw/esbuild/pkg/api"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func buildAction(ctx *cli.Context) error {
|
||||||
|
cfgPath := ctx.String("c")
|
||||||
|
os.Chdir(filepath.Dir(cfgPath))
|
||||||
|
opts := readCfg(cfgPath)
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
cp(o)
|
||||||
|
|
||||||
|
if ctx.Bool("p") {
|
||||||
|
o.ESBuild.MinifyIdentifiers = true
|
||||||
|
o.ESBuild.MinifySyntax = true
|
||||||
|
o.ESBuild.MinifyWhitespace = true
|
||||||
|
o.ESBuild.Sourcemap = api.SourceMapNone
|
||||||
|
}
|
||||||
|
|
||||||
|
api.Build(o.ESBuild)
|
||||||
|
replace(o)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
20
go.mod
20
go.mod
@ -1,17 +1,25 @@
|
|||||||
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/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.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
|
||||||
|
github.com/urfave/cli/v2 v2.16.3 // indirect
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // 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
|
||||||
)
|
)
|
||||||
|
38
go.sum
38
go.sum
@ -1,26 +1,48 @@
|
|||||||
github.com/evanw/esbuild v0.14.5 h1:Gh/vGvDL/g++7erzQZofohZqFBzQblWfLdtYCf15zcQ=
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||||
github.com/evanw/esbuild v0.14.5/go.mod h1:GG+zjdi59yh3ehDn4ZWfPcATxjPDUH53iU4ZJbp7dkY=
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/evanw/esbuild v0.14.50 h1:h7sijkRPGB9ckpIOc6FMZ81/NMy/4g40LhsBAtPa3/I=
|
||||||
|
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/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
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=
|
||||||
|
github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk=
|
||||||
|
github.com/urfave/cli/v2 v2.16.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||||
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=
|
||||||
|
128
linker.go
Normal file
128
linker.go
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
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] {
|
||||||
|
pp, err := filepath.Abs(filepath.Dir(packageFiles[i]))
|
||||||
|
if err == nil {
|
||||||
|
packages[name] = pp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Found %d npm packages to monitor for changes.\n", len(packages))
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
285
main.go
285
main.go
@ -2,16 +2,16 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"path/filepath"
|
||||||
"syscall"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/evanw/esbuild/pkg/api"
|
"github.com/evanw/esbuild/pkg/api"
|
||||||
"github.com/goyek/goyek"
|
"github.com/otiai10/copy"
|
||||||
"github.com/jaschaephraim/lrserver"
|
"github.com/urfave/cli/v2"
|
||||||
"github.com/radovskyb/watcher"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var triggerReload = make(chan struct{})
|
var triggerReload = make(chan struct{})
|
||||||
@ -20,97 +20,224 @@ 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{}
|
cfgParam := &cli.StringFlag{
|
||||||
cfgContent, err := os.ReadFile("./.gowebbuild.json")
|
Name: "c",
|
||||||
|
Value: "./.gowebbuild.json",
|
||||||
if err != nil {
|
Usage: "path to config file config file.",
|
||||||
fmt.Printf("%+v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(cfgContent, &opts)
|
app := &cli.App{
|
||||||
if err != nil {
|
Commands: []*cli.Command{
|
||||||
fmt.Printf("%+v\n", err)
|
{
|
||||||
os.Exit(1)
|
Name: "build",
|
||||||
|
Usage: "build web sources one time and exit",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cfgParam,
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "p",
|
||||||
|
Value: false,
|
||||||
|
Usage: "use production ready build settings",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: buildAction,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Name: "watch",
|
||||||
|
Usage: "watch for changes and trigger the build",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cfgParam,
|
||||||
|
},
|
||||||
|
Action: watchAction,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Name: "replace",
|
||||||
|
ArgsUsage: "[files] [search] [replace]",
|
||||||
|
Usage: "replace text in files",
|
||||||
|
Action: func(ctx *cli.Context) error {
|
||||||
|
files := ctx.Args().Get(0)
|
||||||
|
searchStr := ctx.Args().Get(1)
|
||||||
|
replaceStr := ctx.Args().Get(2)
|
||||||
|
|
||||||
|
if files == "" {
|
||||||
|
return fmt.Errorf("invalid file pattern")
|
||||||
}
|
}
|
||||||
|
|
||||||
flow := &goyek.Flow{}
|
if searchStr == "" {
|
||||||
|
return fmt.Errorf("invalid search string")
|
||||||
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() {
|
replace(options{
|
||||||
for {
|
Replace: []struct {
|
||||||
select {
|
Pattern string
|
||||||
case event := <-w.Event:
|
Search string
|
||||||
fmt.Printf("File %s changed\n", event.Name())
|
Replace string
|
||||||
build(opts)
|
}{
|
||||||
case err := <-w.Error:
|
{
|
||||||
fmt.Println(err.Error())
|
Pattern: files,
|
||||||
case <-w.Closed:
|
Search: searchStr,
|
||||||
return
|
Replace: replaceStr,
|
||||||
}
|
},
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
flow.Main()
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
123
watch.go
Normal file
123
watch.go
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jaschaephraim/lrserver"
|
||||||
|
"github.com/radovskyb/watcher"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func watchAction(ctx *cli.Context) error {
|
||||||
|
cfgPath := ctx.String("c")
|
||||||
|
os.Chdir(filepath.Dir(cfgPath))
|
||||||
|
optsSetups := readCfg(cfgPath)
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
|
for i := range optsSetups {
|
||||||
|
opts := optsSetups[i]
|
||||||
|
|
||||||
|
go func(opts options) {
|
||||||
|
w := watcher.New()
|
||||||
|
w.SetMaxEvents(1)
|
||||||
|
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 {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-w.Event:
|
||||||
|
fmt.Printf("File %s changed\n", event.Name())
|
||||||
|
cp(opts)
|
||||||
|
build(opts)
|
||||||
|
replace(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)
|
||||||
|
|
||||||
|
cp(opts)
|
||||||
|
build(opts)
|
||||||
|
replace(opts)
|
||||||
|
|
||||||
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
||||||
|
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() {
|
||||||
|
for range reqBuildCh {
|
||||||
|
cp(opts)
|
||||||
|
build(opts)
|
||||||
|
replace(opts)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
fmt.Println("Starting live reload server")
|
||||||
|
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("\nStopped watching")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user