11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// If user passed `-c` or `--check` arguments to Node, check its syntax
41cb0ef41Sopenharmony_ci// instead of actually running the file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst { URL } = require('internal/url');
71cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options');
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  prepareMainThreadExecution,
101cb0ef41Sopenharmony_ci  markBootstrapComplete,
111cb0ef41Sopenharmony_ci} = require('internal/process/pre_execution');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst {
141cb0ef41Sopenharmony_ci  readStdin,
151cb0ef41Sopenharmony_ci} = require('internal/process/execution');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst { pathToFileURL } = require('url');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst {
201cb0ef41Sopenharmony_ci  Module: {
211cb0ef41Sopenharmony_ci    _resolveFilename: resolveCJSModuleName,
221cb0ef41Sopenharmony_ci  },
231cb0ef41Sopenharmony_ci  wrapSafe,
241cb0ef41Sopenharmony_ci} = require('internal/modules/cjs/loader');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// TODO(joyeecheung): not every one of these are necessary
271cb0ef41Sopenharmony_ciprepareMainThreadExecution(true);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciif (process.argv[1] && process.argv[1] !== '-') {
301cb0ef41Sopenharmony_ci  // Expand process.argv[1] into a full path.
311cb0ef41Sopenharmony_ci  const path = require('path');
321cb0ef41Sopenharmony_ci  process.argv[1] = path.resolve(process.argv[1]);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  // Read the source.
351cb0ef41Sopenharmony_ci  const filename = resolveCJSModuleName(process.argv[1]);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const fs = require('fs');
381cb0ef41Sopenharmony_ci  const source = fs.readFileSync(filename, 'utf-8');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  markBootstrapComplete();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  loadESMIfNeeded(() => checkSyntax(source, filename));
431cb0ef41Sopenharmony_ci} else {
441cb0ef41Sopenharmony_ci  markBootstrapComplete();
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  loadESMIfNeeded(() => readStdin((code) => {
471cb0ef41Sopenharmony_ci    checkSyntax(code, '[stdin]');
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cifunction loadESMIfNeeded(cb) {
521cb0ef41Sopenharmony_ci  const { getOptionValue } = require('internal/options');
531cb0ef41Sopenharmony_ci  const hasModulePreImport = getOptionValue('--import').length > 0;
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  if (hasModulePreImport) {
561cb0ef41Sopenharmony_ci    const { loadESM } = require('internal/process/esm_loader');
571cb0ef41Sopenharmony_ci    loadESM(cb);
581cb0ef41Sopenharmony_ci    return;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci  cb();
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciasync function checkSyntax(source, filename) {
641cb0ef41Sopenharmony_ci  let isModule = true;
651cb0ef41Sopenharmony_ci  if (filename === '[stdin]' || filename === '[eval]') {
661cb0ef41Sopenharmony_ci    isModule = getOptionValue('--input-type') === 'module' ||
671cb0ef41Sopenharmony_ci      (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs');
681cb0ef41Sopenharmony_ci  } else {
691cb0ef41Sopenharmony_ci    const { defaultResolve } = require('internal/modules/esm/resolve');
701cb0ef41Sopenharmony_ci    const { defaultGetFormat } = require('internal/modules/esm/get_format');
711cb0ef41Sopenharmony_ci    const { url } = await defaultResolve(pathToFileURL(filename).toString());
721cb0ef41Sopenharmony_ci    const format = await defaultGetFormat(new URL(url));
731cb0ef41Sopenharmony_ci    isModule = format === 'module';
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  if (isModule) {
771cb0ef41Sopenharmony_ci    const { ModuleWrap } = internalBinding('module_wrap');
781cb0ef41Sopenharmony_ci    new ModuleWrap(filename, undefined, source, 0, 0);
791cb0ef41Sopenharmony_ci    return;
801cb0ef41Sopenharmony_ci  }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  const { loadESM } = require('internal/process/esm_loader');
831cb0ef41Sopenharmony_ci  const { handleMainPromise } = require('internal/modules/run_main');
841cb0ef41Sopenharmony_ci  handleMainPromise(loadESM((loader) => wrapSafe(filename, source)));
851cb0ef41Sopenharmony_ci}
86