Skip to content

Instantly share code, notes, and snippets.

@bobisme
Created July 15, 2024 14:42
Show Gist options
  • Save bobisme/490c2bd925c240c54b67a9ab8163db7e to your computer and use it in GitHub Desktop.
Save bobisme/490c2bd925c240c54b67a9ab8163db7e to your computer and use it in GitHub Desktop.
Set up a REPL at the end of a script with history and preview.
import * as path from "node:path";
import * as repl from "node:repl";
(async () => {
// MAIN EXECUTION/SETUP GOES HERE
const replServer = repl.start({
prompt: ">> ",
useColors: true,
preview: true,
});
const historyFile = path.join(process.env.HOME ?? "", ".node_repl_history");
replServer.setupHistory(historyFile, (err) => {
if (err) console.warn("Failed to setup history:", err);
});
// SET UP CONTEXT TO BE AVAILABLE IN THE REPL
// replServer.context.thing = thing;
})().catch((err) => {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment