Skip to content

Commit 7136d84

Browse files
committed
cli: respect the "NO_COLOR" convention
1 parent 3acd834 commit 7136d84

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Add support for the `NO_COLOR` environment variable
6+
7+
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.
8+
39
## 0.11.23
410

511
* Add a shim function for unbundled uses of `require` ([#1202](https://github.com/evanw/esbuild/issues/1202))

cmd/esbuild/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import (
1313
)
1414

1515
var helpText = func(colors logger.Colors) string {
16+
// Read "NO_COLOR" from the environment. This is a convention that some
17+
// software follows. See https://no-color.org/ for more information.
18+
for _, key := range os.Environ() {
19+
if strings.HasPrefix(key, "NO_COLOR=") {
20+
colors = logger.Colors{}
21+
break
22+
}
23+
}
24+
1625
return `
1726
` + colors.Bold + `Usage:` + colors.Reset + `
1827
esbuild [options] [entry points]

pkg/cli/cli_impl.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ func runImpl(osArgs []string) int {
626626

627627
switch {
628628
case buildOptions != nil:
629-
// Read the "NODE_PATH" from the environment. This is part of node's
630-
// module resolution algorithm. Documentation for this can be found here:
631-
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
632629
for _, key := range os.Environ() {
630+
// Read the "NODE_PATH" from the environment. This is part of node's
631+
// module resolution algorithm. Documentation for this can be found here:
632+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
633633
if strings.HasPrefix(key, "NODE_PATH=") {
634634
value := key[len("NODE_PATH="):]
635635
separator := ":"
@@ -640,6 +640,12 @@ func runImpl(osArgs []string) int {
640640
buildOptions.NodePaths = strings.Split(value, separator)
641641
break
642642
}
643+
644+
// Read "NO_COLOR" from the environment. This is a convention that some
645+
// software follows. See https://no-color.org/ for more information.
646+
if buildOptions.Color == api.ColorIfTerminal && strings.HasPrefix(key, "NO_COLOR=") {
647+
buildOptions.Color = api.ColorNever
648+
}
643649
}
644650

645651
// Read from stdin when there are no entry points

0 commit comments

Comments
 (0)