/
repl.js
157 lines (157 loc) · 4.91 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Generated by CoffeeScript 2.0.0-beta4
var addHistory, addMultilineHandler, CoffeeScript, CS, fs, merge, nodeREPL, path, vm;
fs = require('fs');
path = require('path');
vm = require('vm');
nodeREPL = require('repl');
CoffeeScript = require('./module');
CS = require('./nodes');
merge = require('./helpers').merge;
addMultilineHandler = function (repl) {
var buffer, cache$, continuationPrompt, enabled, initialPrompt, inputStream, nodeLineListener, outputStream, rli;
cache$ = repl;
rli = cache$.rli;
inputStream = cache$.inputStream;
outputStream = cache$.outputStream;
initialPrompt = repl.prompt.replace(/^[^> ]*/, function (x) {
return x.replace(/./g, '-');
});
continuationPrompt = repl.prompt.replace(/^[^> ]*>?/, function (x) {
return x.replace(/./g, '.');
});
enabled = false;
buffer = '';
nodeLineListener = rli.listeners('line')[0];
rli.removeListener('line', nodeLineListener);
rli.on('line', function (cmd) {
if (enabled) {
buffer += '' + cmd + '\n';
rli.setPrompt(continuationPrompt);
rli.prompt(true);
} else {
nodeLineListener(cmd);
}
});
return inputStream.on('keypress', function (char, key) {
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v'))
return;
if (enabled) {
if (!buffer.match(/\n/)) {
enabled = !enabled;
rli.setPrompt(repl.prompt);
rli.prompt(true);
return;
}
if (null != rli.line && !rli.line.match(/^\s*$/))
return;
enabled = !enabled;
rli.line = '';
rli.cursor = 0;
rli.output.cursorTo(0);
rli.output.clearLine(1);
buffer = buffer.replace(/\n/g, '\uff00');
rli.emit('line', buffer);
buffer = '';
} else {
enabled = !enabled;
rli.setPrompt(initialPrompt);
rli.prompt(true);
}
});
};
addHistory = function (repl, filename, maxSize) {
var buffer, e, fd, lastLine, original_clear, readFd, size, stat;
try {
stat = fs.statSync(filename);
size = Math.min(maxSize, stat.size);
readFd = fs.openSync(filename, 'r');
buffer = new Buffer(size);
if (size)
fs.readSync(readFd, buffer, 0, size, stat.size - size);
repl.rli.history = buffer.toString().split('\n').reverse();
if (stat.size > maxSize)
repl.rli.history.pop();
if (repl.rli.history[0] === '')
repl.rli.history.shift();
repl.rli.historyIndex = -1;
} catch (e$) {
e = e$;
repl.rli.history = [];
}
fd = fs.openSync(filename, 'a');
lastLine = repl.rli.history[0];
repl.rli.addListener('line', function (code) {
if (code && code !== lastLine) {
lastLine = code;
return fs.writeSync(fd, '' + code + '\n');
}
});
original_clear = repl.commands['.clear'].action;
repl.commands['.clear'].action = function () {
repl.outputStream.write('Clearing history...\n');
repl.rli.history = [];
fs.closeSync(fd);
fd = fs.openSync(filename, 'w');
lastLine = void 0;
return original_clear.call(this);
};
return repl.commands['.history'] = {
help: 'Show command history',
action: function () {
repl.outputStream.write('' + repl.rli.history.slice().reverse().join('\n') + '\n');
return repl.displayPrompt();
}
};
};
module.exports = {
start: function (opts) {
var repl;
if (null == opts)
opts = {};
opts.prompt || (opts.prompt = 'coffee> ');
if (null != opts.ignoreUndefined)
opts.ignoreUndefined;
else
opts.ignoreUndefined = true;
if (null != opts.historyFile)
opts.historyFile;
else
opts.historyFile = path.join(process.env.HOME, '.coffee_history');
if (null != opts.historyMaxInputSize)
opts.historyMaxInputSize;
else
opts.historyMaxInputSize = 10 * 1024;
opts['eval'] || (opts['eval'] = function (input, context, filename, cb) {
var err, inputAst, js, jsAst, transformedAst;
input = input.replace(/\uFF00/g, '\n');
input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
input = input.replace(/(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3');
if (/^\s*$/.test(input))
return cb(null);
try {
inputAst = CoffeeScript.parse(input, {
filename: filename,
raw: true
});
transformedAst = new CS.AssignOp(new CS.Identifier('_'), inputAst.body);
jsAst = CoffeeScript.compile(transformedAst, {
bare: true,
inScope: Object.keys(context)
});
js = CoffeeScript.js(jsAst);
return cb(null, vm.runInContext(js, context, filename));
} catch (e$) {
err = e$;
return cb('\x1b[0;31m' + err.constructor.name + ': ' + err.message + '\x1b[0m');
}
});
repl = nodeREPL.start(opts);
repl.on('exit', function () {
return repl.outputStream.write('\n');
});
addMultilineHandler(repl);
if (opts.historyFile)
addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
return repl;
}
};