diff --git a/build.go b/build.go index 1d183ab..af99525 100644 --- a/build.go +++ b/build.go @@ -7,15 +7,12 @@ import ( "path/filepath" "github.com/evanw/esbuild/pkg/api" + "github.com/trading-peter/gowebbuild/fsutils" "github.com/urfave/cli/v2" ) func buildAction(ctx *cli.Context) error { - cfgPath, err := filepath.Abs(ctx.String("c")) - - if err != nil { - return err - } + cfgPath := fsutils.ResolvePath(ctx.String("c")) os.Chdir(filepath.Dir(cfgPath)) opts := readCfg(cfgPath) diff --git a/config.go b/config.go index fcd37df..e0823ec 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/evanw/esbuild/pkg/api" + "github.com/trading-peter/gowebbuild/fsutils" "gopkg.in/yaml.v3" ) @@ -121,6 +122,11 @@ func readCfg(cfgPath string) []options { optsSetups = append(optsSetups, opt) } + // Process all paths in each options setup + for i := range optsSetups { + processPaths(&optsSetups[i]) + } + return optsSetups } @@ -148,3 +154,43 @@ func readJsonCfg(cfgPath string) []options { return optsSetups } + +func processPaths(opts *options) { + // ESBuild paths + for i, entry := range opts.ESBuild.EntryPoints { + opts.ESBuild.EntryPoints[i] = fsutils.ResolvePath(entry) + } + opts.ESBuild.Outdir = fsutils.ResolvePath(opts.ESBuild.Outdir) + opts.ESBuild.Outfile = fsutils.ResolvePath(opts.ESBuild.Outfile) + + // Watch paths + for i, path := range opts.Watch.Paths { + opts.Watch.Paths[i] = fsutils.ResolvePath(path) + } + for i, path := range opts.Watch.Exclude { + opts.Watch.Exclude[i] = fsutils.ResolvePath(path) + } + + // Serve path + opts.Serve.Path = fsutils.ResolvePath(opts.Serve.Path) + + // Copy paths + for i := range opts.Copy { + opts.Copy[i].Src = fsutils.ResolvePath(opts.Copy[i].Src) + opts.Copy[i].Dest = fsutils.ResolvePath(opts.Copy[i].Dest) + } + + // Download paths + for i := range opts.Download { + opts.Download[i].Dest = fsutils.ResolvePath(opts.Download[i].Dest) + } + + // Link paths + opts.Link.From = fsutils.ResolvePath(opts.Link.From) + opts.Link.To = fsutils.ResolvePath(opts.Link.To) + + // Npm proxy paths + for i := range opts.NpmProxy.Overrides { + opts.NpmProxy.Overrides[i].PackageRoot = fsutils.ResolvePath(opts.NpmProxy.Overrides[i].PackageRoot) + } +} diff --git a/fsutils/helpers.go b/fsutils/helpers.go index f21324b..8e06941 100644 --- a/fsutils/helpers.go +++ b/fsutils/helpers.go @@ -2,6 +2,7 @@ package fsutils import ( "errors" + "fmt" "io/fs" "os" "path/filepath" @@ -46,3 +47,29 @@ func IsDir(path string) bool { return err == nil && stat.IsDir() } + +func ResolvePath(path string) string { + // We assume that the user doesn't use the involved feature if the path is empty. + if path == "" { + return "" + } + + expandedPath := os.ExpandEnv(path) + + if strings.HasPrefix(expandedPath, "~") { + homeDir, err := os.UserHomeDir() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + expandedPath = filepath.Join(homeDir, expandedPath[1:]) + } + + path, err := filepath.Abs(expandedPath) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + return path +} diff --git a/main.go b/main.go index d511c37..dffdb6a 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { app := &cli.App{ Name: "gowebbuild", Usage: "All in one tool to build web frontend projects.", - Version: "6.1.3", + Version: "6.2.1", Authors: []*cli.Author{{ Name: "trading-peter (https://github.com/trading-peter)", }},