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

Commit 9ee9f90

Browse files
author
Jan Krems
committed
feat: run & restart
1 parent 9e97d73 commit 9ee9f90

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

examples/three-lines.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
let x = 1;
3+
x = x + 1;
4+
module.exports = x;

lib/internal/inspect-protocol.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ class Client extends EventEmitter {
213213

214214
callMethod(method, params) {
215215
return new Promise((resolve, reject) => {
216+
if (!this._socket) {
217+
reject(new Error('Use `run` to start the app again.'));
218+
return;
219+
}
216220
const data = { id: ++this._lastId, method, params };
217221
this._pending[data.id] = (error, result) => {
218222
if (error) reject(unpackError(error));
@@ -257,6 +261,9 @@ class Client extends EventEmitter {
257261

258262
this._socket = socket;
259263
socket.on('data', this.handleChunk);
264+
socket.on('close', () => {
265+
this.emit('close');
266+
});
260267

261268
Promise.all([
262269
this.callMethod('Runtime.enable'),

lib/internal/inspect-repl.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,26 @@ function createRepl(inspector) {
199199
let repl; // eslint-disable-line prefer-const
200200
let lastCommand;
201201

202+
// Things we want to keep around
202203
const history = { control: [], debug: [] };
203-
const knownScripts = {};
204204
const watchedExpressions = [];
205205
const knownBreakpoints = [];
206206

207+
// Things we need to reset when the app restarts
208+
let knownScripts;
207209
let currentBacktrace;
208210
let selectedFrame;
211+
let exitDebugRepl;
212+
213+
function resetOnStart() {
214+
knownScripts = {};
215+
currentBacktrace = null;
216+
selectedFrame = null;
217+
218+
if (exitDebugRepl) exitDebugRepl();
219+
exitDebugRepl = null;
220+
}
221+
resetOnStart();
209222

210223
const print = inspector.print.bind(inspector);
211224

@@ -594,6 +607,14 @@ function createRepl(inspector) {
594607

595608
function initializeContext(context) {
596609
copyOwnProperties(context, {
610+
get run() {
611+
return inspector.run();
612+
},
613+
614+
get restart() {
615+
return inspector.run();
616+
},
617+
597618
get cont() {
598619
handleResumed();
599620
return Debugger.resume();
@@ -654,7 +675,7 @@ function createRepl(inspector) {
654675

655676
const oldContext = repl.context;
656677

657-
function exitDebugRepl() {
678+
exitDebugRepl = () => {
658679
// Restore all listeners
659680
process.nextTick(() => {
660681
listeners.forEach((listener) => {
@@ -675,7 +696,9 @@ function createRepl(inspector) {
675696

676697
repl.rli.removeListener('SIGINT', exitDebugRepl);
677698
repl.removeListener('exit', exitDebugRepl);
678-
}
699+
700+
exitDebugRepl = null;
701+
};
679702

680703
// Exit debug repl on SIGINT
681704
repl.rli.on('SIGINT', exitDebugRepl);
@@ -733,6 +756,14 @@ function createRepl(inspector) {
733756
repl.rli.emit('SIGINT');
734757
});
735758

759+
inspector.client.on('close', () => {
760+
resetOnStart();
761+
});
762+
763+
inspector.client.on('ready', () => {
764+
// TODO: restore breakpoints
765+
});
766+
736767
return repl;
737768
};
738769
}

lib/node-inspect.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class NodeInspector {
200200
if (!this.paused) {
201201
this.repl.displayPrompt(true);
202202
}
203+
if (/Waiting for the debugger to disconnect\.\.\.\n$/.test(text)) {
204+
this.killChild();
205+
}
203206
}
204207
}
205208

test/cli/launch.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,49 @@ test('examples/empty.js', (t) => {
2727
t.equal(code, 0, 'exits with success');
2828
});
2929
});
30+
31+
test('run after quit / restart', (t) => {
32+
const cli = startCLI(['examples/three-lines.js']);
33+
34+
function onFatal(error) {
35+
cli.quit();
36+
throw error;
37+
}
38+
39+
return cli.waitFor(/break/)
40+
.then(() => cli.waitForPrompt())
41+
.then(() => cli.stepCommand('n'))
42+
.then(() => {
43+
t.match(cli.output, 'break in examples/three-lines.js:2',
44+
'steps to the 2nd line');
45+
})
46+
.then(() => cli.command('cont'))
47+
.then(() => cli.waitFor(/disconnect/))
48+
.then(() => {
49+
t.match(cli.output, 'Waiting for the debugger to disconnect',
50+
'the child was done');
51+
})
52+
.then(() => cli.command('cont'))
53+
.then(() => cli.waitFor(/start the app/))
54+
.then(() => {
55+
t.match(cli.output, 'Use `run` to start the app again');
56+
})
57+
.then(() => cli.stepCommand('run'))
58+
.then(() => cli.waitForPrompt())
59+
.then(() => {
60+
t.match(cli.output, 'break in examples/three-lines.js:1',
61+
'is back at the beginning');
62+
})
63+
.then(() => cli.stepCommand('n'))
64+
.then(() => {
65+
t.match(cli.output, 'break in examples/three-lines.js:2',
66+
'steps to the 2nd line');
67+
})
68+
.then(() => cli.stepCommand('restart'))
69+
.then(() => {
70+
t.match(cli.output, 'break in examples/three-lines.js:1',
71+
'is back at the beginning');
72+
})
73+
.then(() => cli.quit())
74+
.then(null, onFatal);
75+
});

0 commit comments

Comments
 (0)