Skip to content

Commit

Permalink
cli: respect the "NO_COLOR" convention
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 17, 2021
1 parent 3acd834 commit 7136d84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Add support for the `NO_COLOR` environment variable

The CLI will now omit color if the `NO_COLOR` environment variable is present, which is an existing convention that is followed by some other software. See https://no-color.org/ for more information.

## 0.11.23

* Add a shim function for unbundled uses of `require` ([#1202](https://github.com/evanw/esbuild/issues/1202))
Expand Down
9 changes: 9 additions & 0 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ 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
}
}

return `
` + colors.Bold + `Usage:` + colors.Reset + `
esbuild [options] [entry points]
Expand Down
12 changes: 9 additions & 3 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,10 @@ func runImpl(osArgs []string) int {

switch {
case buildOptions != nil:
// 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
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 := ":"
Expand All @@ -640,6 +640,12 @@ func runImpl(osArgs []string) int {
buildOptions.NodePaths = strings.Split(value, separator)
break
}

// Read "NO_COLOR" from the environment. This is a convention that some
// software follows. See https://no-color.org/ for more information.
if buildOptions.Color == api.ColorIfTerminal && strings.HasPrefix(key, "NO_COLOR=") {
buildOptions.Color = api.ColorNever
}
}

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

0 comments on commit 7136d84

Please sign in to comment.