UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

86 lines (70 loc) 2.83 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/main/check_syntax.js import * as __hoisted_fs__ from "nstdlib/lib/fs"; import * as __hoisted_internal_modules_run_main__ from "nstdlib/lib/internal/modules/run_main"; import * as __hoisted_internal_modules_esm_resolve__ from "nstdlib/lib/internal/modules/esm/resolve"; import * as __hoisted_internal_modules_esm_get_format__ from "nstdlib/lib/internal/modules/esm/get_format"; import { getOptionValue } from "nstdlib/lib/internal/options"; import { URL, pathToFileURL } from "nstdlib/lib/internal/url"; import { prepareMainThreadExecution, markBootstrapComplete, } from "nstdlib/lib/internal/process/pre_execution"; import { readStdin } from "nstdlib/lib/internal/process/execution"; import { Module as __Module__, wrapSafe, } from "nstdlib/lib/internal/modules/cjs/loader"; import * as __hoisted_path__ from "nstdlib/lib/path"; // If user passed `-c` or `--check` arguments to Node, check its syntax // instead of actually running the file. const { _resolveFilename: resolveCJSModuleName } = __Module__; // TODO(joyeecheung): not every one of these are necessary prepareMainThreadExecution(true); if (process.argv[1] && process.argv[1] !== "-") { // Expand process.argv[1] into a full path. const path = __hoisted_path__; process.argv[1] = path.resolve(process.argv[1]); // Read the source. const filename = resolveCJSModuleName(process.argv[1]); const fs = __hoisted_fs__; const source = fs.readFileSync(filename, "utf-8"); markBootstrapComplete(); loadESMIfNeeded(() => checkSyntax(source, filename)); } else { markBootstrapComplete(); loadESMIfNeeded(() => readStdin((code) => { checkSyntax(code, "[stdin]"); }), ); } function loadESMIfNeeded(cb) { const hasModulePreImport = getOptionValue("--import").length > 0; if (hasModulePreImport) { __hoisted_internal_modules_run_main__.runEntryPointWithESMLoader(cb); return; } cb(); } async function checkSyntax(source, filename) { let format; if (filename === "[stdin]" || filename === "[eval]") { format = getOptionValue("--input-type") === "module" || (getOptionValue("--experimental-default-type") === "module" && getOptionValue("--input-type") !== "commonjs") ? "module" : "commonjs"; } else { const { defaultResolve } = __hoisted_internal_modules_esm_resolve__; const { defaultGetFormat } = __hoisted_internal_modules_esm_get_format__; const { url } = await defaultResolve(pathToFileURL(filename).toString()); format = await defaultGetFormat(new URL(url)); } if (format === "module") { const { ModuleWrap } = require("binding/module_wrap"); new ModuleWrap(filename, undefined, source, 0, 0); return; } wrapSafe(filename, source, undefined, format); }