node REPL "on exit" event

Problem

Is there any way to trigger a function that's called when the user types .exit or [Ctrl][D]s the script?

Something like:

repl.start().on('exit', function() {
    console.log("Thanks for using customized Node REPL!");
});

(Except, of course, I'd actually put useful code instead of the console.log.)

Problem courtesy of: Aaron Yodaiken

Solution

repl.start().rli.on('close', function() {
    console.log("Thanks for using customized Node REPL!");
}); 

REPL uses the Readline module, which emits a Close event. From the docs:

Event: 'close'

function () {}

Emitted whenever the in stream receives a ^C or ^D, respectively known as SIGINT and EOT. This is a good way to know the user is finished using your program.

Solution courtesy of: Pero P.

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.