12 Commits
1.0.0 ... 4.1.1

Author SHA1 Message Date
pk
760decfb85 Ignore hidden files 2022-12-15 12:17:04 +01:00
pk
f081b97beb Add purge feature 2022-11-29 12:33:02 +01:00
pk
427e287895 Add default command back in 2022-11-29 10:23:42 +01:00
pk
8433170681 New command structure. Add replace command. 2022-09-28 10:10:01 +02:00
pk
bea89e2a80 Support watching for modified npm packages
Allow array of build configs.
2022-09-01 22:44:20 +02:00
a0d81ca2be Switch repo 2022-06-01 14:58:23 +02:00
30ab451d3a Add simple serve 2022-05-11 12:35:55 +02:00
8e7d4d2978 Improve the replace feature to support env variables 2022-04-18 23:55:46 +02:00
ea1d5ea911 Merge remote-tracking branch 'origin/master' 2022-02-01 17:16:01 +01:00
12de91324e Add -c param and exclusion 2022-02-01 17:15:14 +01:00
2bd82e1981 Create copy destination if it doesn't exist. 2022-01-20 20:59:48 +01:00
311339685c Add support for copying files and folders. 2021-12-19 14:31:57 +01:00
8 changed files with 639 additions and 89 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor/

37
build.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"os"
"path/filepath"
"github.com/evanw/esbuild/pkg/api"
"github.com/urfave/cli/v2"
)
func buildAction(ctx *cli.Context) error {
cfgPath, err := filepath.Abs(ctx.String("c"))
if err != nil {
return err
}
os.Chdir(filepath.Dir(cfgPath))
opts := readCfg(cfgPath)
for _, o := range opts {
purge(o)
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.BuildOptions)
replace(o)
}
return nil
}

20
go.mod
View File

@ -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
View File

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

129
linker.go Normal file
View File

@ -0,0 +1,129 @@
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)
w.IgnoreHiddenFiles(true)
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)
}

333
main.go
View File

@ -2,115 +2,288 @@ 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/jaschaephraim/lrserver" "github.com/jaschaephraim/lrserver"
"github.com/radovskyb/watcher" "github.com/otiai10/copy"
"github.com/urfave/cli/v2"
) )
var triggerReload = make(chan struct{}) var triggerReload = make(chan struct{})
type ESBuildExtended struct {
api.BuildOptions
PurgeBeforeBuild bool
}
type options struct { type options struct {
ESBuild api.BuildOptions ESBuild ESBuildExtended
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 { Name: "gowebbuild",
fmt.Printf("%+v\n", err) Usage: "All in one tool to build web frontend projects.",
os.Exit(1) Version: "4.1.1",
Authors: []*cli.Author{{
Name: "trading-peter (https://github.com/trading-peter)",
}},
UsageText: `gowebbuild [global options] command [command options] [arguments...]
Examples:
Watch project and rebuild if a files changes:
$ gowebbuild
Use a different name or path for the config file (working directory is always the location of the config file):
$ gowebbuild -c watch
Production build:
$ gowebbuild build -p
Manually replace a string within some files (not limited to project directory):
$ gowebbuild replace *.go foo bar
`,
Commands: []*cli.Command{
{
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,
&cli.UintFlag{
Name: "lr-port",
Value: uint(lrserver.DefaultPort),
Usage: "port for the live reload server",
},
},
Action: watchAction,
},
{
Name: "replace",
ArgsUsage: "[glob file pattern] [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
},
},
},
DefaultCommand: "watch",
}
flow.Main() if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}
func purge(opts options) {
if opts.ESBuild.PurgeBeforeBuild {
if opts.ESBuild.Outdir != "" {
os.RemoveAll(opts.ESBuild.Outdir)
}
if opts.ESBuild.Outfile != "" {
os.Remove(opts.ESBuild.Outfile)
}
}
}
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.BuildOptions)
if len(result.Errors) > 0 { if len(result.Errors) == 0 {
os.Exit(1)
} else {
triggerReload <- struct{}{} triggerReload <- struct{}{}
} }
} }

49
sample.gowebbuild.json Normal file
View 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
}
}

130
watch.go Normal file
View File

@ -0,0 +1,130 @@
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, err := filepath.Abs(ctx.String("c"))
if err != nil {
return err
}
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())
purge(opts)
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")
port := ctx.Uint("lr-port")
lr := lrserver.New(lrserver.DefaultName, uint16(port))
go func() {
for {
<-triggerReload
lr.Reload("")
}
}()
lr.SetStatusLog(nil)
err := lr.ListenAndServe()
if err != nil {
panic(err)
}
}()
<-c
fmt.Println("\nStopped watching")
return nil
}