From 30f8be3d5d40102187c57310423c08793eaaf898 Mon Sep 17 00:00:00 2001 From: pk Date: Thu, 4 Jul 2024 10:56:41 +0200 Subject: [PATCH] Add post build command and exclude the esbuild outdir in the watcher. --- build.go | 15 +++++++++++++++ main.go | 7 +++++-- sample.gowebbuild.json | 5 ++++- watch.go | 14 ++++++++++---- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/build.go b/build.go index 637aab7..7ec7a05 100644 --- a/build.go +++ b/build.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "os" + "os/exec" "path/filepath" "github.com/evanw/esbuild/pkg/api" @@ -34,6 +36,19 @@ func buildAction(ctx *cli.Context) error { api.Build(o.ESBuild.BuildOptions) replace(o) + + if ctx.Bool("p") && o.ProductionBuildOptions.CmdPostBuild != "" { + fmt.Printf("Executing post production build command `%s`\n", o.ProductionBuildOptions.CmdPostBuild) + cmd := exec.Command("sh", "-c", o.ProductionBuildOptions.CmdPostBuild) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + + if err != nil { + fmt.Printf("Failed to execute post production build command `%s`: %+v\n", o.ProductionBuildOptions.CmdPostBuild, err) + os.Exit(1) + } + } } return nil diff --git a/main.go b/main.go index 207ce66..c218f26 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ type ESBuildExtended struct { type options struct { ESBuild ESBuildExtended Watch struct { - Path string + Paths []string Exclude []string } Serve struct { @@ -52,6 +52,9 @@ type options struct { From string To string } + ProductionBuildOptions struct { + CmdPostBuild string + } } func readCfg(cfgPath string) []options { @@ -89,7 +92,7 @@ func main() { app := &cli.App{ Name: "gowebbuild", Usage: "All in one tool to build web frontend projects.", - Version: "4.3.1", + Version: "4.4.0", Authors: []*cli.Author{{ Name: "trading-peter (https://github.com/trading-peter)", }}, diff --git a/sample.gowebbuild.json b/sample.gowebbuild.json index ff7b285..68ceacc 100644 --- a/sample.gowebbuild.json +++ b/sample.gowebbuild.json @@ -1,6 +1,6 @@ { "Watch": { - "Path": "./frontend/src", + "Paths": [ "./frontend/src" ], "Exclude": [ "./dist" ] }, "Copy": [ @@ -45,5 +45,8 @@ "Bundle": true, "Write": true, "LogLevel": 3 + }, + "ProductionBuildOptions": { + "CmdPostBuild": "my-build-script.sh" } } \ No newline at end of file diff --git a/watch.go b/watch.go index 10f375e..5d4a18c 100644 --- a/watch.go +++ b/watch.go @@ -38,9 +38,15 @@ func watchAction(ctx *cli.Context) error { w.Ignore(opts.Watch.Exclude...) } - if err := w.AddRecursive(opts.Watch.Path); err != nil { - fmt.Println(err.Error()) - os.Exit(1) + if opts.ESBuild.Outdir != "" { + w.Ignore(opts.ESBuild.Outdir) + } + + for _, p := range opts.Watch.Paths { + if err := w.AddRecursive(p); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } } go func() { @@ -60,7 +66,7 @@ func watchAction(ctx *cli.Context) error { } }() - 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.Paths) purge(opts) cp(opts)