-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
37 lines (33 loc) · 835 Bytes
/
index.js
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
29
30
31
32
33
34
35
36
37
const vm = require('vm')
const assert = require('assert')
const readline = require('readline')
async function debug (context) {
context = context || {}
assert.strictEqual(Object.prototype.toString.call(context), '[object Object]')
context.console = console // for debug
vm.createContext(context)
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> '
})
rl.prompt()
rl.on('line', (line) => {
vm.runInContext(`(async () => {
${line}
})()`, context).then(() => {
rl.prompt()
}).catch(e => {
console.error(e)
rl.prompt()
})
})
rl.on('close', () => {
rl.close()
console.log('')
resolve()
})
})
}
module.exports = debug