• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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} = require('internal/bootstrap/pre_execution');
9
10const esmLoader = require('internal/process/esm_loader');
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  esmLoader.loadESM(() => {
37    console.log(`Welcome to Node.js ${process.version}.\n` +
38      'Type ".help" for more information.');
39
40    const cliRepl = require('internal/repl');
41    cliRepl.createInternalRepl(process.env, (err, repl) => {
42      if (err) {
43        throw err;
44      }
45      repl.on('exit', () => {
46        if (repl._flushing) {
47          repl.pause();
48          return repl.once('flushHistory', () => {
49            process.exit();
50          });
51        }
52        process.exit();
53      });
54    });
55
56    // If user passed '-e' or '--eval' along with `-i` or `--interactive`,
57    // evaluate the code in the current context.
58    if (getOptionValue('[has_eval_string]')) {
59      evalScript('[eval]',
60                 getOptionValue('--eval'),
61                 getOptionValue('--inspect-brk'),
62                 getOptionValue('--print'));
63    }
64  });
65}
66