Allow for ${ENV_VARIABLES} in paths. Also ~ is now supported, even on windows.

This commit is contained in:
trading_peter 2024-11-21 11:19:28 +01:00
parent 48b15de4c5
commit 4b0bfa8e4c
4 changed files with 76 additions and 6 deletions

View File

@ -7,15 +7,12 @@ import (
"path/filepath" "path/filepath"
"github.com/evanw/esbuild/pkg/api" "github.com/evanw/esbuild/pkg/api"
"github.com/trading-peter/gowebbuild/fsutils"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
func buildAction(ctx *cli.Context) error { func buildAction(ctx *cli.Context) error {
cfgPath, err := filepath.Abs(ctx.String("c")) cfgPath := fsutils.ResolvePath(ctx.String("c"))
if err != nil {
return err
}
os.Chdir(filepath.Dir(cfgPath)) os.Chdir(filepath.Dir(cfgPath))
opts := readCfg(cfgPath) opts := readCfg(cfgPath)

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/evanw/esbuild/pkg/api" "github.com/evanw/esbuild/pkg/api"
"github.com/trading-peter/gowebbuild/fsutils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -121,6 +122,11 @@ func readCfg(cfgPath string) []options {
optsSetups = append(optsSetups, opt) optsSetups = append(optsSetups, opt)
} }
// Process all paths in each options setup
for i := range optsSetups {
processPaths(&optsSetups[i])
}
return optsSetups return optsSetups
} }
@ -148,3 +154,43 @@ func readJsonCfg(cfgPath string) []options {
return optsSetups 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)
}
}

View File

@ -2,6 +2,7 @@ package fsutils
import ( import (
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -46,3 +47,29 @@ func IsDir(path string) bool {
return err == nil && stat.IsDir() 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
}

View File

@ -26,7 +26,7 @@ func main() {
app := &cli.App{ app := &cli.App{
Name: "gowebbuild", Name: "gowebbuild",
Usage: "All in one tool to build web frontend projects.", Usage: "All in one tool to build web frontend projects.",
Version: "6.1.3", Version: "6.2.1",
Authors: []*cli.Author{{ Authors: []*cli.Author{{
Name: "trading-peter (https://github.com/trading-peter)", Name: "trading-peter (https://github.com/trading-peter)",
}}, }},