2022-09-28 10:10:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-04 10:56:41 +02:00
|
|
|
"fmt"
|
2022-09-28 10:10:01 +02:00
|
|
|
"os"
|
2024-07-04 10:56:41 +02:00
|
|
|
"os/exec"
|
2022-09-28 10:10:01 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
2024-11-21 11:19:28 +01:00
|
|
|
"github.com/trading-peter/gowebbuild/fsutils"
|
2022-09-28 10:10:01 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func buildAction(ctx *cli.Context) error {
|
2024-11-21 11:19:28 +01:00
|
|
|
cfgPath := fsutils.ResolvePath(ctx.String("c"))
|
2022-11-29 10:23:42 +01:00
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
os.Chdir(filepath.Dir(cfgPath))
|
|
|
|
opts := readCfg(cfgPath)
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2023-09-13 11:45:24 +02:00
|
|
|
if ctx.Bool("p") {
|
|
|
|
download(o)
|
|
|
|
}
|
2022-11-29 12:33:02 +01:00
|
|
|
purge(o)
|
2022-09-28 10:10:01 +02:00
|
|
|
cp(o)
|
|
|
|
|
2024-08-26 23:12:33 +02:00
|
|
|
esBuildCfg := cfgToESBuildCfg(o)
|
|
|
|
|
2022-09-28 10:10:01 +02:00
|
|
|
if ctx.Bool("p") {
|
2024-08-26 23:12:33 +02:00
|
|
|
esBuildCfg.MinifyIdentifiers = true
|
|
|
|
esBuildCfg.MinifySyntax = true
|
|
|
|
esBuildCfg.MinifyWhitespace = true
|
|
|
|
esBuildCfg.Sourcemap = api.SourceMapNone
|
2022-09-28 10:10:01 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 23:12:33 +02:00
|
|
|
api.Build(esBuildCfg)
|
2022-09-28 10:10:01 +02:00
|
|
|
replace(o)
|
2024-07-04 10:56:41 +02:00
|
|
|
|
|
|
|
if ctx.Bool("p") && o.ProductionBuildOptions.CmdPostBuild != "" {
|
2024-11-06 10:43:05 +01:00
|
|
|
defer func() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
2024-07-04 10:56:41 +02:00
|
|
|
}
|
2022-09-28 10:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|