xref: /third_party/node/lib/internal/main/repl.js (revision 1cb0ef41)
1'use strict';
2
3// Create the REPL if `-i` or `--interactive` is passed, or if
4// the main module is not specified and stdin is a TTY.
5
6const {
7  prepareMainThreadExecution,
8  markBootstrapComplete,
9} = require('internal/process/pre_execution');
10
11const {
12  evalScript,
13} = require('internal/process/execution');
14
15const console = require('internal/console/global');
16
17const { getOptionValue } = require('internal/options');
18
19prepareMainThreadExecution();
20
21markBootstrapComplete();
22
23if (process.env.NODE_REPL_EXTERNAL_MODULE) {
24  require('internal/modules/cjs/loader')
25    .Module
26    ._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true);
27} else {
28  // --input-type flag not supported in REPL
29  if (getOptionValue('--input-type')) {
30    // If we can't write to stderr, we'd like to make this a noop,
31    // so use console.error.
32    console.error('Cannot specify --input-type for REPL');
33    process.exit(1);
34  }
35
36  const esmLoader = require('internal/process/esm_loader');
37  esmLoader.loadESM(() => {
38    console.log(`Welcome to Node.js ${process.version}.\n` +
39      'Type ".help" for more information.');
40
41    const cliRepl = require('internal/repl');
42    cliRepl.createInternalRepl(process.env, (err, repl) => {
43      if (err) {
44        throw err;
45      }
46      repl.on('exit', () => {
47        if (repl._flushing) {
48          repl.pause();
49          return repl.once('flushHistory', () => {
50            process.exit();
51          });
52        }
53        process.exit();
54      });
55    });
56
57    // If user passed '-e' or '--eval' along with `-i` or `--interactive`,
58    // evaluate the code in the current context.
59    if (getOptionValue('[has_eval_string]')) {
60      evalScript('[eval]',
61                 getOptionValue('--eval'),
62                 getOptionValue('--inspect-brk'),
63                 getOptionValue('--print'));
64    }
65  });
66}
67