-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
46 lines (37 loc) · 1.05 KB
/
repl.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
40
41
42
43
44
"use strict"
var Repl = require('module').requireRepl()
var Domain = require('domain')
var defaults = require('defaults')
var transpile = require('./transpile')
module.exports = start
function start(opts) {
var domain = opts.domain || Domain.create()
var repl = Repl.start(defaults({
domain: domain,
useGlobal: true,
ignoreUndefined: false
}, opts))
// use of domain here allows capture of
// both sync and async errors.
domain.on('error', function(e) {
repl.outputStream.write((e.stack || e) + '\n');
repl.bufferedCommand = '';
repl.lines.level = [];
repl.displayPrompt();
})
var originalEval = repl.eval
// before executing original repl eval,
// compile src and add new globals to context.
function eval_(src, context, filename, callback) {
try {
src = transpile(src, {modules: 'inline'})
} catch(err) {
err.name = 'SyntaxError'
return callback(err, src)
}
arguments[0] = src
return originalEval.apply(this, arguments)
}
repl.eval = domain.bind(eval_)
return repl
}