-
Notifications
You must be signed in to change notification settings - Fork 906
/
project.js
125 lines (111 loc) · 3.76 KB
/
project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
var clc = require("cli-color");
var _ = require("lodash");
var firebaseApi = require("../../firebaseApi");
var { promptOnce } = require("../../prompt");
var logger = require("../../logger");
var utils = require("../../utils");
var NO_PROJECT = "[don't setup a default project]";
var NEW_PROJECT = "[create a new project]";
/**
* Get the user's desired project, prompting if necessary.
* Returns an object with three fields:
*
* {
* id: project ID [required]
* label: project display label [optional]
* instance: project database instance [optional]
* }
*/
function _getProject(options) {
// The user passed in a --project flag directly, so no need to
// load all projects.
if (options.project) {
return firebaseApi
.getProject(options.project)
.then(function(project) {
var id = project.projectId;
var name = project.displayName;
return {
id: id,
label: id + " (" + name + ")",
instance: _.get(project, "resources.realtimeDatabaseInstance"),
};
})
.catch(function(e) {
return utils.reject("Error getting project " + options.project, { original: e });
});
}
// Load all projects and prompt the user to choose.
return firebaseApi.listProjects().then(function(projects) {
var choices = projects.filter((project) => !!project).map((project) => {
return {
name: project.projectId + " (" + project.displayName + ")",
value: project.projectId,
};
});
choices = _.orderBy(choices, ["name"], ["asc"]);
choices.unshift({ name: NO_PROJECT, value: NO_PROJECT });
choices.push({ name: NEW_PROJECT, value: NEW_PROJECT });
if (choices.length >= 25) {
utils.logBullet(
"Don't want to scroll through all your projects? If you know your project ID, " +
"you can initialize it directly using " +
clc.bold("firebase init --project <project_id>") +
".\n"
);
}
return promptOnce({
type: "list",
name: "id",
message: "Select a default Firebase project for this directory:",
validate: function(answer) {
if (!_.includes(choices, answer)) {
return "Must specify a Firebase to which you have access.";
}
return true;
},
choices: choices,
}).then(function(id) {
if (id === NEW_PROJECT || id === NO_PROJECT) {
return { id: id };
}
const project = projects.find((p) => p.projectId === id);
const label = choices.find((p) => p.value === id).name;
return {
id: id,
label: label,
instance: _.get(project, "resources.realtimeDatabaseInstance"),
};
});
});
}
module.exports = function(setup, config, options) {
setup.project = {};
logger.info();
logger.info("First, let's associate this project directory with a Firebase project.");
logger.info(
"You can create multiple project aliases by running " + clc.bold("firebase use --add") + ", "
);
logger.info("but for now we'll just set up a default project.");
logger.info();
if (_.has(setup.rcfile, "projects.default")) {
utils.logBullet(".firebaserc already has a default project, skipping");
setup.projectId = _.get(setup.rcfile, "projects.default");
return undefined;
}
return _getProject(options).then(function(project) {
if (project.id === NEW_PROJECT) {
setup.createProject = true;
return;
} else if (project.id === NO_PROJECT) {
return;
}
utils.logBullet("Using project " + project.label);
// write "default" alias and activate it immediately
_.set(setup.rcfile, "projects.default", project.id);
setup.projectId = project.id;
setup.instance = project.instance;
utils.makeActiveProject(config.projectDir, project.id);
});
};