Skip to content

Commit 5b01210

Browse files
Rework color detection (#87)
* add node v20 to the testing matrix * use process.stdout.isTTY instead of require * properly test env variables * adding empty NO_COLOR test * github actions again? * remove unnecessary alias * make nonempty testing easier since env variables always string|undefined
1 parent ef5553b commit 5b01210

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

.github/workflows/testing.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
node-version:
16+
- 20
1617
- 18
1718
- 16
1819
- 14

picocolors.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
let argv = process.argv || [],
2-
env = process.env
1+
let p = process || {},
2+
argv = p.argv || [],
3+
env = p.env || {}
34
let isColorSupported =
4-
!("NO_COLOR" in env || argv.includes("--no-color")) &&
5-
("FORCE_COLOR" in env ||
5+
!(!!env.NO_COLOR || argv.includes("--no-color")) &&
6+
(!!env.FORCE_COLOR ||
67
argv.includes("--color") ||
7-
process.platform === "win32" ||
8-
(require != null && require("tty").isatty(1) && env.TERM !== "dumb") ||
9-
"CI" in env)
8+
p.platform === "win32" ||
9+
((p.stdout || {}).isTTY && env.TERM !== "dumb") ||
10+
!!env.CI)
1011

1112
let formatter =
1213
(open, close, replace = open) =>

tests/environments.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ let fs = require("fs")
33
let pc = require("../picocolors.js")
44
let assert = require("assert")
55
let source = fs.readFileSync(__dirname + "/../picocolors.js", "utf-8")
6+
let CI = process.env.CI
67

78
test("ci server", () => {
89
let pc = initModuleEnv({ env: { TERM: "dumb", CI: "1" } })
@@ -22,6 +23,12 @@ test("env NO_COLOR", () => {
2223
assert.equal(pc.red("text"), pc.createColors(false).red("text"))
2324
})
2425

26+
test("env NO_COLOR empty", () => {
27+
let pc = initModuleEnv({ env: { NO_COLOR: "", CI } })
28+
assert.equal(pc.isColorSupported, true)
29+
assert.equal(pc.red("text"), pc.createColors(true).red("text"))
30+
})
31+
2532
test("env FORCE_COLOR", () => {
2633
let pc = initModuleEnv({ env: { TERM: "dumb", FORCE_COLOR: "1" } })
2734
assert.equal(pc.isColorSupported, true)
@@ -62,8 +69,14 @@ function test(name, fn) {
6269
}
6370
}
6471

65-
function initModuleEnv({ env, argv = [], platform = "darwin", require = global.require }) {
66-
let process = { env, argv, platform }
72+
function initModuleEnv({
73+
env,
74+
argv = [],
75+
platform = "darwin",
76+
require = global.require,
77+
stdout = process.stdout,
78+
}) {
79+
let process = { env, argv, platform, stdout }
6780
let context = vm.createContext({ require, process, module: { exports: {} } })
6881
let script = new vm.Script(source)
6982
script.runInContext(context)

0 commit comments

Comments
 (0)