Skip to content

Commit

Permalink
refactor: os.LookupEnv (#2708)
Browse files Browse the repository at this point in the history
  • Loading branch information
daychongyang committed Dec 3, 2022
1 parent cfac504 commit cbc7aae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
7 changes: 2 additions & 5 deletions cmd/esbuild/main.go
Expand Up @@ -16,11 +16,8 @@ import (
var helpText = func(colors logger.Colors) string {
// Read "NO_COLOR" from the environment. This is a convention that some
// software follows. See https://no-color.org/ for more information.
for _, key := range os.Environ() {
if strings.HasPrefix(key, "NO_COLOR=") {
colors = logger.Colors{}
break
}
if _, ok := os.LookupEnv("NO_COLOR"); ok {
colors = logger.Colors{}
}

return `
Expand Down
17 changes: 6 additions & 11 deletions internal/logger/logger.go
Expand Up @@ -133,11 +133,8 @@ func isProbablyWindowsCommandPrompt() bool {
// because that means we're running in the new Windows Terminal instead.
if runtime.GOOS == "windows" {
windowsCommandPrompt.isProbablyCMD = true
for _, env := range os.Environ() {
if strings.HasPrefix(env, "WT_SESSION=") {
windowsCommandPrompt.isProbablyCMD = false
break
}
if _, ok := os.LookupEnv("WT_SESSION"); ok {
windowsCommandPrompt.isProbablyCMD = false
}
}
}
Expand Down Expand Up @@ -261,12 +258,10 @@ var noColorOnce sync.Once

func hasNoColorEnvironmentVariable() bool {
noColorOnce.Do(func() {
for _, key := range os.Environ() {
// Read "NO_COLOR" from the environment. This is a convention that some
// software follows. See https://no-color.org/ for more information.
if strings.HasPrefix(key, "NO_COLOR=") {
noColorResult = true
}
// Read "NO_COLOR" from the environment. This is a convention that some
// software follows. See https://no-color.org/ for more information.
if _, ok := os.LookupEnv("NO_COLOR"); ok {
noColorResult = true
}
})
return noColorResult
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api_impl.go
Expand Up @@ -921,8 +921,8 @@ func printSummary(logOptions logger.OutputOptions, outputFiles []OutputFile, sta

// Don't print the time taken by the build if we're running under Yarn 1
// since Yarn 1 always prints its own copy of the time taken by each command
for _, env := range os.Environ() {
if strings.HasPrefix(env, "npm_config_user_agent=") && strings.Contains(env, "yarn/1.") {
if userAgent, ok := os.LookupEnv("npm_config_user_agent"); ok {
if strings.Contains(userAgent, "yarn/1.") {
logger.PrintSummary(logOptions.Color, table, nil)
return
}
Expand Down
24 changes: 10 additions & 14 deletions pkg/cli/cli_impl.go
Expand Up @@ -1064,20 +1064,16 @@ func runImpl(osArgs []string) int {

switch {
case buildOptions != nil:
for _, key := range os.Environ() {
// Read the "NODE_PATH" from the environment. This is part of node's
// module resolution algorithm. Documentation for this can be found here:
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
if strings.HasPrefix(key, "NODE_PATH=") {
value := key[len("NODE_PATH="):]
separator := ":"
if fs.CheckIfWindows() {
// On Windows, NODE_PATH is delimited by semicolons instead of colons
separator = ";"
}
buildOptions.NodePaths = splitWithEmptyCheck(value, separator)
break
}
// Read the "NODE_PATH" from the environment. This is part of node's
// module resolution algorithm. Documentation for this can be found here:
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
if value, ok := os.LookupEnv("NODE_PATH"); ok {
separator := ":"
if fs.CheckIfWindows() {
// On Windows, NODE_PATH is delimited by semicolons instead of colons
separator = ";"
}
buildOptions.NodePaths = splitWithEmptyCheck(value, separator)
}

// Read from stdin when there are no entry points
Expand Down

0 comments on commit cbc7aae

Please sign in to comment.