Skip to content

Commit fe96314

Browse files
gengjiawenBridgeAR
authored andcommitted
repl: add welcome message
PR-URL: #25947 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent f3b5cc0 commit fe96314

9 files changed

+36
-24
lines changed

lib/internal/main/repl.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ const {
1111
evalScript
1212
} = require('internal/process/execution');
1313

14+
const console = require('internal/console/global');
15+
1416
prepareMainThreadExecution();
1517

1618
// --entry-type flag not supported in REPL
1719
if (require('internal/options').getOptionValue('--entry-type')) {
1820
// If we can't write to stderr, we'd like to make this a noop,
1921
// so use console.error.
20-
const { error } = require('internal/console/global');
21-
error('Cannot specify --entry-type for REPL');
22+
console.error('Cannot specify --entry-type for REPL');
2223
process.exit(1);
2324
}
2425

26+
console.log(`Welcome to Node.js ${process.version}.\n` +
27+
'Type ".help" for more information.');
28+
2529
const cliRepl = require('internal/repl');
2630
cliRepl.createInternalRepl(process.env, (err, repl) => {
2731
if (err) {

test/parallel/test-force-repl-with-eval.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cp.stdout.setEncoding('utf8');
1212
let output = '';
1313
cp.stdout.on('data', function(b) {
1414
output += b;
15-
if (output === '> 42\n') {
15+
if (output.endsWith('> 42\n')) {
1616
gotToEnd = true;
1717
cp.kill();
1818
}

test/parallel/test-force-repl.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ const assert = require('assert');
44
const spawn = require('child_process').spawn;
55

66
// Spawn a node child process in interactive mode (enabling the REPL) and
7-
// confirm the '> ' prompt is included in the output.
7+
// confirm the '> ' prompt and welcome message is included in the output.
88
const cp = spawn(process.execPath, ['-i']);
99

1010
cp.stdout.setEncoding('utf8');
1111

12-
cp.stdout.once('data', common.mustCall(function(b) {
13-
assert.strictEqual(b, '> ');
14-
cp.kill();
12+
let out = '';
13+
cp.stdout.on('data', (d) => {
14+
out += d;
15+
});
16+
17+
cp.stdout.on('end', common.mustCall(() => {
18+
assert.strictEqual(out, `Welcome to Node.js ${process.version}.\n` +
19+
'Type ".help" for more information.\n> ');
1520
}));
21+
22+
cp.stdin.end('');

test/parallel/test-preload.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ const replProc = childProcess.spawn(
8686
);
8787
replProc.stdin.end('.exit\n');
8888
let replStdout = '';
89-
replProc.stdout.on('data', function(d) {
89+
replProc.stdout.on('data', (d) => {
9090
replStdout += d;
9191
});
9292
replProc.on('close', function(code) {
9393
assert.strictEqual(code, 0);
9494
const output = [
9595
'A',
9696
'> '
97-
].join('\n');
98-
assert.strictEqual(replStdout, output);
97+
];
98+
assert.ok(replStdout.startsWith(output[0]));
99+
assert.ok(replStdout.endsWith(output[1]));
99100
});
100101

101102
// Test that preload placement at other points in the cmdline
@@ -114,7 +115,7 @@ const interactive = childProcess.exec(
114115
`"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
115116
common.mustCall(function(err, stdout, stderr) {
116117
assert.ifError(err);
117-
assert.strictEqual(stdout, "> 'test'\n> ");
118+
assert.ok(stdout.endsWith("> 'test'\n> "));
118119
})
119120
);
120121

test/parallel/test-repl-harmony.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ const child = spawn(process.execPath, args);
3030
const input = '(function(){"use strict"; const y=1;y=2})()\n';
3131
// This message will vary based on JavaScript engine, so don't check the message
3232
// contents beyond confirming that the `Error` is a `TypeError`.
33-
const expectOut = /^> Thrown:\nTypeError: /;
33+
const expectOut = /> Thrown:\nTypeError: /;
3434

3535
child.stderr.setEncoding('utf8');
36-
child.stderr.on('data', function(c) {
36+
child.stderr.on('data', (d) => {
3737
throw new Error('child.stderr be silent');
3838
});
3939

4040
child.stdout.setEncoding('utf8');
4141
let out = '';
42-
child.stdout.on('data', function(c) {
43-
out += c;
42+
child.stdout.on('data', (d) => {
43+
out += d;
4444
});
45-
child.stdout.on('end', function() {
45+
child.stdout.on('end', () => {
4646
assert(expectOut.test(out));
4747
console.log('ok');
4848
});

test/parallel/test-repl-inspect-defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ child.stdout.on('data', (data) => {
1111
});
1212

1313
child.on('exit', common.mustCall(() => {
14-
const results = output.replace(/^> /mg, '').split('\n');
14+
const results = output.replace(/^> /mg, '').split('\n').slice(2);
1515
assert.deepStrictEqual(
1616
results,
1717
[

test/parallel/test-repl-require-after-write.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ child.stdout.on('data', (c) => {
2525
out += c;
2626
});
2727
child.stdout.on('end', common.mustCall(() => {
28-
assert.strictEqual(out, '> 1\n> ');
28+
assert.ok(out.endsWith('> 1\n> '));
2929
}));
3030

3131
child.stdin.end(input);

test/parallel/test-repl-require-context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ child.stdout.on('data', (data) => {
1313
});
1414

1515
child.on('exit', common.mustCall(() => {
16-
const results = output.replace(/^> /mg, '').split('\n');
16+
const results = output.replace(/^> /mg, '').split('\n').slice(2);
1717
assert.deepStrictEqual(results, ['undefined', 'true', 'true', '']);
1818
}));
1919

test/parallel/test-repl-unexpected-token-recoverable.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ const child = spawn(process.execPath, args);
1212

1313
const input = 'var foo = "bar\\\nbaz"';
1414
// Match '...' as well since it marks a multi-line statement
15-
const expectOut = /^> \.\.\. undefined\n/;
15+
const expectOut = /> \.\.\. undefined\n/;
1616

1717
child.stderr.setEncoding('utf8');
18-
child.stderr.on('data', function(c) {
18+
child.stderr.on('data', (d) => {
1919
throw new Error('child.stderr be silent');
2020
});
2121

2222
child.stdout.setEncoding('utf8');
2323
let out = '';
24-
child.stdout.on('data', function(c) {
25-
out += c;
24+
child.stdout.on('data', (d) => {
25+
out += d;
2626
});
2727

28-
child.stdout.on('end', function() {
28+
child.stdout.on('end', () => {
2929
assert(expectOut.test(out));
3030
console.log('ok');
3131
});

0 commit comments

Comments
 (0)