Permalink
Browse files

[fix][api] better resync closes #86

  • Loading branch information...
blakmatrix committed Feb 3, 2013
1 parent c2c587e commit 32d567d2cef9c1e1708adf2b1bec10b9063e5c44
Showing with 54 additions and 26 deletions.
  1. +54 −26 lib/jitsu/package.js
@@ -604,50 +604,78 @@ package.properties = function (dir) {
// Prompts for appname and subdomain until the combination is available
//
package.available = function (pkg, dir, callback, createPackage) {

jitsu.apps.available(pkg, function (err, isAvailable) {

var props, fields = [];
if (err) {
jitsu.log.error('There was an error while checking app name / subdomain availability.');
return callback(err);
function removeAppname(){
delete pkg.name;
fields.push('name');
}

if (!isAvailable.available) {
if (isAvailable.appname === false) {
delete pkg.name;
fields.push('name');
}
if (isAvailable.subdomain === false) {
delete pkg.subdomain;
fields.push('subdomain');
}
function removeSubdomain(){
delete pkg.subdomain;
fields.push('subdomain');
}
function addProps(){
props = package.properties(dir).filter(function (p) {
return fields.indexOf(p.name) !== -1;
});

//
// auto-suggest a new domain field based on username
//
for (var p in props) {
if(props[p].name === 'subdomain') {
props[p].default = props[p].default + '.' + jitsu.config.get('username');
}
}

if (fields.indexOf('name') !== -1) {
jitsu.prompt.addProperties(pkg, props, createPackage);
return;
}
if (err) {
jitsu.log.error('There was an error while checking app name / subdomain availability.');
return callback(err);
} else if (!isAvailable.available) {
// only appname is taken
if(!isAvailable.appname && isAvailable.subdomain){
jitsu.log.error('The app name requested is already in use');
jitsu.prompt.confirm('It appears you have already used this appname before ('+pkg.name.magenta+'). ' + 'Overwrite?'.green.bold, { default: 'yes'}, function (err, result) {
if (err) {
return callback(err);
}
if (!result){
removeAppname();
addProps();
} else {
callback(null, pkg);
}

});
}
if (fields.indexOf('subdomain') !== -1) {
// only subdomain is taken
else if(isAvailable.appname && !isAvailable.subdomain){
jitsu.log.error('The subdomain requested is already in use');
removeSubdomain();
addProps();
}
jitsu.prompt.addProperties(pkg, props, createPackage);
return;
//both are taken
else if(!isAvailable.appname && !isAvailable.subdomain){
jitsu.log.error('The subdomain and app name requested are already in use');
jitsu.prompt.confirm('This app already exists! ('+pkg.name.magenta+'). ' + 'Do you want to deploy over it?'.green.bold, { default: 'yes'}, function (err, result) {
if (err) {
return callback(err);
}
if (!result){
removeAppname();
removeSubdomain();
addProps();
} else {
callback(null, pkg);
}

});

}
} else { //nothing is wrong
callback(null, pkg);
}

callback(null, pkg);
});
}
};

package.runScript = function (pkg, action, callback) {
var command = pkg.scripts[action];

0 comments on commit 32d567d

Please sign in to comment.