| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a899991 commit cc0d64f
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -102,6 +102,9 @@ const { Console } = console; | |||
| 102 | 102 | ||
| 103 | 103 | <!-- YAML | |
| 104 | 104 | changes: | |
| 105 | + - version: REPLACEME | ||
| 106 | + pr-url: https://github.com/nodejs/node/pull/60082 | ||
| 107 | + description: The `inspectOptions` option can be a `Map` from stream to options. | ||
| 105 | 108 | - version: | |
| 106 | 109 | - v14.2.0 | |
| 107 | 110 | - v12.17.0 | |
@@ -131,8 +134,9 @@ changes: | |||
| 131 | 134 | and the value returned by `getColorDepth()` on the respective stream. This | |
| 132 | 135 | option can not be used, if `inspectOptions.colors` is set as well. | |
| 133 | 136 | **Default:** `'auto'`. | |
| 134 | - * `inspectOptions` {Object} Specifies options that are passed along to | ||
| 135 | - [`util.inspect()`][]. | ||
| 137 | + * `inspectOptions` {Object|Map} Specifies options that are passed along to | ||
| 138 | + [`util.inspect()`][]. Can be an options object or, if different options | ||
| 139 | + for stdout and stderr are desired, a `Map` from stream objects to options. | ||
| 136 | 140 | * `groupIndentation` {number} Set group indentation. | |
| 137 | 141 | **Default:** `2`. | |
| 138 | 142 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,8 @@ const { | |||
| 12 | 12 | Boolean, | |
| 13 | 13 | ErrorCaptureStackTrace, | |
| 14 | 14 | FunctionPrototypeBind, | |
| 15 | + MapPrototypeGet, | ||
| 16 | + MapPrototypeValues, | ||
| 15 | 17 | ObjectDefineProperties, | |
| 16 | 18 | ObjectDefineProperty, | |
| 17 | 19 | ObjectKeys, | |
@@ -139,12 +141,20 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { | |||
| 139 | 141 | if (inspectOptions !== undefined) { | |
| 140 | 142 | validateObject(inspectOptions, 'options.inspectOptions'); | |
| 141 | 143 | ||
| 142 | - if (inspectOptions.colors !== undefined && | ||
| 143 | - options.colorMode !== undefined) { | ||
| 144 | - throw new ERR_INCOMPATIBLE_OPTION_PAIR( | ||
| 145 | - 'options.inspectOptions.color', 'colorMode'); | ||
| 144 | + const inspectOptionsMap = isMap(inspectOptions) ? | ||
| 145 | + inspectOptions : new SafeMap([ | ||
| 146 | + [stdout, inspectOptions], | ||
| 147 | + [stderr, inspectOptions], | ||
| 148 | + ]); | ||
| 149 | + | ||
| 150 | + for (const inspectOptions of MapPrototypeValues(inspectOptionsMap)) { | ||
| 151 | + if (inspectOptions.colors !== undefined && | ||
| 152 | + options.colorMode !== undefined) { | ||
| 153 | + throw new ERR_INCOMPATIBLE_OPTION_PAIR( | ||
| 154 | + 'options.inspectOptions.color', 'colorMode'); | ||
| 155 | + } | ||
| 146 | 156 | } | |
| 147 | - optionsMap.set(this, inspectOptions); | ||
| 157 | + optionsMap.set(this, inspectOptionsMap); | ||
| 148 | 158 | } | |
| 149 | 159 | ||
| 150 | 160 | // Bind the prototype functions to this Console instance | |
@@ -321,7 +331,8 @@ ObjectDefineProperties(Console.prototype, { | |||
| 321 | 331 | color = lazyUtilColors().shouldColorize(stream); | |
| 322 | 332 | } | |
| 323 | 333 | ||
| 324 | - const options = optionsMap.get(this); | ||
| 334 | + const inspectOptionsMap = optionsMap.get(this); | ||
| 335 | + const options = inspectOptionsMap ? MapPrototypeGet(inspectOptionsMap, stream) : undefined; | ||
| 325 | 336 | if (options) { | |
| 326 | 337 | if (options.colors === undefined) { | |
| 327 | 338 | options.colors = color; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const { Console } = require('console'); | ||
| 4 | + const { PassThrough } = require('stream'); | ||
| 5 | + const { strict: assert } = require('assert'); | ||
| 6 | + | ||
| 7 | + const stdout = new PassThrough().setEncoding('utf8'); | ||
| 8 | + const stderr = new PassThrough().setEncoding('utf8'); | ||
| 9 | + | ||
| 10 | + const console = new Console({ | ||
| 11 | + stdout, | ||
| 12 | + stderr, | ||
| 13 | + inspectOptions: new Map([ | ||
| 14 | + [stdout, { colors: true }], | ||
| 15 | + [stderr, { colors: false }], | ||
| 16 | + ]), | ||
| 17 | + }); | ||
| 18 | + | ||
| 19 | + console.log('Hello', 42); | ||
| 20 | + console.warn('Hello', 42); | ||
| 21 | + | ||
| 22 | + assert.strictEqual(stdout.read(), 'Hello \x1B[33m42\x1B[39m\n'); | ||
| 23 | + assert.strictEqual(stderr.read(), 'Hello 42\n'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments