diff options
author | monkingxue <yingchenxue@qq.com> | 2018-05-09 10:34:57 +0800 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-18 15:47:17 +0200 |
commit | 9aa4ec43fce7fd9166459c98f347760cf450a350 (patch) | |
tree | 87550905478fcf01db756bd04ede6c8b8ab2baab /lib/repl.js | |
parent | 9deca876bb94626564184ad263ff80dc8042808b (diff) | |
download | android-node-v8-9aa4ec43fce7fd9166459c98f347760cf450a350.tar.gz android-node-v8-9aa4ec43fce7fd9166459c98f347760cf450a350.tar.bz2 android-node-v8-9aa4ec43fce7fd9166459c98f347760cf450a350.zip |
repl: add friendly tips about how to exit repl
Imitate python repl, when the user enters 'exit' or 'quit',
no longer prompt 'Reference Error', but
prompts 'To exit, press ^D or type .exit'.
If the user defines variables named 'exit' or 'quit' ,
only the value of the variables are output
PR-URL: https://github.com/nodejs/node/pull/20617
Fixes: https://github.com/nodejs/node/issues/19021
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib/repl.js')
-rw-r--r-- | lib/repl.js | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/repl.js b/lib/repl.js index 600816a605..928e7c6d69 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -215,9 +215,15 @@ function REPLServer(prompt, function defaultEval(code, context, file, cb) { var err, result, script, wrappedErr; + var isExitCommand = false; var wrappedCmd = false; var awaitPromise = false; var input = code; + var trimmedCommand = code.trim(); + + if (trimmedCommand === 'exit' || trimmedCommand === 'quit') { + isExitCommand = true; + } if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) { // It's confusing for `{ a : 1 }` to be interpreted as a block @@ -313,10 +319,16 @@ function REPLServer(prompt, breakOnSigint: self.breakEvalOnSigint }; + const localContext = self.useGlobal ? global : self.context; + if (isExitCommand && !localContext.hasOwnProperty(trimmedCommand)) { + self.outputStream.write('(To exit, press ^D or type .exit)\n'); + return self.displayPrompt(); + } + if (self.useGlobal) { result = script.runInThisContext(scriptOptions); } else { - result = script.runInContext(context, scriptOptions); + result = script.runInContext(localContext, scriptOptions); } } finally { if (self.breakEvalOnSigint) { @@ -332,12 +344,10 @@ function REPLServer(prompt, } } catch (e) { err = e; - if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') { - // The stack trace for this case is not very useful anyway. + // The stack trace for this case is not very useful anyway. Object.defineProperty(err, 'stack', { value: '' }); } - if (process.domain) { debug('not recoverable, send to domain'); process.domain.emit('error', err); |