node/lib/internal/main/repl.js
Joyee Cheung 2b24ffae22
lib: print to stdout/stderr directly instead of using console
This patch adds an internal function that prints to stdout or
stderr by directly writing to the known file descriptor, and
uses it internally in common cases to avoid the overhead
of the console implementation.

PR-URL: https://github.com/nodejs/node/pull/27320
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-04-28 14:46:23 +08:00

56 lines
1.3 KiB
JavaScript

'use strict';
// Create the REPL if `-i` or `--interactive` is passed, or if
// the main module is not specified and stdin is a TTY.
const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
const {
evalScript
} = require('internal/process/execution');
const { print, kStderr, kStdout } = require('internal/util/print');
const { getOptionValue } = require('internal/options');
prepareMainThreadExecution();
markBootstrapComplete();
// --input-type flag not supported in REPL
if (getOptionValue('--input-type')) {
print(kStderr, 'Cannot specify --input-type for REPL');
process.exit(1);
}
print(kStdout, `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) {
throw err;
}
repl.on('exit', () => {
if (repl._flushing) {
repl.pause();
return repl.once('flushHistory', () => {
process.exit();
});
}
process.exit();
});
});
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
// evaluate the code in the current context.
const source = getOptionValue('--eval');
if (source != null) {
evalScript('[eval]',
source,
getOptionValue('--inspect-brk'),
getOptionValue('--print'));
}