1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-16 13:17:30 +00:00
bibliogram/src/site/repl.js

34 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-01-18 15:38:14 +00:00
const {instance, pugCache, wss} = require("./passthrough")
2020-02-02 14:53:37 +00:00
const {userRequestCache, timelineEntryCache, history} = require("../lib/collectors")
2020-01-28 03:14:21 +00:00
const constants = require("../lib/constants")
2020-04-07 06:30:00 +00:00
const collectors = require("../lib/collectors")
2020-01-18 15:38:14 +00:00
const util = require("util")
const repl = require("repl")
const vm = require("vm")
/**
* @param {string} input
* @param {vm.Context} context
* @param {string} filename
* @param {(err: Error|null, result: any) => any} callback
*/
async function customEval(input, context, filename, callback) {
let depth = 0
if (input == "exit\n") return process.exit()
if (input.startsWith(":")) {
const depthOverwrite = input.split(" ")[0]
depth = +depthOverwrite.slice(1)
input = input.slice(depthOverwrite.length + 1)
}
const result = await eval(input)
const output = util.inspect(result, false, depth, true)
return callback(undefined, output)
}
function customWriter(output) {
return output
}
console.log("REPL started")
repl.start({prompt: "b) ", eval: customEval, writer: customWriter}).once("exit", () => process.exit())