diff --git a/lib/Compile.js b/lib/Compile.js index 67be3723..307335df 100644 --- a/lib/Compile.js +++ b/lib/Compile.js @@ -1,6 +1,6 @@ //@flow -const { supportsColor } = require('chalk'); +const { supportsColor } = require('./chalk'); const spawn = require('cross-spawn'); const ElmCompiler = require('./ElmCompiler'); const Report = require('./Report'); diff --git a/lib/Generate.js b/lib/Generate.js index e6aec134..71e81d13 100644 --- a/lib/Generate.js +++ b/lib/Generate.js @@ -1,6 +1,6 @@ // @flow -const { supportsColor } = require('chalk'); +const { supportsColor } = require('./chalk'); const fs = require('fs'); const path = require('path'); const { DependencyProvider } = require('./DependencyProvider.js'); diff --git a/lib/RunTests.js b/lib/RunTests.js index 42e34320..98aa06c0 100644 --- a/lib/RunTests.js +++ b/lib/RunTests.js @@ -1,6 +1,6 @@ // @flow -const chalk = require('chalk'); +const chalk = require('./chalk'); const chokidar = require('chokidar'); const path = require('path'); const readline = require('readline'); diff --git a/lib/Supervisor.js b/lib/Supervisor.js index 22ddbf2f..a6cef1d6 100644 --- a/lib/Supervisor.js +++ b/lib/Supervisor.js @@ -1,6 +1,6 @@ // @flow -const chalk = require('chalk'); +const chalk = require('./chalk'); const child_process = require('child_process'); const fs = require('fs'); const net = require('net'); diff --git a/lib/chalk.js b/lib/chalk.js new file mode 100644 index 00000000..2f439597 --- /dev/null +++ b/lib/chalk.js @@ -0,0 +1,20 @@ +// @flow +const supportsColor = require('./supports-color'); + +// Find more colors/styles in +// https://github.com/chalk/ansi-styles/blob/main/index.js + +function red(string /*: string */) /*: string */ { + return supportsColor ? `\x1B[31m${string}\x1B[39m` : string; +} + +function blue(string /*: string */) /*: string */ { + return supportsColor ? `\x1B[34m${string}\x1B[39m` : string; +} + +module.exports = { + supportsColor, + + red, + blue, +}; diff --git a/lib/supports-color.js b/lib/supports-color.js new file mode 100644 index 00000000..b37762b4 --- /dev/null +++ b/lib/supports-color.js @@ -0,0 +1,76 @@ +// @flow +/** +Based on https://github.com/chalk/supports-color v10.2.2 +but adapted to use CommonJs and simplified to our needs (only knowing whether stdout supports colors). + +MIT License + +Copyright (c) Sindre Sorhus (https://sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +const { env } = process; + +function supportsColor() /*: boolean */ { + if ('FORCE_COLOR' in env && env.FORCE_COLOR !== '') { + return env.FORCE_COLOR !== 'false' && env.FORCE_COLOR !== '0'; + } + + if (process.argv.includes('--no-color')) { + return false; + } else if (process.argv.includes('--color')) { + return true; + } + + // Here’s the original comment from the supports-color package: + // “Check for Azure DevOps pipelines. Has to be above the tty check.” + // This is pretty weird. CI systems typically redirect the output to a non-terminal + // to be able to capture it and display it on the web. This means that `isTTY` is false. + // However, many CI systems support terminal colors on the web to some extent. + // Apparently Azure DevOps does and the supports-color authors decided to let it show + // colors even when `isTTY` is false. But a weird thing with that is that if you + // run `elm-test | some-program` locally and in CI, `some-program` will _not_ see + // color escape codes locally (as expected), but _will_ see them in CI! + // GitHub Actions also supports colors. But the color support as of 2026-07-06 was + // pretty broken: Colors were lost at newlines (while they should keep going until + // the next color escape code). Anyways, there is a `'CI' in env` check below, + // and GitHub Actions does set `CI`, but it is _after_ the `isTTY` check, so it + // does not make any difference. At least for GitHub Actions. There might be _some_ + // CI system out there which _does_ use a (pseudo) TTY and also sets `CI`. + // All in all, the logic here is copied from the supports-color package since we + // depended on it for such a long time and didn’t want to break anything when vendoring + // and simplifying a bit. But if we ever rethink color support and CI systems in the future, + // at least we have some of the weirdness documented now! + if ('TF_BUILD' in env && 'AGENT_NAME' in env) { + return true; + } + + if (process.stdout.isTTY !== true) { + return false; + } + + if (process.platform === 'win32' || 'CI' in env) { + return true; + } + + if (env.TERM === 'dumb') { + return false; + } + + if ( + 'TERM' in env || + 'COLORTERM' in env || + 'TERM_PROGRAM' in env || + 'TEAMCITY_VERSION' in env + ) { + return true; + } + + return false; +} + +module.exports = (supportsColor() /*: boolean */); diff --git a/package-lock.json b/package-lock.json index 030d0165..0e595b61 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.19.1-revision17", "license": "BSD-3-Clause", "dependencies": { - "chalk": "^4.1.2", "chokidar": "^3.5.3", "commander": "^9.4.1", "cross-spawn": "^7.0.6", @@ -656,6 +655,7 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, "dependencies": { "color-convert": "^2.0.1" }, @@ -854,6 +854,7 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -966,6 +967,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, "dependencies": { "color-name": "~1.1.4" }, @@ -976,7 +978,8 @@ "node_modules/color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true }, "node_modules/commander": { "version": "9.4.1", @@ -1695,6 +1698,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, "engines": { "node": ">=8" } @@ -2669,6 +2673,7 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, "dependencies": { "has-flag": "^4.0.0" }, @@ -3391,6 +3396,7 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, "requires": { "color-convert": "^2.0.1" } @@ -3526,6 +3532,7 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, "requires": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -3602,6 +3609,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, "requires": { "color-name": "~1.1.4" } @@ -3609,7 +3617,8 @@ "color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true }, "commander": { "version": "9.4.1", @@ -4113,7 +4122,8 @@ "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true }, "he": { "version": "1.2.0", @@ -4810,6 +4820,7 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, "requires": { "has-flag": "^4.0.0" } diff --git a/package.json b/package.json index 67eafc8a..0b99a2fc 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ }, "homepage": "https://github.com/rtfeldman/node-test-runner#readme", "dependencies": { - "chalk": "^4.1.2", "chokidar": "^3.5.3", "commander": "^9.4.1", "cross-spawn": "^7.0.6",