Skip to content

Commit

Permalink
Recycle irb process on build step
Browse files Browse the repository at this point in the history
* Combine file save and code load into single socket event
* Emit each stage as buildStatus for UI
* Emit build success or fail depending on step failures
  • Loading branch information
mgarbacz committed Aug 5, 2014
1 parent 9449913 commit 1ebc2a5
Showing 1 changed file with 71 additions and 39 deletions.
110 changes: 71 additions & 39 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,89 @@ exec('IRBRC=\'irb.rc\' irb', function(error, stdout, stderr) {

io.of('/ruby').on('connection', function(socket) {

var ruby = spawn('irb'),
var ruby,
socketOn = false;

ruby.stdout.setEncoding('utf8');
ruby.stderr.setEncoding('utf8');
socket.on('codeBuild', function(data) {
console.log(data);

ruby.stdout.on('data', function(data) {
console.log('stdout: ' + data);
if (socketOn) {
socket.emit('terminalOutput', {
output: data
});
}
});
var fileSaveError,
codeLoadError;

ruby.stderr.on('data', function(data) {
console.log('stderr: ' + data);
socket.emit('terminalError', {
output: data
});
});
// Save file
fs.writeFile('games/game.rb', data.fileContent, function(error) {
fileSaveError = error;

ruby.on('close', function(code) {
console.log('Exit code: ' + code);
});
// Emit file saved
socket.emit('buildStatus', {
output: error || 'Code saved.'
});

socket.on('fileLoad', function(data) {
socketOn = false;
console.log(data);
ruby.stdin.write('exec($0)\n');
setTimeout(function() {
ruby.stdin.write(data.input + '\n');
socket.emit('fileLoaded', {
output: 'Game loaded.'
// Check file syntax?

// Start IRB
ruby = spawn('irb');

ruby.stdout.setEncoding('utf8');
ruby.stderr.setEncoding('utf8');

// Funnel file into IRB
socketOn = false;
ruby.stdin.write(data.fileContent + '\n', function(error) {
codeLoadError = error;

// Emit code loaded
socket.emit('buildStatus', {
output: error || 'Game loaded.'
});

if (!fileSaveError && !codeLoadError) {
socket.emit('buildStatus', {
output: 'Build successful!'
});
} else {
socket.emit('terminalError', {
output: 'Build failed!'
});
}
});
}, 1000);
});

socket.on('terminalInput', function(data) {
socketOn = true;
console.log(data);
ruby.stdin.write(data.input + '\n');
// Bind events on IRB
ruby.stdout.on('data', function(data) {
console.log('stdout: ' + data);
if (socketOn) {
console.log('socket on');
socket.emit('terminalOutput', {
output: data
});
}
});

ruby.stderr.on('data', function(data) {
console.log('stderr: ' + data);
socket.emit('terminalError', {
output: data
});
});

ruby.on('close', function(code) {
console.log('Exit code: ' + code);
});

});
});

socket.on('fileSave', function(data) {
socket.on('terminalInput', function(data) {
console.log(data);
fs.writeFile('games/game.rb', data.fileContent, function(err) {
socket.emit('fileSaved', {
output: err || 'Code saved.'
socketOn = true;
if (ruby) {
ruby.stdin.write(data.input + '\n');
}
else {
socket.emit('terminalError', {
output: 'Game not built!'
});
});
}
});

fs.readFile('games/game.rb', function(err, contents) {
Expand Down

0 comments on commit 1ebc2a5

Please sign in to comment.