forked from CreaturePhil/Showdown-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
44 lines (40 loc) · 1.29 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
var fs = require('fs');
var path = require('path');
var clearedPrefixes = {};
// The eval function is passed in because there is no other way to access a file's non-global context
exports.start = function (prefix, suffix, evalFunction) {
if (process.platform === 'win32') return; // Windows doesn't support sockets mounted in the filesystem
prefix = path.resolve(__dirname, Config.replsocketprefix || 'logs/repl', prefix);
if (!evalFunction) {
evalFunction = suffix;
suffix = "";
}
var name = prefix + suffix;
if (!clearedPrefixes[prefix]) {
// Clear out any old sockets
var directory = path.dirname(prefix);
var basename = path.basename(prefix);
fs.readdirSync(directory).forEach(function (file) {
if (file.indexOf(basename) !== 0) return;
try {
fs.unlinkSync(directory + '/' + file);
} catch (e) {
require('./crashlogger.js')(e, 'REPL: ' + prefix);
}
});
clearedPrefixes[prefix] = true;
}
require('net').createServer(function (socket) {
require('repl').start({
input: socket,
output: socket,
eval: function (cmd, context, filename, callback) {
try {
callback(null, evalFunction(cmd));
} catch (e) {
callback(e);
}
}
}).on('exit', socket.end.bind(socket));
}).listen(name, fs.chmodSync.bind(fs, name, Config.replsocketmode || 0600));
};