From 0471b8d211a099f7b6ab5d38e6f56d936630ff4a Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Sat, 15 Jul 2023 05:51:44 -0600 Subject: [PATCH] feat: transform `import` into `await import()` in the repl --- src/repl_globals.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/repl_globals.js b/src/repl_globals.js index e88d6ac..f0c5168 100644 --- a/src/repl_globals.js +++ b/src/repl_globals.js @@ -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})) @@ -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()