Skip to content

Commit

Permalink
Convert cloud9 to coffeescript
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jul 26, 2011
1 parent 53829cf commit 54574ee
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 115 deletions.
98 changes: 98 additions & 0 deletions lib/plugins/cloud9.coffee
@@ -0,0 +1,98 @@

fs = require('fs')
path = require('path')
exec = require('child_process').exec

module.exports = (settings) ->
# Validation
throw new Error 'No shell provided' if not settings.shell
shell = settings.shell
# Default settings
settings.workspace ?= shell.project_dir
throw new Error('No workspace provided') if not settings.workspace
settings.stdout ?= '/dev/null'
settings.stderr ?= '/dev/null'
# Register commands
cloud9 = null
shell.on 'exit', ->
cloud9.kill() if shell.isShell and not settings.detach and cloud9
shell.cmd 'cloud9 start', 'Start Cloud9', (req, res, next) ->
args = [];
detached = not shell.isShell or settings.detach
args.push '-w'
args.push settings.workspace
if settings.config
args.push '-c'
args.push settings.config
if settings.group
args.push '-g'
args.push settings.group
if settings.user
args.push '-u'
args.push settings.user
if settings.action
args.push '-a'
args.push settings.action
if settings.ip
args.push '-l'
args.push settings.ip
if settings.port
args.push '-p'
args.push settings.port
if detached
args.push '>'
args.push settings.stdout or '/dev/null'
if detached
args.push '2>'
args.push settings.stderr or '/dev/null'
args.unshift 'cloud9'
args = args.join ' '
cloud9 = exec args
if detached
pidfile = settings.pidfile or '/tmp/cloud9.pid'
fs.writeFileSync pidfile, '' + cloud9.pid
else
cloud9.stdout.pipe(
if typeof settings.stdout is 'string'
then fs.createWriteStream settings.stdout
else settings.stdout
)
cloud9.stderr.pipe(
if typeof settings.stderr is 'string'
then fs.createWriteStream settings.stderr
else settings.stderr
)
# Give a chance to cloud9 to startup
# and open a browser window in command mode
setTimeout ->
if cloud9
ip = settings.ip or '127.0.0.1'
port = settings.port or 3000
message = "Cloud9 started http://#{ ip }:#{ port }"
res.cyan( message ).ln()
res.prompt()
, 500
shell.cmd 'cloud9 stop', 'Stop Cloud9', (req, res, next) ->
if not shell.isShell or settings.detach or not cloud9
pidfile = settings.pidfile or '/tmp/cloud9.pid'
pid = fs.readFileSync pidfile
cmds = []
cmds.push "for i in `ps -ef| awk '$3 == '#{ pid }' { print $2 }'` ; do kill $i ; done"
cmds.push "kill #{ pid }"
cloud9 = exec cmds.join(' && ')
cloud9.on 'exit', (code) ->
if code is 0
then res.cyan('Cloud9 successfully stoped').ln()
else res.red('Error while stoping Cloud9').ln()
fs.unlinkSync pidfile
res.prompt()
else if cloud9
cloud9.on 'exit', (code) ->
cloud9 = null
res.cyan('Cloud9 successfully stopped').ln()
res.prompt()
cloud9.kill()
else
console.log 'this should not appear'
res.prompt()

235 changes: 120 additions & 115 deletions lib/plugins/cloud9.js
@@ -1,119 +1,124 @@

var fs = require('fs'),
path = require('path'),
exec = require('child_process').exec;

module.exports = function(settings){
// Validation
if(!settings.shell){
throw new Error('No shell provided');
var exec, fs, path;
fs = require('fs');
path = require('path');
exec = require('child_process').exec;
module.exports = function(settings) {
var cloud9, shell, _ref, _ref2, _ref3;
if (!settings.shell) {
throw new Error('No shell provided');
}
shell = settings.shell;
if ((_ref = settings.workspace) != null) {
_ref;
} else {
settings.workspace = shell.project_dir;
};
if (!settings.workspace) {
throw new Error('No workspace provided');
}
if ((_ref2 = settings.stdout) != null) {
_ref2;
} else {
settings.stdout = '/dev/null';
};
if ((_ref3 = settings.stderr) != null) {
_ref3;
} else {
settings.stderr = '/dev/null';
};
cloud9 = null;
shell.on('exit', function() {
if (shell.isShell && !settings.detach && cloud9) {
return cloud9.kill();
}
var shell = settings.shell;
// Default settings
settings.workspace = settings.workspace || shell.project_dir;
if(!settings.workspace){
throw new Error('No workspace provided');
});
shell.cmd('cloud9 start', 'Start Cloud9', function(req, res, next) {
var args, detached, pidfile;
args = [];
detached = !shell.isShell || settings.detach;
args.push('-w');
args.push(settings.workspace);
if (settings.config) {
args.push('-c');
args.push(settings.config);
}
// Register commands
var cloud9;
shell.on('exit', function() {
if (shell.isShell && !settings.detach && cloud9) {
cloud9.kill();
}
});
shell.cmd('cloud9 start', 'Start Cloud9', function(req, res, next) {
var args = [];
var detached = !shell.isShell || settings.detach;
var pipeStdout = settings.stdout && !detached;
var pipeStderr = settings.stderr && !detached;
args.push('-w');
args.push(settings.workspace);
if(settings.config){
args.push('-c');
args.push(settings.config);
}
if(settings.group){
args.push('-g');
args.push(settings.group);
}
if(settings.user){
args.push('-u');
args.push(settings.user);
}
if(settings.action){
args.push('-a');
args.push(settings.action);
}
if(settings.ip){
args.push('-l');
args.push(settings.ip);
}
if(settings.port){
args.push('-p');
args.push(settings.port);
}
if(!pipeStdout){
args.push('>');
args.push(settings.stdout);
}
if(!pipeStderr){
args.push('2>');
args.push(settings.stderr);
}
args.unshift('cloud9');
cloud9 = exec(args.join(' '));
if(pipeStdout){
cloud9.stdout.pipe(
typeof settings.stdout === 'string'
? fs.createWriteStream(settings.stdout)
: settings.stdout
);
}
if(pipeStderr){
cloud9.stderr.pipe(
typeof settings.stderr === 'string'
? fs.createWriteStream(settings.stderr)
: settings.stderr
);
}
if(detached){
var pidfile = settings.pidfile || '/tmp/cloud9.pid';
fs.writeFileSync(pidfile,''+cloud9.pid);
}
// Give a chance to cloud9 to startup
// and open a browser window in command mode
setTimeout(function(){
if(cloud9){
res.cyan('Cloud9 started').ln();
}
res.prompt();
},500);
});
shell.cmd('cloud9 stop', 'Stop Cloud9', function(req, res, next) {
if(!shell.isShell || settings.detach || !cloud9){
var pidfile = settings.pidfile || '/tmp/cloud9.pid';
var pid = fs.readFileSync(pidfile);
var cmds = [];
cmds.push('for i in `ps -ef| awk \'$3 == \''+pid+'\' { print $2 }\'` ; do kill $i ; done');
cmds.push('kill '+pid);
return exec(cmds.join(' && '))
.on('exit', function(code){
code === 0
? res.cyan('Cloud9 successfully stoped')
: res.red('Error while stoping Cloud9');
fs.unlinkSync(pidfile);
res.prompt();
});
}else if (cloud9) {
cloud9.on('exit', function(code) {
cloud9 = null;
res.cyan('Cloud9 successfully stopped');
res.prompt();
});
cloud9.kill();
if (settings.group) {
args.push('-g');
args.push(settings.group);
}
if (settings.user) {
args.push('-u');
args.push(settings.user);
}
if (settings.action) {
args.push('-a');
args.push(settings.action);
}
if (settings.ip) {
args.push('-l');
args.push(settings.ip);
}
if (settings.port) {
args.push('-p');
args.push(settings.port);
}
if (detached) {
args.push('>');
args.push(settings.stdout || '/dev/null');
}
if (detached) {
args.push('2>');
args.push(settings.stderr || '/dev/null');
}
args.unshift('cloud9');
args = args.join(' ');
cloud9 = exec(args);
if (detached) {
pidfile = settings.pidfile || '/tmp/cloud9.pid';
fs.writeFileSync(pidfile, '' + cloud9.pid);
} else {
cloud9.stdout.pipe(typeof settings.stdout === 'string' ? fs.createWriteStream(settings.stdout) : settings.stdout);
cloud9.stderr.pipe(typeof settings.stderr === 'string' ? fs.createWriteStream(settings.stderr) : settings.stderr);
}
return setTimeout(function() {
var ip, message, port;
if (cloud9) {
ip = settings.ip || '127.0.0.1';
port = settings.port || 3000;
message = "Cloud9 started http://" + ip + ":" + port;
res.cyan(message).ln();
}
return res.prompt();
}, 500);
});
return shell.cmd('cloud9 stop', 'Stop Cloud9', function(req, res, next) {
var cmds, pid, pidfile;
if (!shell.isShell || settings.detach || !cloud9) {
pidfile = settings.pidfile || '/tmp/cloud9.pid';
pid = fs.readFileSync(pidfile);
cmds = [];
cmds.push("for i in `ps -ef| awk '$3 == '" + pid + "' { print $2 }'` ; do kill $i ; done");
cmds.push("kill " + pid);
cloud9 = exec(cmds.join(' && '));
return cloud9.on('exit', function(code) {
if (code === 0) {
res.cyan('Cloud9 successfully stoped').ln();
} else {
res.prompt();
res.red('Error while stoping Cloud9').ln();
}
});
}

fs.unlinkSync(pidfile);
return res.prompt();
});
} else if (cloud9) {
cloud9.on('exit', function(code) {
cloud9 = null;
res.cyan('Cloud9 successfully stopped').ln();
return res.prompt();
});
return cloud9.kill();
} else {
console.log('this should not appear');
return res.prompt();
}
});
};

0 comments on commit 54574ee

Please sign in to comment.