Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit c64155f

Browse files
author
Jan Krems
committed
feat: repl command
1 parent 2f5dac6 commit c64155f

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

lib/node-inspect.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ function createCommandContext(inspector) {
476476

477477
const scripts = {};
478478
const watchedExpressions = [];
479-
480479
const knownBreakpoints = [];
480+
const history = { debug: [], control: [] };
481481

482482
const writer = createRemoteAwareWriter(true);
483483
const formatValue = value => writer(value);
@@ -530,6 +530,21 @@ function createCommandContext(inspector) {
530530
}
531531
}
532532

533+
// Exit debug repl
534+
function exitRepl() {
535+
// Restore eval
536+
repl.eval = (code, ctx, file, cb) =>
537+
inspector.controlEval(code, ctx, file, cb);
538+
539+
// Swap history
540+
history.debug = repl.rli.history;
541+
repl.rli.history = history.control;
542+
543+
repl.context = inspector.context;
544+
repl.rli.setPrompt('debug> ');
545+
repl.displayPrompt();
546+
}
547+
533548
const ctx = {
534549
get help() {
535550
const commands = [
@@ -582,6 +597,49 @@ function createCommandContext(inspector) {
582597
.then(convertResultToRemoteObject);
583598
},
584599

600+
get repl() {
601+
// if (!this.requireConnection()) return;
602+
print('Press Ctrl + C to leave debug repl');
603+
604+
// Don't display any default messages
605+
const listeners = repl.rli.listeners('SIGINT').slice(0);
606+
repl.rli.removeAllListeners('SIGINT');
607+
608+
function exitDebugRepl() {
609+
// Restore all listeners
610+
process.nextTick(() => {
611+
listeners.forEach((listener) => {
612+
repl.rli.on('SIGINT', listener);
613+
});
614+
});
615+
616+
// Exit debug repl
617+
exitRepl();
618+
619+
repl.rli.removeListener('SIGINT', exitDebugRepl);
620+
repl.removeListener('exit', exitDebugRepl);
621+
}
622+
623+
// Exit debug repl on SIGINT
624+
repl.rli.on('SIGINT', exitDebugRepl);
625+
626+
// Exit debug repl on repl exit
627+
repl.on('exit', exitDebugRepl);
628+
629+
// Set new
630+
repl.eval = (code, evalCtx, file, cb) => {
631+
toCallback(ctx.debugEval(code), cb);
632+
};
633+
repl.context = {};
634+
635+
// Swap history
636+
history.control = repl.rli.history;
637+
repl.rli.history = history.debug;
638+
639+
repl.rli.setPrompt('> ');
640+
repl.displayPrompt();
641+
},
642+
585643
exec(code) {
586644
return ctx.debugEval(code);
587645
},
@@ -926,7 +984,6 @@ class Inspector {
926984
this.waiting = null;
927985
this.paused = 0;
928986
this.context = this.repl.context;
929-
this.history = { debug: [], control: [] };
930987
this.breakpoints = [];
931988
this._watchers = [];
932989

0 commit comments

Comments
 (0)