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

Commit

Permalink
feat: run & restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Oct 16, 2016
1 parent 9e97d73 commit 9ee9f90
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/three-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
let x = 1;
x = x + 1;
module.exports = x;
7 changes: 7 additions & 0 deletions lib/internal/inspect-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ class Client extends EventEmitter {

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

this._socket = socket;
socket.on('data', this.handleChunk);
socket.on('close', () => {
this.emit('close');
});

Promise.all([
this.callMethod('Runtime.enable'),
Expand Down
37 changes: 34 additions & 3 deletions lib/internal/inspect-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,26 @@ function createRepl(inspector) {
let repl; // eslint-disable-line prefer-const
let lastCommand;

// Things we want to keep around
const history = { control: [], debug: [] };
const knownScripts = {};
const watchedExpressions = [];
const knownBreakpoints = [];

// Things we need to reset when the app restarts
let knownScripts;
let currentBacktrace;
let selectedFrame;
let exitDebugRepl;

function resetOnStart() {
knownScripts = {};
currentBacktrace = null;
selectedFrame = null;

if (exitDebugRepl) exitDebugRepl();
exitDebugRepl = null;
}
resetOnStart();

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

Expand Down Expand Up @@ -594,6 +607,14 @@ function createRepl(inspector) {

function initializeContext(context) {
copyOwnProperties(context, {
get run() {
return inspector.run();
},

get restart() {
return inspector.run();
},

get cont() {
handleResumed();
return Debugger.resume();
Expand Down Expand Up @@ -654,7 +675,7 @@ function createRepl(inspector) {

const oldContext = repl.context;

function exitDebugRepl() {
exitDebugRepl = () => {
// Restore all listeners
process.nextTick(() => {
listeners.forEach((listener) => {
Expand All @@ -675,7 +696,9 @@ function createRepl(inspector) {

repl.rli.removeListener('SIGINT', exitDebugRepl);
repl.removeListener('exit', exitDebugRepl);
}

exitDebugRepl = null;
};

// Exit debug repl on SIGINT
repl.rli.on('SIGINT', exitDebugRepl);
Expand Down Expand Up @@ -733,6 +756,14 @@ function createRepl(inspector) {
repl.rli.emit('SIGINT');
});

inspector.client.on('close', () => {
resetOnStart();
});

inspector.client.on('ready', () => {
// TODO: restore breakpoints
});

return repl;
};
}
Expand Down
3 changes: 3 additions & 0 deletions lib/node-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class NodeInspector {
if (!this.paused) {
this.repl.displayPrompt(true);
}
if (/Waiting for the debugger to disconnect\.\.\.\n$/.test(text)) {
this.killChild();
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions test/cli/launch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,49 @@ test('examples/empty.js', (t) => {
t.equal(code, 0, 'exits with success');
});
});

test('run after quit / restart', (t) => {
const cli = startCLI(['examples/three-lines.js']);

function onFatal(error) {
cli.quit();
throw error;
}

return cli.waitFor(/break/)
.then(() => cli.waitForPrompt())
.then(() => cli.stepCommand('n'))
.then(() => {
t.match(cli.output, 'break in examples/three-lines.js:2',
'steps to the 2nd line');
})
.then(() => cli.command('cont'))
.then(() => cli.waitFor(/disconnect/))
.then(() => {
t.match(cli.output, 'Waiting for the debugger to disconnect',
'the child was done');
})
.then(() => cli.command('cont'))
.then(() => cli.waitFor(/start the app/))
.then(() => {
t.match(cli.output, 'Use `run` to start the app again');
})
.then(() => cli.stepCommand('run'))
.then(() => cli.waitForPrompt())
.then(() => {
t.match(cli.output, 'break in examples/three-lines.js:1',
'is back at the beginning');
})
.then(() => cli.stepCommand('n'))
.then(() => {
t.match(cli.output, 'break in examples/three-lines.js:2',
'steps to the 2nd line');
})
.then(() => cli.stepCommand('restart'))
.then(() => {
t.match(cli.output, 'break in examples/three-lines.js:1',
'is back at the beginning');
})
.then(() => cli.quit())
.then(null, onFatal);
});

0 comments on commit 9ee9f90

Please sign in to comment.