Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: add welcome message #27328

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ const {
evalScript
} = require('internal/process/execution');

const console = require('internal/console/global');

prepareMainThreadExecution();

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

console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');

const cliRepl = require('internal/repl');
cliRepl.createInternalRepl(process.env, (err, repl) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-force-repl-with-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cp.stdout.setEncoding('utf8');
let output = '';
cp.stdout.on('data', function(b) {
output += b;
if (output === '> 42\n') {
if (output.endsWith('> 42\n')) {
gotToEnd = true;
cp.kill();
}
Expand Down
15 changes: 11 additions & 4 deletions test/parallel/test-force-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ const assert = require('assert');
const spawn = require('child_process').spawn;

// 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']);

cp.stdout.setEncoding('utf8');

cp.stdout.once('data', common.mustCall(function(b) {
assert.strictEqual(b, '> ');
cp.kill();
let out = '';
cp.stdout.on('data', (d) => {
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('');
9 changes: 5 additions & 4 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ const replProc = childProcess.spawn(
);
replProc.stdin.end('.exit\n');
let replStdout = '';
replProc.stdout.on('data', function(d) {
replProc.stdout.on('data', (d) => {
replStdout += d;
});
replProc.on('close', function(code) {
assert.strictEqual(code, 0);
const output = [
'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
Expand All @@ -114,7 +115,7 @@ const interactive = childProcess.exec(
`"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
common.mustCall(function(err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout, "> 'test'\n> ");
assert.ok(stdout.endsWith("> 'test'\n> "));
})
);

Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-repl-harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const child = spawn(process.execPath, args);
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
// contents beyond confirming that the `Error` is a `TypeError`.
const expectOut = /^> Thrown:\nTypeError: /;
const expectOut = /> Thrown:\nTypeError: /;

child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) {
child.stderr.on('data', (d) => {
throw new Error('child.stderr be silent');
});

child.stdout.setEncoding('utf8');
let out = '';
child.stdout.on('data', function(c) {
out += c;
child.stdout.on('data', (d) => {
out += d;
});
child.stdout.on('end', function() {
child.stdout.on('end', () => {
assert(expectOut.test(out));
console.log('ok');
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-inspect-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ child.stdout.on('data', (data) => {
});

child.on('exit', common.mustCall(() => {
const results = output.replace(/^> /mg, '').split('\n');
const results = output.replace(/^> /mg, '').split('\n').slice(2);
assert.deepStrictEqual(
results,
[
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-require-after-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ child.stdout.on('data', (c) => {
out += c;
});
child.stdout.on('end', common.mustCall(() => {
assert.strictEqual(out, '> 1\n> ');
assert.ok(out.endsWith('> 1\n> '));
}));

child.stdin.end(input);
2 changes: 1 addition & 1 deletion test/parallel/test-repl-require-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ child.stdout.on('data', (data) => {
});

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', '']);
}));

Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-repl-unexpected-token-recoverable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ const child = spawn(process.execPath, args);

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

child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) {
child.stderr.on('data', (d) => {
throw new Error('child.stderr be silent');
});

child.stdout.setEncoding('utf8');
let out = '';
child.stdout.on('data', function(c) {
out += c;
child.stdout.on('data', (d) => {
out += d;
});

child.stdout.on('end', function() {
child.stdout.on('end', () => {
assert(expectOut.test(out));
console.log('ok');
});
Expand Down