3 Commits
4.0.0 ... 4.1.1

Author SHA1 Message Date
pk
760decfb85 Ignore hidden files 2022-12-15 12:17:04 +01:00
pk
f081b97beb Add purge feature 2022-11-29 12:33:02 +01:00
pk
427e287895 Add default command back in 2022-11-29 10:23:42 +01:00
4 changed files with 67 additions and 7 deletions

View File

@ -9,11 +9,17 @@ import (
)
func buildAction(ctx *cli.Context) error {
cfgPath := ctx.String("c")
cfgPath, err := filepath.Abs(ctx.String("c"))
if err != nil {
return err
}
os.Chdir(filepath.Dir(cfgPath))
opts := readCfg(cfgPath)
for _, o := range opts {
purge(o)
cp(o)
if ctx.Bool("p") {
@ -23,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)
}

View File

@ -45,6 +45,7 @@ func link(from, to string) chan struct{} {
w := watcher.New()
w.SetMaxEvents(1)
w.FilterOps(watcher.Write, watcher.Rename, watcher.Move, watcher.Create, watcher.Remove)
w.IgnoreHiddenFiles(true)
if err := w.AddRecursive(from); err != nil {
fmt.Println(err.Error())

52
main.go
View File

@ -10,14 +10,20 @@ import (
"strings"
"github.com/evanw/esbuild/pkg/api"
"github.com/jaschaephraim/lrserver"
"github.com/otiai10/copy"
"github.com/urfave/cli/v2"
)
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
@ -74,6 +80,28 @@ func main() {
}
app := &cli.App{
Name: "gowebbuild",
Usage: "All in one tool to build web frontend projects.",
Version: "4.1.1",
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",
@ -94,13 +122,18 @@ func main() {
Usage: "watch for changes and trigger the build",
Flags: []cli.Flag{
cfgParam,
&cli.UintFlag{
Name: "lr-port",
Value: uint(lrserver.DefaultPort),
Usage: "port for the live reload server",
},
},
Action: watchAction,
},
{
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)
@ -132,6 +165,7 @@ func main() {
},
},
},
DefaultCommand: "watch",
}
if err := app.Run(os.Args); err != nil {
@ -139,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")
@ -235,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{}{}

View File

@ -15,7 +15,12 @@ import (
)
func watchAction(ctx *cli.Context) error {
cfgPath := ctx.String("c")
cfgPath, err := filepath.Abs(ctx.String("c"))
if err != nil {
return err
}
os.Chdir(filepath.Dir(cfgPath))
optsSetups := readCfg(cfgPath)
@ -44,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)
@ -100,7 +106,8 @@ func watchAction(ctx *cli.Context) error {
go func() {
fmt.Println("Starting live reload server")
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
port := ctx.Uint("lr-port")
lr := lrserver.New(lrserver.DefaultName, uint16(port))
go func() {
for {