Skip to content

Commit

Permalink
Improve error messaging for no active alias. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbleigh committed May 16, 2016
1 parent cbe778e commit 40e4622
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion commands/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ module.exports = new Command('use [alias_or_project_id]')
{
type: 'input',
name: 'alias',
message: 'What alias do you want to use for this project? (e.g. staging)'
message: 'What alias do you want to use for this project? (e.g. staging)',
validate: function(input) {
return input && input.length > 0;
}
}
]).then(function() {
writeAlias(options.projectRoot, options.rc, results.alias, results.project);
Expand Down
23 changes: 20 additions & 3 deletions lib/getProjectId.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

var _ = require('lodash');
var chalk = require('chalk');

var FirebaseError = require('./error');

/**
Expand All @@ -13,9 +16,23 @@ var FirebaseError = require('./error');
*/
module.exports = function(options, allowNull) {
if (!options.project && !allowNull) {
throw new FirebaseError('No project specified. Run with --project or inside project directory', {
exit: 1
});
var aliases = _.get(options, 'rc.projects', {});
var aliasCount = _.size(aliases);

if (aliasCount === 0) {
throw new FirebaseError('No project active. Run with ' + chalk.bold('--project <projectId>') + ' or define an alias by\nrunning ' + chalk.bold('firebase use --add'), {
exit: 1
});
} else if (aliasCount === 1) {
var name = _.head(_.keys(aliases));
throw new FirebaseError('No project active, but a project alias is available.\n\nRun ' + chalk.bold('firebase use ' + name) + ' to activate project ' + chalk.bold(aliases[name]));
} else {
var aliasList = _.map(aliases, function(projectId, aname) {
return ' ' + aname + ' (' + projectId + ')';
}).join('\n');

throw new FirebaseError('No project active, but project aliases are available.\n\nRun ' + chalk.bold('firebase use <alias>') + ' with one of these options:\n\n' + aliasList);
}
}
return options.project;
};

0 comments on commit 40e4622

Please sign in to comment.