From ba16a120518e972bb3ac0190196fdb4caa32f4a6 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 10 Mar 2016 21:44:54 -0800 Subject: [PATCH] src: allow combination of -i and -e cli flags If both -i and -e flags are specified, do not ignore the -i. Instead, launch the interactive REPL and start by evaluating the passed string. Fixes: https://github.com/nodejs/node/issues/1197 PR-URL: https://github.com/nodejs/node/pull/5655 Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- src/node.js | 10 ++++++-- test/sequential/test-force-repl-with-eval.js | 27 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/sequential/test-force-repl-with-eval.js diff --git a/src/node.js b/src/node.js index a0f5aec86db245..a24338b8279ff6 100644 --- a/src/node.js +++ b/src/node.js @@ -98,8 +98,9 @@ delete process.env.NODE_UNIQUE_ID; } - if (process._eval != null) { - // User passed '-e' or '--eval' arguments to Node. + if (process._eval != null && !process._forceRepl) { + // User passed '-e' or '--eval' arguments to Node without '-i' or + // '--interactive' startup.preloadModules(); evalScript('[eval]'); } else if (process.argv[1]) { @@ -171,6 +172,11 @@ process.exit(); }); }); + + if (process._eval != null) { + // User passed '-e' or '--eval' + evalScript('[eval]'); + } } else { // Read all of stdin - execute it. process.stdin.setEncoding('utf8'); diff --git a/test/sequential/test-force-repl-with-eval.js b/test/sequential/test-force-repl-with-eval.js new file mode 100644 index 00000000000000..a285db8e57d930 --- /dev/null +++ b/test/sequential/test-force-repl-with-eval.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const spawn = require('child_process').spawn; + +// spawn a node child process in "interactive" mode (force the repl) and eval +const cp = spawn(process.execPath, ['-i', '-e', 'console.log("42")']); +var gotToEnd = false; +const timeoutId = setTimeout(function() { + throw new Error('timeout!'); +}, common.platformTimeout(1000)); // give node + the repl 1 second to boot up + +cp.stdout.setEncoding('utf8'); + +var output = ''; +cp.stdout.on('data', function(b) { + output += b; + if (output === '> 42\n') { + clearTimeout(timeoutId); + gotToEnd = true; + cp.kill(); + } +}); + +process.on('exit', function() { + assert(gotToEnd); +});