Skip to content

Commit 4ecd996

Browse files
Trottevanlucas
authored andcommitted
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 8c4c84f commit 4ecd996

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
@@ -88,8 +88,9 @@
8888
delete process.env.NODE_UNIQUE_ID;
8989
}
9090

91-
if (process._eval != null) {
92-
// User passed '-e' or '--eval' arguments to Node.
91+
if (process._eval != null && !process._forceRepl) {
92+
// User passed '-e' or '--eval' arguments to Node without '-i' or
93+
// '--interactive'
9394
startup.preloadModules();
9495
evalScript('[eval]');
9596
} else if (process.argv[1]) {
@@ -161,6 +162,11 @@
161162
process.exit();
162163
});
163164
});
165+
166+
if (process._eval != null) {
167+
// User passed '-e' or '--eval'
168+
evalScript('[eval]');
169+
}
164170
} else {
165171
// Read all of stdin - execute it.
166172
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)