From f081b97beb402c12f704532d95366787b93f9b12 Mon Sep 17 00:00:00 2001 From: pk Date: Tue, 29 Nov 2022 12:33:02 +0100 Subject: [PATCH] Add purge feature --- build.go | 3 ++- main.go | 45 ++++++++++++++++++++++++++++++++++++++++++--- watch.go | 1 + 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/build.go b/build.go index 297e99a..0058f4e 100644 --- a/build.go +++ b/build.go @@ -19,6 +19,7 @@ func buildAction(ctx *cli.Context) error { opts := readCfg(cfgPath) for _, o := range opts { + purge(o) cp(o) if ctx.Bool("p") { @@ -28,7 +29,7 @@ func buildAction(ctx *cli.Context) error { o.ESBuild.Sourcemap = api.SourceMapNone } - api.Build(o.ESBuild) + api.Build(o.ESBuild.BuildOptions) replace(o) } diff --git a/main.go b/main.go index 171d9ae..860cc2c 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,13 @@ import ( var triggerReload = make(chan struct{}) +type ESBuildExtended struct { + api.BuildOptions + PurgeBeforeBuild bool +} + type options struct { - ESBuild api.BuildOptions + ESBuild ESBuildExtended Watch struct { Path string Exclude []string @@ -75,6 +80,28 @@ func main() { } app := &cli.App{ + Name: "gowebbuild", + Usage: "All in one tool to build web frontend projects.", + Version: "4.1.0", + 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", @@ -106,7 +133,7 @@ func main() { { Name: "replace", - ArgsUsage: "[files] [search] [replace]", + ArgsUsage: "[glob file pattern] [search] [replace]", Usage: "replace text in files", Action: func(ctx *cli.Context) error { files := ctx.Args().Get(0) @@ -146,6 +173,18 @@ func main() { } } +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") @@ -242,7 +281,7 @@ func isDir(path string) bool { } func build(opts options) { - result := api.Build(opts.ESBuild) + result := api.Build(opts.ESBuild.BuildOptions) if len(result.Errors) == 0 { triggerReload <- struct{}{} diff --git a/watch.go b/watch.go index 8a65d90..fabe85a 100644 --- a/watch.go +++ b/watch.go @@ -49,6 +49,7 @@ func watchAction(ctx *cli.Context) error { select { case event := <-w.Event: fmt.Printf("File %s changed\n", event.Name()) + purge(opts) cp(opts) build(opts) replace(opts)