forked from ownadi/async-repl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stubber.js
31 lines (29 loc) · 816 Bytes
/
stubber.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
const history = require('repl.history');
const os = require('os');
const path = require('path');
const historyFile = path.join(os.homedir(), '.async_repl_history');
const rewrite = require('./rewrite');
module.exports = function(repl){
const originalEval = repl.eval;
history(repl, historyFile);
repl.eval = awaitingEval;
function awaitingEval(cmd, context, filename, callback) {
try {
cmd = rewrite(cmd);
} catch (err) {
callback(err);
}
originalEval.call(this, cmd, context, filename, async function(err,value) {
if (err) {
callback.call(this, err, null);
} else {
try {
value = await value;
callback.call(this, err, value);
} catch (error) {
callback.call(this, error, null);
}
}
});
}
};