Add purge feature

This commit is contained in:
trading_peter 2022-11-29 12:33:02 +01:00
parent 427e287895
commit f081b97beb
3 changed files with 45 additions and 4 deletions

View File

@ -19,6 +19,7 @@ func buildAction(ctx *cli.Context) error {
opts := readCfg(cfgPath) opts := readCfg(cfgPath)
for _, o := range opts { for _, o := range opts {
purge(o)
cp(o) cp(o)
if ctx.Bool("p") { if ctx.Bool("p") {
@ -28,7 +29,7 @@ func buildAction(ctx *cli.Context) error {
o.ESBuild.Sourcemap = api.SourceMapNone o.ESBuild.Sourcemap = api.SourceMapNone
} }
api.Build(o.ESBuild) api.Build(o.ESBuild.BuildOptions)
replace(o) replace(o)
} }

45
main.go
View File

@ -17,8 +17,13 @@ import (
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 Exclude []string
@ -75,6 +80,28 @@ func main() {
} }
app := &cli.App{ 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{ Commands: []*cli.Command{
{ {
Name: "build", Name: "build",
@ -106,7 +133,7 @@ func main() {
{ {
Name: "replace", Name: "replace",
ArgsUsage: "[files] [search] [replace]", ArgsUsage: "[glob file pattern] [search] [replace]",
Usage: "replace text in files", Usage: "replace text in files",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
files := ctx.Args().Get(0) 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) { func cp(opts options) {
if len(opts.Copy) == 0 { if len(opts.Copy) == 0 {
fmt.Println("Nothing to copy") fmt.Println("Nothing to copy")
@ -242,7 +281,7 @@ func isDir(path string) bool {
} }
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 {
triggerReload <- struct{}{} triggerReload <- struct{}{}

View File

@ -49,6 +49,7 @@ func watchAction(ctx *cli.Context) error {
select { select {
case event := <-w.Event: case event := <-w.Event:
fmt.Printf("File %s changed\n", event.Name()) fmt.Printf("File %s changed\n", event.Name())
purge(opts)
cp(opts) cp(opts)
build(opts) build(opts)
replace(opts) replace(opts)