Skip to content

Commit 8120131

Browse files
authored
Use FORCE_COLOR environmental variable to force colorized output (#7033)
* Use FORCE_COLOR environmental variable to force colorized output * Update CHANGELOG.md
1 parent f3917c9 commit 8120131

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 12.0.0-alpha.4 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- Use FORCE_COLOR environmental variable to force colorized output https://github.com/rescript-lang/rescript-compiler/pull/7033
18+
1519
#### :bug: Bug fix
1620

1721
- Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024

cli/rescript_bsb.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function clean(args) {
110110
delegate(["clean", ...args]);
111111
}
112112

113-
const isTtyError = process.stderr.isTTY;
114-
const isTtyStd = process.stdout.isTTY;
113+
const shouldColorizeError = process.stderr.isTTY || process.env.FORCE_COLOR == "1";
114+
const shouldColorize = process.stdout.isTTY || process.env.FORCE_COLOR == "1";
115115

116116
/**
117117
* @type {[number,number]}
@@ -133,7 +133,7 @@ function logFinishCompiling(code) {
133133
if (code) {
134134
log = log + " (exit: " + code + ")";
135135
}
136-
if (isTtyStd) {
136+
if (shouldColorize) {
137137
log = "\x1b[36m" + log + "\x1b[0m";
138138
}
139139
if (code) {
@@ -146,7 +146,7 @@ function logFinishCompiling(code) {
146146
function logStartCompiling() {
147147
updateStartTime();
148148
let log = `>>>> Start compiling`;
149-
if (isTtyStd) {
149+
if (shouldColorize) {
150150
log = "\x1b[36m" + log + "\x1b[0m";
151151
}
152152
console.log(log);
@@ -265,7 +265,7 @@ function watch(args) {
265265
.on("error", function (err) {
266266
// @ts-ignore
267267
if (err !== undefined && err.code === "EADDRINUSE") {
268-
var error = isTtyStd ? `\x1b[1;31mERROR:\x1b[0m` : `ERROR:`;
268+
var error = shouldColorize ? `\x1b[1;31mERROR:\x1b[0m` : `ERROR:`;
269269
console.error(`${error} The websocket port number ${webSocketPort} is in use.
270270
Please pick a different one using the \`-ws [host:]port\` flag from bsb.`);
271271
} else {
@@ -358,7 +358,7 @@ Please pick a different one using the \`-ws [host:]port\` flag from bsb.`);
358358
* @param highlight {string}
359359
*/
360360
function outputError(error, highlight) {
361-
if (isTtyError && highlight) {
361+
if (shouldColorizeError && highlight) {
362362
process.stderr.write(
363363
error.replace(highlight, "\x1b[1;31m" + highlight + "\x1b[0m"),
364364
);

rescript

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ var bsb = require("./cli/rescript_bsb.js");
1414
var cwd = process.cwd();
1515
process.env.BSB_PROJECT_ROOT = cwd;
1616

17-
if (process.env.NINJA_ANSI_FORCED === undefined) {
17+
if (process.env.FORCE_COLOR === undefined) {
1818
if (require("tty").isatty(1)) {
19+
process.env.FORCE_COLOR = "1";
1920
process.env.NINJA_ANSI_FORCED = "1";
2021
}
2122
} else {
23+
if (process.env.FORCE_COLOR === "1" && process.env.NINJA_ANSI_FORCED === undefined) {
24+
process.env.NINJA_ANSI_FORCED = "1";
25+
}
2226
if (process.argv.includes("-verbose")) {
23-
console.log(`NINJA_ANSI_FORCED: "${process.env.NINJA_ANSI_FORCED}"`);
27+
console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`);
2428
}
2529
}
2630

0 commit comments

Comments
 (0)