Repository: chalk/chalk (js)

Issue:
Auto-detect support for `stderr`
<!-- Issuehunt Badges -->
[<img alt="Issuehunt badges" src="https://img.shields.io/badge/IssueHunt-%2480%20Funded-%2300A156.svg" />](https://issuehunt.io/r/chalk/chalk/issues/301)
<!-- /Issuehunt Badges -->


I want use colors only on `console.error` calls which is typically terminal.
I suggest some API extension, very easy idea is chaining like this:
```js
const coloredOutput = chalk.stderr.red('RED ALERT');
```
This can be used more easily like this:
```js
const chalk = require('chalk').stderr;
const coloredOutput = chalk.red('RED ALERT');
```


<!-- Issuehunt content -->

---

<details>
<summary>
<b>IssueHunt Summary</b>
</summary>


### Backers (Total: $80.00)

- [<img src='https://avatars3.githubusercontent.com/u/44827199?v=4' alt='issuehunt' width=24 height=24> issuehunt](https://issuehunt.io/u/issuehunt) ($80.00)
### Submitted pull Requests
- [#359 Add chalk.stderr and chalk.stderr.supportsColor](https://issuehunt.io/r/chalk/chalk/pull/359)
---

#### [Become a backer now!](https://issuehunt.io/r/chalk/chalk/issues/301)
#### [Or submit a pull request to get the deposits!](https://issuehunt.io/r/chalk/chalk/issues/301)
### Tips

- Checkout the [Issuehunt explorer](https://issuehunt.io/r/chalk/chalk/) to discover more funded issues.
- Need some help from other developers? [Add your repositories](https://issuehunt.io/r/new) on IssueHunt to raise funds.
---
IssueHunt has been backed by the following sponsors. [Become a sponsor](https://issuehunt.io/membership/members)
</details>
<!-- /Issuehunt content-->

Pull request description:
Add `chalk.stderr`
Subj.

Fixes #301

Also, fixes incorrect test setup in `test/no-color-support.js`.

<!-- Issuehunt content -->

---

<details>
<summary>
<b>IssueHunt Summary</b>
</summary>

### Referenced issues

This pull request has been submitted to:
- [#301: Auto-detect support for `stderr`](https://issuehunt.io/repos/11855195/issues/301)
---

IssueHunt has been backed by the following sponsors. [Become a sponsor](https://issuehunt.io/membership/members)
</details>
<!-- /Issuehunt content-->

Patch:
diff --git a/index.d.ts b/index.d.ts
index c650eb9..52dd5ae 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -378,6 +378,7 @@ declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
 	ForegroundColor: ForegroundColor;
 	BackgroundColor: BackgroundColor;
 	Modifiers: Modifiers;
+	stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false};
 };
 
 export = chalk;
diff --git a/readme.md b/readme.md
index 0de3051..aaa8b47 100644
--- a/readme.md
+++ b/readme.md
@@ -147,6 +147,10 @@ Can be overridden by the user with the flags `--color` and `--no-color`. For sit
 
 Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
 
+### chalk.stderr and chalk.stderr.supportsColor
+
+`chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience.
+
 
 ## Styles
 
diff --git a/source/index.js b/source/index.js
index 9b4b46b..686758b 100644
--- a/source/index.js
+++ b/source/index.js
@@ -1,6 +1,6 @@
 'use strict';
 const ansiStyles = require('ansi-styles');
-const {stdout: stdoutColor} = require('supports-color');
+const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
 const template = require('./templates');
 const {
 	stringReplaceAll,
@@ -218,5 +218,9 @@ const chalkTag = (chalk, ...strings) => {
 
 Object.defineProperties(Chalk.prototype, styles);
 
-module.exports = Chalk(); // eslint-disable-line new-cap
-module.exports.supportsColor = stdoutColor;
+const chalk = Chalk(); // eslint-disable-line new-cap
+chalk.supportsColor = stdoutColor;
+chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
+chalk.stderr.supportsColor = stderrColor;
+
+module.exports = chalk;


Test patch:
diff --git a/index.test-d.ts b/index.test-d.ts
index 1d2df98..bc3d17a 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -16,6 +16,16 @@ expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).hasBasic);
 expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has256);
 expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has16m);
 
+// - stderr -
+expectType<chalk.Chalk>(chalk.stderr);
+expectType<chalk.ColorSupport | false>(chalk.stderr.supportsColor);
+expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic);
+expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has256);
+expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has16m);
+
+// -- `stderr` is not a member of the Chalk interface --
+expectError(chalk.reset.stderr);
+
 // -- `supportsColor` is not a member of the Chalk interface --
 expectError(chalk.reset.supportsColor);
 
diff --git a/test/_fixture.js b/test/_fixture.js
index 214a86c..29f34b0 100644
--- a/test/_fixture.js
+++ b/test/_fixture.js
@@ -1,4 +1,4 @@
 'use strict';
 const chalk = require('../source');
 
-console.log(chalk.hex('#ff6159')('test'));
+console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`);
diff --git a/test/_supports-color.js b/test/_supports-color.js
index 490c323..b5c0f78 100644
--- a/test/_supports-color.js
+++ b/test/_supports-color.js
@@ -7,6 +7,12 @@ const DEFAULT = {
 		hasBasic: true,
 		has256: true,
 		has16m: true
+	},
+	stderr: {
+		level: 3,
+		hasBasic: true,
+		has256: true,
+		has16m: true
 	}
 };
 
diff --git a/test/chalk.js b/test/chalk.js
index 2992a09..5e33f5e 100644
--- a/test/chalk.js
+++ b/test/chalk.js
@@ -105,3 +105,8 @@ test('don\'t emit RGB codes if level is 0', t => {
 test('supports blackBright color', t => {
 	t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m');
 });
+
+test('sets correct level for chalk.stderr and respects it', t => {
+	t.is(chalk.stderr.level, 3);
+	t.is(chalk.stderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m');
+});
diff --git a/test/level.js b/test/level.js
index 828de24..65d4720 100644
--- a/test/level.js
+++ b/test/level.js
@@ -41,5 +41,5 @@ test('propagate enable/disable changes from child colors', t => {
 
 test('disable colors if they are not supported', async t => {
 	const {stdout} = await execa.node(path.join(__dirname, '_fixture'));
-	t.is(stdout, 'test');
+	t.is(stdout, 'testout testerr');
 });
diff --git a/test/no-color-support.js b/test/no-color-support.js
index 7318452..ae88ac9 100644
--- a/test/no-color-support.js
+++ b/test/no-color-support.js
@@ -2,10 +2,18 @@ import test from 'ava';
 
 // Spoof supports-color
 require('./_supports-color')(__dirname, {
-	level: 0,
-	hasBasic: false,
-	has256: false,
-	has16m: false
+	stdout: {
+		level: 0,
+		hasBasic: false,
+		has256: false,
+		has16m: false
+	},
+	stderr: {
+		level: 0,
+		hasBasic: false,
+		has256: false,
+		has16m: false
+	}
 });
 
 const chalk = require('../source');