function startREPL() {
  const outputPath = args["--output"];
  let printFn = (content) => console.log(content);
  let fileLogger;
  if (outputPath) {
    // also write stdout to a file
    const ws = fs.createWriteStream(outputPath);
    [
      `exit`,
      `SIGINT`,
      `SIGUSR1`,
      `SIGUSR2`,
      `uncaughtException`,
      `SIGTERM`,
    ].forEach((eventType) => {
      process.on(eventType, () => {
        ws.close();
      });
    });
    fileLogger = (content) => {
      ws.write(content);
    };
    printFn = (content) => {
      console.log(content);
      fileLogger(content);
      fileLogger(os.EOL);
    };
  }

  // Print the versions and other info
  printFn(
    `Hello ${os.userInfo().username} on NodeJS ${
      process.version
    } ${os.type()}-${os.release()}-${os.arch()} :)`
  );
  printFn(`Versions > js2py v${packageInfoVersion}`);
  printFn(
    `Please report any bugs you encounter in full detail on: ${packageInfoBugs.url}`
  );

  historyFP =
    args["--history"] ||
    path.join(os.userInfo().homedir, ".js2py_repl_history");

  // Start the CLI in REPL mode
  const js2pyREPL = new REPL({
    evalPython: !!args["--eval"],
    evalPythonServer: args["--eval-server"],
  });

  const js2pyREPLRunner = repl.start({
    prompt: ">>> ",
    eval: function (cmd, context, filename, callback) {
      js2pyREPL.eval(cmd, context, filename, (error, output) => {
        if (!error && fileLogger) {
          let lines = cmd.trim().split(os.EOL);
          fileLogger(`>>> ${lines[0]}${os.EOL}`);
          lines.slice(1).forEach((line) => {
            fileLogger(`... ${line}${os.EOL}`);
          });
        }
        callback(error, output);
      });
    },
    writer: function (...args) {
      const output = js2pyREPL.write(...args);
      if (fileLogger) {
        fileLogger(output);
        fileLogger(os.EOL);
      }
      return output;
    },
    ignoreUndefined: true,
    preview: false,
  });

  js2pyREPLRunner.on("close", () => {
    js2pyREPL.close();
    // write commands used in current session to file
    fs.appendFileSync(historyFP, js2pyREPLRunner.lines.join("\n") + "\n");
  });

  try {
    // synchronous methods are fine, here. you don't want to run a REPL on any middleware that
    // handles user requests, anyway.
    fs.statSync(historyFP);

    // load command history from history file in the current directory
    fs.readFileSync(historyFP)
      .toString()
      .split("\n")
      .reverse()
      .filter((line) => line.trim())
      .map((line) => js2pyREPLRunner.history.push(line));
  } catch (err) {
    if (err.code !== "ENOENT") {
      throw err;
    }
  }
}
// used for an interactive REPL session
used for an interactive REPL session
used for an interactive REPL session
function startREPL() {
  const outputPath = args["--output"];
  let printFn = (content) => console.log(content);
  let fileLogger;
  if (outputPath) {
    const ws = fs.createWriteStream(outputPath);
    [
      `exit`,
      `SIGINT`,
      `SIGUSR1`,
      `SIGUSR2`,
      `uncaughtException`,
      `SIGTERM`,
    ].forEach((eventType) => {
      process.on(eventType, () => {
        ws.close();
      });
    });
    fileLogger = (content) => {
      ws.write(content);
    };
    printFn = (content) => {
      console.log(content);
      fileLogger(content);
      fileLogger(os.EOL);
    };
  }
  printFn(
    `Hello ${os.userInfo().username} on NodeJS ${
      process.version
    } ${os.type()}-${os.release()}-${os.arch()} :)`
  );
  printFn(`Versions > js2py v${packageInfoVersion}`);
  printFn(
    `Please report any bugs you encounter in full detail on: ${packageInfoBugs.url}`
  );
  historyFP =
    args["--history"] ||
    path.join(os.userInfo().homedir, ".js2py_repl_history");
  const js2pyREPL = new REPL({
    evalPython: !!args["--eval"],
    evalPythonServer: args["--eval-server"],
  });
  const js2pyREPLRunner = repl.start({
    prompt: ">>> ",
    eval: function (cmd, context, filename, callback) {
      js2pyREPL.eval(cmd, context, filename, (error, output) => {
        if (!error && fileLogger) {
          let lines = cmd.trim().split(os.EOL);
          fileLogger(`>>> ${lines[0]}${os.EOL}`);
          lines.slice(1).forEach((line) => {
            fileLogger(`... ${line}${os.EOL}`);
          });
        }
        callback(error, output);
      });
    },
    writer: function (...args) {
      const output = js2pyREPL.write(...args);
      if (fileLogger) {
        fileLogger(output);
        fileLogger(os.EOL);
      }
      return output;
    },
    ignoreUndefined: true,
    preview: false,
  });
  js2pyREPLRunner.on("close", () => {
    js2pyREPL.close();
    fs.appendFileSync(historyFP, js2pyREPLRunner.lines.join("\n") + "\n");
  });
  try {
    fs.statSync(historyFP);
    fs.readFileSync(historyFP)
      .toString()
      .split("\n")
      .reverse()
      .filter((line) => line.trim())
      .map((line) => js2pyREPLRunner.history.push(line));
  } catch (err) {
    if (err.code !== "ENOENT") {
      throw err;
    }
  }
}
function startREPL ( ) { const outputPath = args [ "--output" ] ; let printFn = ( content ) => console . log ( content ) ; let fileLogger ; if ( outputPath ) { const ws = fs . createWriteStream ( outputPath ) ; [ ` ` , ` ` , ` ` , ` ` , ` ` , ` ` , ] . forEach ( ( eventType ) => { process . on ( eventType , ( ) => { ws . close ( ) ; } ) ; } ) ; fileLogger = ( content ) => { ws . write ( content ) ; } ; printFn = ( content ) => { console . log ( content ) ; fileLogger ( content ) ; fileLogger ( os . EOL ) ; } ; } printFn ( ` ${ os . userInfo ( ) . username } ${ process . version } ${ os . type ( ) } ${ os . release ( ) } ${ os . arch ( ) } ` ) ; printFn ( ` ${ packageInfoVersion } ` ) ; printFn ( ` ${ packageInfoBugs . url } ` ) ; historyFP = args [ "--history" ] || path . join ( os . userInfo ( ) . homedir , ".js2py_repl_history" ) ; const js2pyREPL = new REPL ( { evalPython : ! ! args [ "--eval" ] , evalPythonServer : args [ "--eval-server" ] , } ) ; const js2pyREPLRunner = repl . start ( { prompt : ">>> " , eval : function ( cmd , context , filename , callback ) { js2pyREPL . eval ( cmd , context , filename , ( error , output ) => { if ( ! error && fileLogger ) { let lines = cmd . trim ( ) . split ( os . EOL ) ; fileLogger ( ` ${ lines [ 0 ] } ${ os . EOL } ` ) ; lines . slice ( 1 ) . forEach ( ( line ) => { fileLogger ( ` ${ line } ${ os . EOL } ` ) ; } ) ; } callback ( error , output ) ; } ) ; } , writer : function ( ... args ) { const output = js2pyREPL . write ( ... args ) ; if ( fileLogger ) { fileLogger ( output ) ; fileLogger ( os . EOL ) ; } return output ; } , ignoreUndefined : true , preview : false , } ) ; js2pyREPLRunner . on ( "close" , ( ) => { js2pyREPL . close ( ) ; fs . appendFileSync ( historyFP , js2pyREPLRunner . lines . join ( "\n" ) + "\n" ) ; } ) ; try { fs . statSync ( historyFP ) ; fs . readFileSync ( historyFP ) . toString ( ) . split ( "\n" ) . reverse ( ) . filter ( ( line ) => line . trim ( ) ) . map ( ( line ) => js2pyREPLRunner . history . push ( line ) ) ; } catch ( err ) { if ( err . code !== "ENOENT" ) { throw err ; } } }
used for an interactive REPL session
used for an interactive REPL session
// also write stdout to a file // Print the versions and other info // Start the CLI in REPL mode // write commands used in current session to file // synchronous methods are fine, here. you don't want to run a REPL on any middleware that // handles user requests, anyway. // load command history from history file in the current directory