Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
// Generated by CoffeeScript 1.12.6
2
(function() {
3
var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, ref, replDefaults, runInContext, updateSyntaxError, vm;
4
5
fs = require('fs');
6
7
path = require('path');
8
9
vm = require('vm');
10
11
nodeREPL = require('repl');
12
13
CoffeeScript = require('./coffee-script');
14
15
ref = require('./helpers'), merge = ref.merge, updateSyntaxError = ref.updateSyntaxError;
16
17
replDefaults = {
18
prompt: 'coffee> ',
19
historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
20
historyMaxInputSize: 10240,
21
"eval": function(input, context, filename, cb) {
22
var Assign, Block, Literal, Value, ast, err, js, ref1, referencedVars, token, tokens;
23
input = input.replace(/\uFF00/g, '\n');
24
input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
25
input = input.replace(/^\s*try\s*{([\s\S]*)}\s*catch.*$/m, '$1');
26
ref1 = require('./nodes'), Block = ref1.Block, Assign = ref1.Assign, Value = ref1.Value, Literal = ref1.Literal;
27
try {
28
tokens = CoffeeScript.tokens(input);
29
referencedVars = (function() {
30
var i, len, results;
31
results = [];
32
for (i = 0, len = tokens.length; i < len; i++) {
33
token = tokens[i];
34
if (token[0] === 'IDENTIFIER') {
35
results.push(token[1]);
36
}
37
}
38
return results;
39
})();
40
ast = CoffeeScript.nodes(tokens);
41
ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
42
js = ast.compile({
43
bare: true,
44
locals: Object.keys(context),
45
referencedVars: referencedVars
46
});
47
return cb(null, runInContext(js, context, filename));
48
} catch (error) {
49
err = error;
50
updateSyntaxError(err, input);
51
return cb(err);
52
}
53
}
54
};
55
56
runInContext = function(js, context, filename) {
57
if (context === global) {
58
return vm.runInThisContext(js, filename);
59
} else {
60
return vm.runInContext(js, context, filename);
61
}
62
};
63
64
addMultilineHandler = function(repl) {
65
var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref1, rli;
66
rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
67
origPrompt = (ref1 = repl._prompt) != null ? ref1 : repl.prompt;
68
multiline = {
69
enabled: false,
70
initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
71
return x.replace(/./g, '-');
72
}),
73
prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
74
return x.replace(/./g, '.');
75
}),
76
buffer: ''
77
};
78
nodeLineListener = rli.listeners('line')[0];
79
rli.removeListener('line', nodeLineListener);
80
rli.on('line', function(cmd) {
81
if (multiline.enabled) {
82
multiline.buffer += cmd + "\n";
83
rli.setPrompt(multiline.prompt);
84
rli.prompt(true);
85
} else {
86
rli.setPrompt(origPrompt);
87
nodeLineListener(cmd);
88
}
89
});
90
return inputStream.on('keypress', function(char, key) {
91
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
92
return;
93
}
94
if (multiline.enabled) {
95
if (!multiline.buffer.match(/\n/)) {
96
multiline.enabled = !multiline.enabled;
97
rli.setPrompt(origPrompt);
98
rli.prompt(true);
99
return;
100
}
101
if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
102
return;
103
}
104
multiline.enabled = !multiline.enabled;
105
rli.line = '';
106
rli.cursor = 0;
107
rli.output.cursorTo(0);
108
rli.output.clearLine(1);
109
multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
110
rli.emit('line', multiline.buffer);
111
multiline.buffer = '';
112
} else {
113
multiline.enabled = !multiline.enabled;
114
rli.setPrompt(multiline.initialPrompt);
115
rli.prompt(true);
116
}
117
});
118
};
119
120
addHistory = function(repl, filename, maxSize) {
121
var buffer, fd, lastLine, readFd, size, stat;
122
lastLine = null;
123
try {
124
stat = fs.statSync(filename);
125
size = Math.min(maxSize, stat.size);
126
readFd = fs.openSync(filename, 'r');
127
buffer = new Buffer(size);
128
fs.readSync(readFd, buffer, 0, size, stat.size - size);
129
fs.closeSync(readFd);
130
repl.rli.history = buffer.toString().split('\n').reverse();
131
if (stat.size > maxSize) {
132
repl.rli.history.pop();
133
}
134
if (repl.rli.history[0] === '') {
135
repl.rli.history.shift();
136
}
137
repl.rli.historyIndex = -1;
138
lastLine = repl.rli.history[0];
139
} catch (error) {}
140
fd = fs.openSync(filename, 'a');
141
repl.rli.addListener('line', function(code) {
142
if (code && code.length && code !== '.history' && code !== '.exit' && lastLine !== code) {
143
fs.writeSync(fd, code + "\n");
144
return lastLine = code;
145
}
146
});
147
repl.on('exit', function() {
148
return fs.closeSync(fd);
149
});
150
return repl.commands[getCommandId(repl, 'history')] = {
151
help: 'Show command history',
152
action: function() {
153
repl.outputStream.write((repl.rli.history.slice(0).reverse().join('\n')) + "\n");
154
return repl.displayPrompt();
155
}
156
};
157
};
158
159
getCommandId = function(repl, commandName) {
160
var commandsHaveLeadingDot;
161
commandsHaveLeadingDot = repl.commands['.help'] != null;
162
if (commandsHaveLeadingDot) {
163
return "." + commandName;
164
} else {
165
return commandName;
166
}
167
};
168
169
module.exports = {
170
start: function(opts) {
171
var build, major, minor, ref1, repl;
172
if (opts == null) {
173
opts = {};
174
}
175
ref1 = process.versions.node.split('.').map(function(n) {
176
return parseInt(n, 10);
177
}), major = ref1[0], minor = ref1[1], build = ref1[2];
178
if (major === 0 && minor < 8) {
179
console.warn("Node 0.8.0+ required for CoffeeScript REPL");
180
process.exit(1);
181
}
182
CoffeeScript.register();
183
process.argv = ['coffee'].concat(process.argv.slice(2));
184
opts = merge(replDefaults, opts);
185
repl = nodeREPL.start(opts);
186
if (opts.prelude) {
187
runInContext(opts.prelude, repl.context, 'prelude');
188
}
189
repl.on('exit', function() {
190
if (!repl.rli.closed) {
191
return repl.outputStream.write('\n');
192
}
193
});
194
addMultilineHandler(repl);
195
if (opts.historyFile) {
196
addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
197
}
198
repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';
199
return repl;
200
}
201
};
202
203
}).call(this);
204
205