-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_env.go
35 lines (30 loc) · 783 Bytes
/
handle_env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package caddy_esbuild_plugin
import (
"github.com/joho/godotenv"
"go.uber.org/zap"
"os"
"strings"
)
func (m *Esbuild) handleEnv() {
process := Process{Env: map[string]string{}}
currentEnv := os.Getenv("NODE_ENV")
if currentEnv == "" {
currentEnv = "development"
}
_ = godotenv.Load(".env." + currentEnv + ".local")
_ = godotenv.Load(".env.local")
_ = godotenv.Load(".env." + currentEnv)
err := godotenv.Load()
process.Env["NODE_ENV"] = os.Getenv("NODE_ENV")
if err != nil {
m.logger.Error("Failed to load env", zap.Error(err))
} else {
for _, pair := range os.Environ() {
item := strings.SplitN(pair, "=", 2)
key := item[0]
val := item[1]
val = strings.ReplaceAll(val, "\"", "\\\"")
m.Defines["process.env."+key] = "\"" + val + "\""
}
}
}