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"
|
2023-09-13 11:45:24 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-12-17 21:39:46 +01:00
|
|
|
"os"
|
2021-12-19 14:31:57 +01:00
|
|
|
"path/filepath"
|
2022-04-18 23:55:46 +02:00
|
|
|
"strings"
|
2023-09-13 11:45:24 +02:00
|
|
|
"time"
|
2021-12-17 21:39:46 +01:00
|
|
|
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
2022-11-29 10:23:42 +01:00
|
|
|
"github.com/jaschaephraim/lrserver"
|
2021-12-19 14:31:57 +01:00
|
|
|
"github.com/otiai10/copy"
|
2023-09-13 11:45:24 +02:00
|
|
|
"github.com/radovskyb/watcher"
|
2022-09-28 10:10:01 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-12-17 21:39:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var triggerReload = make(chan struct{})
|
|
|
|
|
2022-11-29 12:33:02 +01:00
|
|
|
type ESBuildExtended struct {
|
|
|
|
api.BuildOptions
|
|
|
|
PurgeBeforeBuild bool
|
|
|
|
}
|
|
|
|
|
2021-12-17 21:39:46 +01:00
|
|
|
type options struct {
|
2022-11-29 12:33:02 +01:00
|
|
|
ESBuild ESBuildExtended
|
2021-12-17 21:39:46 +01:00
|
|
|
Watch struct {
|
2022-02-01 17:15:14 +01:00
|
|
|
Path string
|
|
|
|
Exclude []string
|
2021-12-17 21:39:46 +01:00
|
|
|
}
|
2022-05-11 12:35:55 +02:00
|
|
|
Serve struct {
|
|
|
|
Path string
|
|
|
|
Port int
|
|
|
|
}
|
2021-12-19 14:31:57 +01:00
|
|
|
Copy []struct {
|
|
|
|
Src string
|
|
|
|
Dest string
|
|
|
|
}
|
2023-09-13 11:45:24 +02:00
|
|
|
Download []struct {
|
|
|
|
Url string
|
|
|
|
Dest string
|
|
|
|
}
|
2022-04-18 23:55:46 +02:00
|
|
|
Replace []struct {
|
|
|
|
Pattern string
|
|
|
|
Search string
|
|
|
|
Replace string
|
|
|
|
}
|
2022-09-01 22:44:20 +02:00
|
|
|
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
|
2021-12-17 21:39:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-09-28 10:10:01 +02:00
|
|
|
cfgParam := &cli.StringFlag{
|
|
|
|
Name: "c",
|
|
|
|
Value: "./.gowebbuild.json",
|
|
|
|
Usage: "path to config file config file.",
|
2022-04-18 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
app := &cli.App{
|
2022-11-29 12:33:02 +01:00
|
|
|
Name: "gowebbuild",
|
|
|
|
Usage: "All in one tool to build web frontend projects.",
|
2024-01-04 21:56:44 +01:00
|
|
|
Version: "4.3.1",
|
2022-11-29 12:33:02 +01:00
|
|
|
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
|
|
|
|
`,
|
2022-09-28 10:10:01 +02:00
|
|
|
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,
|
2022-11-29 10:23:42 +01:00
|
|
|
&cli.UintFlag{
|
|
|
|
Name: "lr-port",
|
|
|
|
Value: uint(lrserver.DefaultPort),
|
|
|
|
Usage: "port for the live reload server",
|
|
|
|
},
|
2022-09-28 10:10:01 +02:00
|
|
|
},
|
|
|
|
Action: watchAction,
|
|
|
|
},
|
|
|
|
|
2023-09-13 11:45:24 +02:00
|
|
|
{
|
|
|
|
Name: "serve",
|
|
|
|
Usage: "serve a directory with a simply http server",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "root",
|
|
|
|
Value: "./",
|
|
|
|
Usage: "folder to serve",
|
|
|
|
},
|
|
|
|
&cli.UintFlag{
|
|
|
|
Name: "port",
|
|
|
|
Value: uint(8080),
|
|
|
|
Usage: "serve directory this on port",
|
|
|
|
},
|
|
|
|
&cli.UintFlag{
|
|
|
|
Name: "lr-port",
|
|
|
|
Value: uint(lrserver.DefaultPort),
|
|
|
|
Usage: "port for the live reload server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
port := ctx.Uint("port")
|
|
|
|
root := ctx.String("root")
|
|
|
|
lrPort := ctx.Uint("lr-port")
|
|
|
|
|
|
|
|
if lrPort != 0 {
|
|
|
|
go func() {
|
|
|
|
w := watcher.New()
|
|
|
|
w.SetMaxEvents(1)
|
|
|
|
w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove)
|
|
|
|
|
|
|
|
if err := w.AddRecursive(root); 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())
|
|
|
|
triggerReload <- struct{}{}
|
|
|
|
case err := <-w.Error:
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
case <-w.Closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
lr := lrserver.New(lrserver.DefaultName, uint16(lrPort))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-triggerReload
|
|
|
|
lr.Reload("")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
lr.SetStatusLog(nil)
|
|
|
|
err := lr.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return Serve(root, port)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Name: "download",
|
|
|
|
Usage: "execute downloads as configured",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cfgParam,
|
|
|
|
},
|
|
|
|
Action: func(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 i := range opts {
|
|
|
|
download(opts[i])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
{
|
|
|
|
Name: "replace",
|
2022-11-29 12:33:02 +01:00
|
|
|
ArgsUsage: "[glob file pattern] [search] [replace]",
|
2022-09-28 10:10:01 +02:00
|
|
|
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")
|
2022-09-01 22:44:20 +02:00
|
|
|
}
|
2022-02-01 17:15:14 +01:00
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
if searchStr == "" {
|
|
|
|
return fmt.Errorf("invalid search string")
|
2022-09-01 22:44:20 +02:00
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
replace(options{
|
|
|
|
Replace: []struct {
|
|
|
|
Pattern string
|
|
|
|
Search string
|
|
|
|
Replace string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Pattern: files,
|
|
|
|
Search: searchStr,
|
|
|
|
Replace: replaceStr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2021-12-17 21:39:46 +01:00
|
|
|
},
|
2022-11-29 10:23:42 +01:00
|
|
|
DefaultCommand: "watch",
|
2022-02-01 17:15:14 +01:00
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-12-17 21:39:46 +01:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:02 +01:00
|
|
|
func purge(opts options) {
|
|
|
|
if opts.ESBuild.PurgeBeforeBuild {
|
|
|
|
if opts.ESBuild.Outdir != "" {
|
2023-09-13 11:45:24 +02:00
|
|
|
fmt.Printf("Purging output folder %s\n", opts.ESBuild.Outdir)
|
2022-11-29 12:33:02 +01:00
|
|
|
os.RemoveAll(opts.ESBuild.Outdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.ESBuild.Outfile != "" {
|
2023-09-13 11:45:24 +02:00
|
|
|
fmt.Printf("Purging output file %s\n", opts.ESBuild.Outfile)
|
2022-11-29 12:33:02 +01:00
|
|
|
os.Remove(opts.ESBuild.Outfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:45:24 +02:00
|
|
|
func download(opts options) {
|
|
|
|
if len(opts.Download) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dl := range opts.Download {
|
|
|
|
if !isDir(filepath.Dir(dl.Dest)) {
|
|
|
|
fmt.Printf("Failed to find destination folder for downloading from %s\n", dl.Url)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(dl.Dest)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create file for downloading from %s: %v\n", dl.Url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
client := http.Client{
|
|
|
|
CheckRedirect: func(r *http.Request, via []*http.Request) error {
|
|
|
|
r.URL.Opaque = r.URL.Path
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Downloading %s to %s\n", dl.Url, dl.Dest)
|
|
|
|
resp, err := client.Get(dl.Url)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to download file from %s: %v\n", dl.Url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(file, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to write file downloaded from %s: %v\n", dl.Url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
func cp(opts options) {
|
|
|
|
if len(opts.Copy) == 0 {
|
|
|
|
fmt.Println("Nothing to copy")
|
|
|
|
return
|
|
|
|
}
|
2023-09-13 11:45:24 +02:00
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
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
|
|
|
|
}
|
2022-05-11 12:35:55 +02:00
|
|
|
|
2022-04-18 23:55:46 +02:00
|
|
|
for _, p := range paths {
|
|
|
|
if !isFile(p) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:45:24 +02:00
|
|
|
read, err := os.ReadFile(p)
|
2022-04-18 23:55:46 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := op.Replace
|
|
|
|
if strings.HasPrefix(op.Replace, "$") {
|
|
|
|
r = os.ExpandEnv(op.Replace)
|
|
|
|
}
|
|
|
|
|
2022-05-11 12:35:55 +02:00
|
|
|
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)
|
2023-09-13 11:45:24 +02:00
|
|
|
err = os.WriteFile(p, []byte(newContents), 0)
|
2022-05-11 12:35:55 +02:00
|
|
|
|
2022-04-18 23:55:46 +02:00
|
|
|
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) {
|
2022-11-29 12:33:02 +01:00
|
|
|
result := api.Build(opts.ESBuild.BuildOptions)
|
2021-12-17 21:39:46 +01:00
|
|
|
|
2021-12-19 14:31:57 +01:00
|
|
|
if len(result.Errors) == 0 {
|
2021-12-17 21:39:46 +01:00
|
|
|
triggerReload <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|