Improve the replace feature to support env variables

This commit is contained in:
trading_peter 2022-04-18 23:55:46 +02:00
parent ea1d5ea911
commit 8e7d4d2978
2 changed files with 97 additions and 0 deletions

90
main.go
View File

@ -4,9 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings"
"syscall" "syscall"
"time" "time"
@ -29,6 +31,11 @@ type options struct {
Src string Src string
Dest string Dest string
} }
Replace []struct {
Pattern string
Search string
Replace string
}
} }
func main() { func main() {
@ -41,6 +48,45 @@ func main() {
Default: "./.gowebbuild.json", Default: "./.gowebbuild.json",
}) })
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)
},
}
watchFrontend := goyek.Task{ watchFrontend := goyek.Task{
Name: "watch-frontend", Name: "watch-frontend",
Usage: "", Usage: "",
@ -86,6 +132,7 @@ func main() {
fmt.Printf("File %s changed\n", event.Name()) fmt.Printf("File %s changed\n", event.Name())
cp(opts) 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:
@ -98,6 +145,7 @@ func main() {
cp(opts) cp(opts)
build(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())
@ -128,6 +176,7 @@ func main() {
} }
flow.DefaultTask = flow.Register(watchFrontend) flow.DefaultTask = flow.Register(watchFrontend)
flow.Register(buildOnly)
flow.Main() flow.Main()
} }
@ -161,6 +210,47 @@ func cp(opts options) {
} }
} }
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)
}
}
}
}
}
func isFile(path string) bool { func isFile(path string) bool {
stat, err := os.Stat(path) stat, err := os.Stat(path)

View File

@ -25,6 +25,13 @@
"Dest": "./api/frontend-dist" "Dest": "./api/frontend-dist"
} }
], ],
"Replace": [
{
"Pattern": "*.go|*.js|*.html",
"Search": "Something",
"Replace": "This"
}
],
"ESBuild": { "ESBuild": {
"EntryPoints": [ "EntryPoints": [
"./frontend/src/the-app.js", "./frontend/src/the-app.js",