nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
42 lines (36 loc) • 1.21 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/main/eval_stdin.js
import {
prepareMainThreadExecution,
markBootstrapComplete,
} from "nstdlib/lib/internal/process/pre_execution";
import { getOptionValue } from "nstdlib/lib/internal/options";
import {
evalModuleEntryPoint,
evalScript,
readStdin,
} from "nstdlib/lib/internal/process/execution";
// Stdin is not a TTY, we will read it and execute it.
prepareMainThreadExecution();
markBootstrapComplete();
readStdin((code) => {
// This is necessary for fork() and CJS module compilation.
// TODO(joyeecheung): pass this with something really internal.
process._eval = code;
const print = getOptionValue("--print");
const shouldLoadESM = getOptionValue("--import").length > 0;
if (
getOptionValue("--input-type") === "module" ||
(getOptionValue("--experimental-default-type") === "module" &&
getOptionValue("--input-type") !== "commonjs")
) {
((...args) => globalThis.evalModuleEntryPoint(...args))(code, print);
} else {
((...args) => globalThis.evalScript(...args))(
"[stdin]",
code,
getOptionValue("--inspect-brk"),
print,
shouldLoadESM,
);
}
});