-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
66 lines (57 loc) · 1.38 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var repl = require('repl');
var debug = require('debuglog');
var vm = require('vm');
var babel = require('babel-core');
/*
* default eval method from node source
* lib/repl.js
*/
module.exports.defaultEval = function (code, context, file, cb) {
var err, result;
// first, create the Script object to check the syntax
try {
var script = vm.createScript(code, {
filename: file,
displayErrors: false
});
} catch (e) {
err = e;
debug('parse error %j', code, e);
}
if (!err) {
try {
if (this.useGlobal) {
result = script.runInThisContext({ displayErrors: false });
} else {
result = script.runInContext(context, { displayErrors: false });
}
} catch (e) {
err = e;
if (err && process.domain) {
debug('not recoverable, send to domain');
process.domain.emit('error', err);
process.domain.exit();
return;
}
}
}
cb(err, result);
}
/*
* start babel repl
*/
module.exports.start = function (options) {
var defaults = {
prompt: "> ",
useGlobal: true,
eval: function (code, context, file, cb) {
code = babel.transform(code).code;
module.exports.defaultEval.call(this, code, context, file, cb);
}
};
options = options || {};
for (var k in defaults) {
if (!(k in options)) options[k] = defaults[k];
}
return repl.start(options);
};