Allow for ${ENV_VARIABLES} in paths. Also ~ is now supported, even on windows.
This commit is contained in:
parent
48b15de4c5
commit
4b0bfa8e4c
7
build.go
7
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)
|
||||
|
46
config.go
46
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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user