Skip to content

Commit ba16a12

Browse files
committed
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: #1197 PR-URL: #5655 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 6ba5af2 commit ba16a12

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/node.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@
9898
delete process.env.NODE_UNIQUE_ID;
9999
}
100100

101-
if (process._eval != null) {
102-
// User passed '-e' or '--eval' arguments to Node.
101+
if (process._eval != null && !process._forceRepl) {
102+
// User passed '-e' or '--eval' arguments to Node without '-i' or
103+
// '--interactive'
103104
startup.preloadModules();
104105
evalScript('[eval]');
105106
} else if (process.argv[1]) {
@@ -171,6 +172,11 @@
171172
process.exit();
172173
});
173174
});
175+
176+
if (process._eval != null) {
177+
// User passed '-e' or '--eval'
178+
evalScript('[eval]');
179+
}
174180
} else {
175181
// Read all of stdin - execute it.
176182
process.stdin.setEncoding('utf8');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const spawn = require('child_process').spawn;
5+
6+
// spawn a node child process in "interactive" mode (force the repl) and eval
7+
const cp = spawn(process.execPath, ['-i', '-e', 'console.log("42")']);
8+
var gotToEnd = false;
9+
const timeoutId = setTimeout(function() {
10+
throw new Error('timeout!');
11+
}, common.platformTimeout(1000)); // give node + the repl 1 second to boot up
12+
13+
cp.stdout.setEncoding('utf8');
14+
15+
var output = '';
16+
cp.stdout.on('data', function(b) {
17+
output += b;
18+
if (output === '> 42\n') {
19+
clearTimeout(timeoutId);
20+
gotToEnd = true;
21+
cp.kill();
22+
}
23+
});
24+
25+
process.on('exit', function() {
26+
assert(gotToEnd);
27+
});

0 commit comments

Comments
 (0)