Repository: denoland/deno
File: ext/node/polyfills/internal/child_process.ts (TypeScript)

Code before:
  "fmt",
  "help",
  "info",
  "init",
  "install",
  "lint",
  "lsp",
  "publish",
  "repl",
  "run",
  "tasks",
  "test",
  "types",
  "uninstall",
  "upgrade",
  "vendor",
]);

/** Wraps the script for (Node.js) --eval / --print argument
 * Note: Builtin modules are available as global variables */
function wrapScriptForEval(sourceCode: string): string {
  return `
    process.getBuiltinModule("module").builtinModules
      .filter((m) => !/\\/|crypto|process/.test(m))
      .forEach((m) => { globalThis[m] = process.getBuiltinModule(m); }),
    vm.runInThisContext(atob("${btoa(sourceCode)}"))
  `;
}

/** Returns deno args and NODE_OPTIONS for simulating Node.js cli */
function toDenoArgs(args: string[]): [string[], string[]] {
  if (args.length === 0) {
    return [args, args];
  }

  // Update this logic as more CLI arguments are mapped from Node to Deno.
  const denoArgs: string[] = [];
  const nodeOptions: string[] = [];
  let useRunArgs = true;

  for (let i = 0; i < args.length; i++) {
    const arg = args[i];

    if (arg.charAt(0) !== "-" || arg === "--") {
      // Not a flag or no more arguments.

      // If the arg is a Deno subcommand, then the child process is being
      // spawned as Deno, not Deno in Node compat mode. In this case, bail out
      // and return the original args.
      if (kDenoSubcommands.has(arg)) {
        return [args, []];

Review comment (suggestion):
Note: We need `vm.runInThisContext` call here to get the last evaluated value of the source with multiple statements. `deno eval -p` surrounds the source code like `console.log(${source})`, and it only allows a single expression.

Code after:
  "fmt",
  "help",
  "info",
  "init",
  "install",
  "lint",
  "lsp",
  "publish",
  "repl",
  "run",
  "tasks",
  "test",
  "types",
  "uninstall",
  "upgrade",
  "vendor",
]);

/** Wraps the script for (Node.js) --eval / --print argument
 * Note: Builtin modules are available as global variables */
function wrapScriptForEval(sourceCode: string): string {
  // Note: We need vm.runInThisContext call here to get the last evaluated
  // value of the source with multiple statements. `deno eval -p` surrounds
  // the source code like `console.log(${source})`, and it only allows a
  // single expression.
  return `
    process.getBuiltinModule("module").builtinModules
      .filter((m) => !/\\/|crypto|process/.test(m))
      .forEach((m) => { globalThis[m] = process.getBuiltinModule(m); }),
    vm.runInThisContext(${JSON.stringify(sourceCode)})
  `;
}

/** Returns deno args and NODE_OPTIONS for simulating Node.js cli */
function toDenoArgs(args: string[]): [string[], string[]] {
  if (args.length === 0) {
    return [args, args];
  }

  // Update this logic as more CLI arguments are mapped from Node to Deno.
  const denoArgs: string[] = [];
  const nodeOptions: string[] = [];
  let useRunArgs = true;

  for (let i = 0; i < args.length; i++) {
    const arg = args[i];

    if (arg.charAt(0) !== "-" || arg === "--") {
      // Not a flag or no more arguments.

      // If the arg is a Deno subcommand, then the child process is being