-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepl.js
More file actions
28 lines (22 loc) · 805 Bytes
/
Copy pathrepl.js
File metadata and controls
28 lines (22 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const log = require('debug')('ipfs-friends:repl')
const { read } = require('./read')
const { evaluate } = require('./eval')
const print = require('./print')
const loop = require('./loop')
const { withAutoComplete } = require('./auto-complete')
const { withSpin } = require('./spinner')
module.exports = async function repl (ctx, opts) {
opts = opts || {}
opts.read = opts.read || withAutoComplete(read)
opts.evaluate = opts.evaluate || withSpin(evaluate)
return loop(async function rep () {
const { input } = await opts.read(ctx)
let [ cmd, ...cmdArgs ] = input.split(' ').filter(Boolean)
log(cmd, cmdArgs)
await print(async () => {
const res = await opts.evaluate(ctx, cmd, cmdArgs)
if (res && res.ctx) Object.assign(ctx, res.ctx)
return res
})
})
}