Skip to content

Commit 962b4eb

Browse files
committed
fix: respect --no-color and NO_COLOR
The color guard checked argv.noColor, which yargs never sets: --no-color parses to color=false. It only worked because chalk sniffs process.argv itself, so FORCE_COLOR overrode it and NO_COLOR was ignored entirely.
1 parent 555cc4a commit 962b4eb

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/argv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Argv {
105105
}
106106

107107
private constructor (argv: any, writeStreams?: WriteStreams) {
108-
if (argv.noColor) {
108+
if (argv.noColor || argv.color === false || (process.env.NO_COLOR ?? "") !== "") {
109109
chalkBase.level = 0;
110110
}
111111
this.writeStreams = writeStreams;

tests/argv-color.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import "../src/global.js";
2+
import chalkBase from "chalk";
3+
import {Argv} from "../src/argv.js";
4+
import {WriteStreamsMock} from "../src/write-streams.js";
5+
6+
let writeStreams: WriteStreamsMock;
7+
let originalLevel: number;
8+
let originalNoColor: string | undefined;
9+
10+
beforeEach(() => {
11+
writeStreams = new WriteStreamsMock();
12+
originalLevel = chalkBase.level;
13+
originalNoColor = process.env.NO_COLOR;
14+
});
15+
16+
afterEach(() => {
17+
chalkBase.level = originalLevel;
18+
if (originalNoColor === undefined) {
19+
delete process.env.NO_COLOR;
20+
} else {
21+
process.env.NO_COLOR = originalNoColor;
22+
}
23+
});
24+
25+
test("color stays enabled by default", async () => {
26+
chalkBase.level = 2;
27+
delete process.env.NO_COLOR;
28+
await Argv.build({}, writeStreams);
29+
expect(chalkBase.level).toBe(2);
30+
});
31+
32+
test("--no-color disables color", async () => {
33+
chalkBase.level = 2;
34+
delete process.env.NO_COLOR;
35+
await Argv.build({color: false}, writeStreams);
36+
expect(chalkBase.level).toBe(0);
37+
});
38+
39+
test("NO_COLOR disables color", async () => {
40+
chalkBase.level = 2;
41+
process.env.NO_COLOR = "1";
42+
await Argv.build({}, writeStreams);
43+
expect(chalkBase.level).toBe(0);
44+
});
45+
46+
test("an empty NO_COLOR leaves color enabled", async () => {
47+
chalkBase.level = 2;
48+
process.env.NO_COLOR = "";
49+
await Argv.build({}, writeStreams);
50+
expect(chalkBase.level).toBe(2);
51+
});

0 commit comments

Comments
 (0)