Skip to content

Commit 40e4622

Browse files
committed
Improve error messaging for no active alias. (#79)
1 parent cbe778e commit 40e4622

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

commands/use.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ module.exports = new Command('use [alias_or_project_id]')
109109
{
110110
type: 'input',
111111
name: 'alias',
112-
message: 'What alias do you want to use for this project? (e.g. staging)'
112+
message: 'What alias do you want to use for this project? (e.g. staging)',
113+
validate: function(input) {
114+
return input && input.length > 0;
115+
}
113116
}
114117
]).then(function() {
115118
writeAlias(options.projectRoot, options.rc, results.alias, results.project);

lib/getProjectId.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
var _ = require('lodash');
4+
var chalk = require('chalk');
5+
36
var FirebaseError = require('./error');
47

58
/**
@@ -13,9 +16,23 @@ var FirebaseError = require('./error');
1316
*/
1417
module.exports = function(options, allowNull) {
1518
if (!options.project && !allowNull) {
16-
throw new FirebaseError('No project specified. Run with --project or inside project directory', {
17-
exit: 1
18-
});
19+
var aliases = _.get(options, 'rc.projects', {});
20+
var aliasCount = _.size(aliases);
21+
22+
if (aliasCount === 0) {
23+
throw new FirebaseError('No project active. Run with ' + chalk.bold('--project <projectId>') + ' or define an alias by\nrunning ' + chalk.bold('firebase use --add'), {
24+
exit: 1
25+
});
26+
} else if (aliasCount === 1) {
27+
var name = _.head(_.keys(aliases));
28+
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]));
29+
} else {
30+
var aliasList = _.map(aliases, function(projectId, aname) {
31+
return ' ' + aname + ' (' + projectId + ')';
32+
}).join('\n');
33+
34+
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);
35+
}
1936
}
2037
return options.project;
2138
};

0 commit comments

Comments
 (0)