Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f081b97beb | |||
427e287895 |
10
build.go
10
build.go
@ -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)
|
||||
}
|
||||
|
||||
|
52
main.go
52
main.go
@ -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.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",
|
||||
@ -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{}{}
|
||||
|
11
watch.go
11
watch.go
@ -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 {
|
||||
|
Reference in New Issue
Block a user