Skip to content

Commit

Permalink
feat: transform import into await import() in the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Jul 15, 2023
1 parent bb1ef66 commit 0471b8d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/repl_globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const util = require("util")

// TODO check if globals are accessible in a repl

// TODO https://stackoverflow.com/questions/60558170/how-to-auto-reload-files-in-node-js-while-using-esm
// global.awaitReload

// https://stackoverflow.com/questions/5670752/how-can-i-pretty-print-json-using-node-js
global.dir = (ob) => process.stdout.write(require("util").inspect(ob, {depth: null, colors: true}))

Expand Down Expand Up @@ -84,20 +87,39 @@ async function repl(context = {}) {

console.log("Starting REPL...")


const repl = require("repl")
const replServer = repl.start({
prompt: "repl> ",
prompt: " ",
input: process.stdin,
output: process.stdout,
useGlobal: true,
preview: false,
})

// transform import statements into `await import` statements
const _eval = replServer.eval
async function inlineImports(cmd, context, filename, callback) {
if (cmd.startsWith("import")) {
cmd = cmd.replace(/import (.+) from (.+)/, "let $1 = await import($2);")
}

_eval(cmd, context, filename, callback)
}
replServer.eval = inlineImports

replServer.setupHistory(
'.node_repl_history',
(error, replServer) => {
if (error) throw error
}
)

// TODO tie into the ts-node repl
// TODO remove all 'const ' from the input to avoid redeclaring variables error

// list out all local variables
replServer.defineCommand("vars", {
replServer.defineCommand("locals", {
help: "List all local variables",
action: function () {
this.displayPrompt()
Expand Down

0 comments on commit 0471b8d

Please sign in to comment.