-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.js
39 lines (34 loc) · 948 Bytes
/
console.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
38
39
const repl = require('repl')
const _ = require('underscore')
const testUtils = require('./test/utils')
_.extend(global, require('./test/utils'))
_.extend(global, require('./lib/utils'))
const isPromise = (value) => {
if (typeof value === 'object') {
return Object.getPrototypeOf(value) === Promise.prototype
} else {
return false
}
}
//_cmd so we can have a fucntion called cmd in global context
//pretty straightforward eval, except it waits for promises
const runLine = (_cmd, context, filename, callback) => {
const result = eval(_cmd)
if (!isPromise(result)) {
callback(null, result)
} else {
result.then((value) => {
callback(null, value)
}).catch((failure) => {
callback(failure)
})
}
}
const myWriter = function(output) {
if (typeof output === 'object'){
return JSON.stringify(output)
} else {
return output
}
}
repl.start({prompt: '> ', eval: runLine, writer: myWriter})