pokemon-showdown/repl.js
Guangcong Luo 84897f7142 Disable REPL by default
The REPL feature has been responsible for tons of crashes, so I'm
disabling it until these crashes can be fixed.

I'm also disabling it until it actually gets documented in
`logs/repl/README.md`, because what's there right now is not
enough documentation to actually figure out how to use it.
2015-08-01 02:55:05 -04:00

48 lines
1.3 KiB
JavaScript

const REPL_ENABLED = false;
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 (!REPL_ENABLED) return;
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));
};