pp-parachute
Version:
Airdrop JS Applications
116 lines (99 loc) • 4.18 kB
JavaScript
//chute //same as chute deploy
//chute pack appName //register a parachute.js file aliased to name
//chute deploy //deploy all
//chute watch appName env //watch appName with environment
//chute build appName env //build appName with environment
var path = require('path');
var config = require('../shared/config');
var manager = require('../shared/manager');
var explode = require('../shared/explosives');
var Errors = require('../shared/errors');
explode.setCharge(exit);
var args = process.argv;
var command = (args[2] || '').trim();
var arg0 = (args[3] || '').trim();
var arg1 = (args[4] || '').trim();
var appPath = path.resolve(arg1);
var commands = [
'deploy', 'serve', 'watch', 'build', 'add', 'remove', 'rename', 'list', 'version'
];
process.on('SIGINT', function() {
console.log('Now closing. Catch you later!');
process.exit();
});
switch (command.toLowerCase()) {
case '':
case 'deploy':
case 'serve': //here ya go jacob
console.log('Pulling the rip cord!');
return deploy();
case 'watch':
//todo i would prefer to have watch defer to server watch if server is running but unsure how to forward logs
//to just the terminal that invoked the initial watch
if(arg0 === '') return explode(Errors.CmdLineMissingApplicationName, 'build');
if(arg1 === '') return explode(Errors.CmdLineMissingEnvironmentName, 'build');
console.log('Watching', arg0, arg1);
return manager.watchApplication(arg0, arg1);
case 'build':
if(arg0 === '') return explode(Errors.CmdLineMissingApplicationName, 'build');
if(arg1 === '') return explode(Errors.CmdLineMissingEnvironmentName, 'build');
console.log('Building', arg0, arg1);
return manager.buildApplication(arg0, arg1);
case 'add':
appPath = path.resolve(arg0);
//if path doesnt end in /parachute.js add it
if(appPath.lastIndexOf('/parachute.js') !== appPath.length - '/parachute.js'.length) {
appPath += '/parachute.js';
}
return tryRemote('addApplication', {
appPath: appPath
}, function () {
return manager.addApplication(appPath);
}, 'Added ' + require(appPath).name + ' to parachute.');
case 'remove':
return tryRemote('removeApplication', {appName: arg0}, function () {
return manager.removeApplication(arg0);
}, 'Application ' + arg0 + ' was removed from parachute. Dont worry no files were deleted.');
case 'list':
return manager.listApplications();
case 'rename':
//todo assert names valid && not equal
if(arg0 === '') return explode(Errors.CmdLineMissingApplicationName, 'rename');
if(arg1 === '') return explode(Errors.CmdLineMissingRenameName, 'build');
return tryRemote('renameApplication', {appName: arg0, newName: arg1}, function() {
manager.renameApplication(arg0, arg1);
}, 'All set, remember to change the application name in parachute.js file as well.');
case 'version':
console.log(require('../package.json').version);
break;
default:
explode(Errors.CmdLineInvalidCommand, command, commands);
}
function tryRemote(action, payload, remoteFailedCallback, message) {
var http = require('request');
http.post(config.getBaseUrl() + '__reserved/' + action, payload, function (error, response, body) {
if (error && error.code === 'ECONNREFUSED') {
remoteFailedCallback();
if(message) console.log(message);
} else if (error) {
return explode(error);
} else {
if(message) console.log(message);
}
});
}
function deploy() {
var http = require('request');
http.get(config.getBaseUrl() + '/__reserved/ping', function (error, response, body) {
if (error && error.code === 'ECONNREFUSED') {
require('../server/server');
} else if (error) {
return explode(error);
}
});
}
function exit(error) {
console.log(error);
process.exit();
}