mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-16 10:19:10 +00:00
add tests; resync files, forgot where i was.
This commit is contained in:
parent
6b36b1d5f2
commit
1a794e2d7e
30 changed files with 3654 additions and 534 deletions
81
util/rpcrepl.js
Normal file
81
util/rpcrepl.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
import dotenv from 'dotenv'
|
||||
import repl from 'repl'
|
||||
import os from 'os'
|
||||
import { addAwaitOutsideToReplServer } from 'await-outside'
|
||||
import Roleypoly from '../Roleypoly'
|
||||
import chokidar from 'chokidar'
|
||||
import logger from '../logger'
|
||||
process.env.DEBUG = false
|
||||
process.env.IS_BOT = false
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const log = logger(__filename)
|
||||
|
||||
const RP = new Roleypoly(null, null)
|
||||
const reset = async (r) => {
|
||||
await RP.awaitServices()
|
||||
|
||||
r.context.RP = RP
|
||||
|
||||
r.context.ctx = { session: { userId: RP.ctx.discord.cfg.rootUsers.values().next().value } }
|
||||
r.context.guest = { session: {} }
|
||||
|
||||
r.context.g_rpc = {}
|
||||
r.context.rpc = {}
|
||||
r.context.reload = () => {
|
||||
RP.ctx.RPC.reload()
|
||||
r.context.$RPC = RP.ctx.RPC.rpcMap
|
||||
for (let fn in r.context.$RPC) {
|
||||
r.context.g_rpc[fn] = r.context.$RPC[fn].bind(null, r.context.guest)
|
||||
r.context.rpc[fn] = r.context.$RPC[fn].bind(null, r.context.ctx)
|
||||
}
|
||||
}
|
||||
|
||||
r.context.reload()
|
||||
}
|
||||
|
||||
const motd = () => {
|
||||
console.log(`~~ Roleypoly RPC REPL.
|
||||
\`ctx\` a mocked koa context, defaulting to first root user.
|
||||
\`rpc\` maps to all rpc functions, prefilled with ctx.
|
||||
|
||||
\`guest\` maps to a mock guest sessioned koa context.
|
||||
\`g_rpc\` maps to all rpc, prefilled with guest.
|
||||
|
||||
\`$RPC\` maps to all rpc functions as they are.
|
||||
\`RP\` maps to the Roleypoly app. It does NOT have HTTP stuff running.
|
||||
\`reload()\` to refresh RPC functions.
|
||||
\`reset()\` to reset this REPL.
|
||||
`)
|
||||
}
|
||||
|
||||
const start = async () => {
|
||||
if (repl.REPLServer.prototype.setupHistory == null) {
|
||||
console.log(' * History is available on node v11.10.0 and newer.\n')
|
||||
}
|
||||
const r = repl.start('> ')
|
||||
addAwaitOutsideToReplServer(r)
|
||||
r.setupHistory && r.setupHistory(os.homedir() + '/.ROLEYPOLY_RPCREPL_HISTORY', (e) => e && console.error(e))
|
||||
r.context.reset = async () => {
|
||||
await reset(r)
|
||||
motd()
|
||||
r.displayPrompt()
|
||||
}
|
||||
|
||||
await r.context.reset()
|
||||
const rpcWatcher = chokidar.watch('rpc/**', { persistent: true })
|
||||
rpcWatcher.on('ready', () => {
|
||||
rpcWatcher.on('all', (_, path) => {
|
||||
if (r.context.reload) r.context.reload()
|
||||
log.info('reloaded RPCs')
|
||||
r.displayPrompt()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
start().catch(e => console.error(e))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue