11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Create the REPL if `-i` or `--interactive` is passed, or if
41cb0ef41Sopenharmony_ci// the main module is not specified and stdin is a TTY.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  prepareMainThreadExecution,
81cb0ef41Sopenharmony_ci  markBootstrapComplete,
91cb0ef41Sopenharmony_ci} = require('internal/process/pre_execution');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  evalScript,
131cb0ef41Sopenharmony_ci} = require('internal/process/execution');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst console = require('internal/console/global');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciprepareMainThreadExecution();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cimarkBootstrapComplete();
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciif (process.env.NODE_REPL_EXTERNAL_MODULE) {
241cb0ef41Sopenharmony_ci  require('internal/modules/cjs/loader')
251cb0ef41Sopenharmony_ci    .Module
261cb0ef41Sopenharmony_ci    ._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true);
271cb0ef41Sopenharmony_ci} else {
281cb0ef41Sopenharmony_ci  // --input-type flag not supported in REPL
291cb0ef41Sopenharmony_ci  if (getOptionValue('--input-type')) {
301cb0ef41Sopenharmony_ci    // If we can't write to stderr, we'd like to make this a noop,
311cb0ef41Sopenharmony_ci    // so use console.error.
321cb0ef41Sopenharmony_ci    console.error('Cannot specify --input-type for REPL');
331cb0ef41Sopenharmony_ci    process.exit(1);
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const esmLoader = require('internal/process/esm_loader');
371cb0ef41Sopenharmony_ci  esmLoader.loadESM(() => {
381cb0ef41Sopenharmony_ci    console.log(`Welcome to Node.js ${process.version}.\n` +
391cb0ef41Sopenharmony_ci      'Type ".help" for more information.');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    const cliRepl = require('internal/repl');
421cb0ef41Sopenharmony_ci    cliRepl.createInternalRepl(process.env, (err, repl) => {
431cb0ef41Sopenharmony_ci      if (err) {
441cb0ef41Sopenharmony_ci        throw err;
451cb0ef41Sopenharmony_ci      }
461cb0ef41Sopenharmony_ci      repl.on('exit', () => {
471cb0ef41Sopenharmony_ci        if (repl._flushing) {
481cb0ef41Sopenharmony_ci          repl.pause();
491cb0ef41Sopenharmony_ci          return repl.once('flushHistory', () => {
501cb0ef41Sopenharmony_ci            process.exit();
511cb0ef41Sopenharmony_ci          });
521cb0ef41Sopenharmony_ci        }
531cb0ef41Sopenharmony_ci        process.exit();
541cb0ef41Sopenharmony_ci      });
551cb0ef41Sopenharmony_ci    });
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    // If user passed '-e' or '--eval' along with `-i` or `--interactive`,
581cb0ef41Sopenharmony_ci    // evaluate the code in the current context.
591cb0ef41Sopenharmony_ci    if (getOptionValue('[has_eval_string]')) {
601cb0ef41Sopenharmony_ci      evalScript('[eval]',
611cb0ef41Sopenharmony_ci                 getOptionValue('--eval'),
621cb0ef41Sopenharmony_ci                 getOptionValue('--inspect-brk'),
631cb0ef41Sopenharmony_ci                 getOptionValue('--print'));
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci}
67