repl: add welcome message

PR-URL: https://github.com/nodejs/node/pull/25947
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
gengjiawen 2018-12-19 22:51:30 +08:00 committed by Ruben Bridgewater
parent f3b5cc0807
commit fe963149f6
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
9 changed files with 36 additions and 24 deletions

View File

@ -11,17 +11,21 @@ const {
evalScript evalScript
} = require('internal/process/execution'); } = require('internal/process/execution');
const console = require('internal/console/global');
prepareMainThreadExecution(); prepareMainThreadExecution();
// --entry-type flag not supported in REPL // --entry-type flag not supported in REPL
if (require('internal/options').getOptionValue('--entry-type')) { if (require('internal/options').getOptionValue('--entry-type')) {
// If we can't write to stderr, we'd like to make this a noop, // If we can't write to stderr, we'd like to make this a noop,
// so use console.error. // so use console.error.
const { error } = require('internal/console/global'); console.error('Cannot specify --entry-type for REPL');
error('Cannot specify --entry-type for REPL');
process.exit(1); process.exit(1);
} }
console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');
const cliRepl = require('internal/repl'); const cliRepl = require('internal/repl');
cliRepl.createInternalRepl(process.env, (err, repl) => { cliRepl.createInternalRepl(process.env, (err, repl) => {
if (err) { if (err) {

View File

@ -12,7 +12,7 @@ cp.stdout.setEncoding('utf8');
let output = ''; let output = '';
cp.stdout.on('data', function(b) { cp.stdout.on('data', function(b) {
output += b; output += b;
if (output === '> 42\n') { if (output.endsWith('> 42\n')) {
gotToEnd = true; gotToEnd = true;
cp.kill(); cp.kill();
} }

View File

@ -4,12 +4,19 @@ const assert = require('assert');
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
// Spawn a node child process in interactive mode (enabling the REPL) and // Spawn a node child process in interactive mode (enabling the REPL) and
// confirm the '> ' prompt is included in the output. // confirm the '> ' prompt and welcome message is included in the output.
const cp = spawn(process.execPath, ['-i']); const cp = spawn(process.execPath, ['-i']);
cp.stdout.setEncoding('utf8'); cp.stdout.setEncoding('utf8');
cp.stdout.once('data', common.mustCall(function(b) { let out = '';
assert.strictEqual(b, '> '); cp.stdout.on('data', (d) => {
cp.kill(); out += d;
});
cp.stdout.on('end', common.mustCall(() => {
assert.strictEqual(out, `Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.\n> ');
})); }));
cp.stdin.end('');

View File

@ -86,7 +86,7 @@ const replProc = childProcess.spawn(
); );
replProc.stdin.end('.exit\n'); replProc.stdin.end('.exit\n');
let replStdout = ''; let replStdout = '';
replProc.stdout.on('data', function(d) { replProc.stdout.on('data', (d) => {
replStdout += d; replStdout += d;
}); });
replProc.on('close', function(code) { replProc.on('close', function(code) {
@ -94,8 +94,9 @@ replProc.on('close', function(code) {
const output = [ const output = [
'A', 'A',
'> ' '> '
].join('\n'); ];
assert.strictEqual(replStdout, output); assert.ok(replStdout.startsWith(output[0]));
assert.ok(replStdout.endsWith(output[1]));
}); });
// Test that preload placement at other points in the cmdline // Test that preload placement at other points in the cmdline
@ -114,7 +115,7 @@ const interactive = childProcess.exec(
`"${nodeBinary}" ${preloadOption([fixtureD])}-i`, `"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
common.mustCall(function(err, stdout, stderr) { common.mustCall(function(err, stdout, stderr) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(stdout, "> 'test'\n> "); assert.ok(stdout.endsWith("> 'test'\n> "));
}) })
); );

View File

@ -30,19 +30,19 @@ const child = spawn(process.execPath, args);
const input = '(function(){"use strict"; const y=1;y=2})()\n'; const input = '(function(){"use strict"; const y=1;y=2})()\n';
// This message will vary based on JavaScript engine, so don't check the message // This message will vary based on JavaScript engine, so don't check the message
// contents beyond confirming that the `Error` is a `TypeError`. // contents beyond confirming that the `Error` is a `TypeError`.
const expectOut = /^> Thrown:\nTypeError: /; const expectOut = /> Thrown:\nTypeError: /;
child.stderr.setEncoding('utf8'); child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) { child.stderr.on('data', (d) => {
throw new Error('child.stderr be silent'); throw new Error('child.stderr be silent');
}); });
child.stdout.setEncoding('utf8'); child.stdout.setEncoding('utf8');
let out = ''; let out = '';
child.stdout.on('data', function(c) { child.stdout.on('data', (d) => {
out += c; out += d;
}); });
child.stdout.on('end', function() { child.stdout.on('end', () => {
assert(expectOut.test(out)); assert(expectOut.test(out));
console.log('ok'); console.log('ok');
}); });

View File

@ -11,7 +11,7 @@ child.stdout.on('data', (data) => {
}); });
child.on('exit', common.mustCall(() => { child.on('exit', common.mustCall(() => {
const results = output.replace(/^> /mg, '').split('\n'); const results = output.replace(/^> /mg, '').split('\n').slice(2);
assert.deepStrictEqual( assert.deepStrictEqual(
results, results,
[ [

View File

@ -25,7 +25,7 @@ child.stdout.on('data', (c) => {
out += c; out += c;
}); });
child.stdout.on('end', common.mustCall(() => { child.stdout.on('end', common.mustCall(() => {
assert.strictEqual(out, '> 1\n> '); assert.ok(out.endsWith('> 1\n> '));
})); }));
child.stdin.end(input); child.stdin.end(input);

View File

@ -13,7 +13,7 @@ child.stdout.on('data', (data) => {
}); });
child.on('exit', common.mustCall(() => { child.on('exit', common.mustCall(() => {
const results = output.replace(/^> /mg, '').split('\n'); const results = output.replace(/^> /mg, '').split('\n').slice(2);
assert.deepStrictEqual(results, ['undefined', 'true', 'true', '']); assert.deepStrictEqual(results, ['undefined', 'true', 'true', '']);
})); }));

View File

@ -12,20 +12,20 @@ const child = spawn(process.execPath, args);
const input = 'var foo = "bar\\\nbaz"'; const input = 'var foo = "bar\\\nbaz"';
// Match '...' as well since it marks a multi-line statement // Match '...' as well since it marks a multi-line statement
const expectOut = /^> \.\.\. undefined\n/; const expectOut = /> \.\.\. undefined\n/;
child.stderr.setEncoding('utf8'); child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) { child.stderr.on('data', (d) => {
throw new Error('child.stderr be silent'); throw new Error('child.stderr be silent');
}); });
child.stdout.setEncoding('utf8'); child.stdout.setEncoding('utf8');
let out = ''; let out = '';
child.stdout.on('data', function(c) { child.stdout.on('data', (d) => {
out += c; out += d;
}); });
child.stdout.on('end', function() { child.stdout.on('end', () => {
assert(expectOut.test(out)); assert(expectOut.test(out));
console.log('ok'); console.log('ok');
}); });