repl: add replDefaults to customize the writer · nodejs/node@a0b1191 · GitHub
Skip to content

Commit a0b1191

Browse files
committed
repl: add replDefaults to customize the writer
So far it was not possible to modify the inspection defaults used by the REPL from the running instance itself. This introduces a new property on `util.inspect` which is only used inside the REPL and which allows to modify the used inspection defaults at any point of time. PR-URL: #26375 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 203fa63 commit a0b1191

6 files changed

Lines changed: 82 additions & 13 deletions

File tree

doc/api/repl.md

Lines changed: 25 additions & 6 deletions

lib/internal/repl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const REPL = require('repl');
4+
const { kStandaloneREPL } = require('internal/repl/utils');
45

56
module.exports = Object.create(REPL);
67
module.exports.createInternalRepl = createRepl;
@@ -11,6 +12,7 @@ function createRepl(env, opts, cb) {
1112
opts = null;
1213
}
1314
opts = {
15+
[kStandaloneREPL]: true,
1416
ignoreUndefined: false,
1517
terminal: process.stdout.isTTY,
1618
useGlobal: true,
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ function isRecoverableError(e, code) {
7070
}
7171

7272
module.exports = {
73-
isRecoverableError
73+
isRecoverableError,
74+
kStandaloneREPL: Symbol('kStandaloneREPL')
7475
};

lib/repl.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ const { sendInspectorCommand } = require('internal/util/inspector');
7373
const experimentalREPLAwait = require('internal/options').getOptionValue(
7474
'--experimental-repl-await'
7575
);
76-
const { isRecoverableError } = require('internal/repl/recoverable');
76+
const {
77+
isRecoverableError,
78+
kStandaloneREPL
79+
} = require('internal/repl/utils');
7780
const {
7881
getOwnNonIndexProperties,
7982
propertyFilter: {
@@ -499,10 +502,25 @@ function REPLServer(prompt,
499502
}
500503
self.useColors = !!options.useColors;
501504

502-
if (self.useColors && self.writer === writer) {
503-
// Turn on ANSI coloring.
504-
self.writer = (obj) => util.inspect(obj, self.writer.options);
505-
self.writer.options = { ...writer.options, colors: true };
505+
if (self.writer === writer) {
506+
// Conditionally turn on ANSI coloring.
507+
writer.options.colors = self.useColors;
508+
509+
if (options[kStandaloneREPL]) {
510+
Object.defineProperty(util.inspect, 'replDefaults', {
511+
get() {
512+
return writer.options;
513+
},
514+
set(options) {
515+
if (options === null || typeof options !== 'object') {
516+
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
517+
}
518+
return Object.assign(writer.options, options);
519+
},
520+
enumerable: true,
521+
configurable: true
522+
});
523+
}
506524
}
507525

508526
function filterInternalStackFrames(structuredStack) {

node.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
'lib/internal/repl.js',
175175
'lib/internal/repl/await.js',
176176
'lib/internal/repl/history.js',
177-
'lib/internal/repl/recoverable.js',
177+
'lib/internal/repl/utils.js',
178178
'lib/internal/socket_list.js',
179179
'lib/internal/test/binding.js',
180180
'lib/internal/test/heap.js',
Lines changed: 29 additions & 0 deletions

0 commit comments

Comments
 (0)