No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.doSetup = void 0;
  4. const clc = require("colorette");
  5. const _ = require("lodash");
  6. const error_1 = require("../../error");
  7. const projects_1 = require("../../management/projects");
  8. const logger_1 = require("../../logger");
  9. const prompt_1 = require("../../prompt");
  10. const utils = require("../../utils");
  11. const OPTION_NO_PROJECT = "Don't set up a default project";
  12. const OPTION_USE_PROJECT = "Use an existing project";
  13. const OPTION_NEW_PROJECT = "Create a new project";
  14. const OPTION_ADD_FIREBASE = "Add Firebase to an existing Google Cloud Platform project";
  15. function toProjectInfo(projectMetaData) {
  16. const { projectId, displayName, resources } = projectMetaData;
  17. return {
  18. id: projectId,
  19. label: `${projectId}` + (displayName ? ` (${displayName})` : ""),
  20. instance: _.get(resources, "realtimeDatabaseInstance"),
  21. location: _.get(resources, "locationId"),
  22. };
  23. }
  24. async function promptAndCreateNewProject() {
  25. utils.logBullet("If you want to create a project in a Google Cloud organization or folder, please use " +
  26. `"firebase projects:create" instead, and return to this command when you've created the project.`);
  27. const promptAnswer = {};
  28. await (0, prompt_1.prompt)(promptAnswer, projects_1.PROJECTS_CREATE_QUESTIONS);
  29. if (!promptAnswer.projectId) {
  30. throw new error_1.FirebaseError("Project ID cannot be empty");
  31. }
  32. return await (0, projects_1.createFirebaseProjectAndLog)(promptAnswer.projectId, {
  33. displayName: promptAnswer.displayName,
  34. });
  35. }
  36. async function promptAndAddFirebaseToCloudProject() {
  37. const projectId = await (0, projects_1.promptAvailableProjectId)();
  38. if (!projectId) {
  39. throw new error_1.FirebaseError("Project ID cannot be empty");
  40. }
  41. return await (0, projects_1.addFirebaseToCloudProjectAndLog)(projectId);
  42. }
  43. async function projectChoicePrompt(options) {
  44. const choices = [
  45. { name: OPTION_USE_PROJECT, value: OPTION_USE_PROJECT },
  46. { name: OPTION_NEW_PROJECT, value: OPTION_NEW_PROJECT },
  47. { name: OPTION_ADD_FIREBASE, value: OPTION_ADD_FIREBASE },
  48. { name: OPTION_NO_PROJECT, value: OPTION_NO_PROJECT },
  49. ];
  50. const projectSetupOption = await (0, prompt_1.promptOnce)({
  51. type: "list",
  52. name: "id",
  53. message: "Please select an option:",
  54. choices,
  55. });
  56. switch (projectSetupOption) {
  57. case OPTION_USE_PROJECT:
  58. return (0, projects_1.getOrPromptProject)(options);
  59. case OPTION_NEW_PROJECT:
  60. return promptAndCreateNewProject();
  61. case OPTION_ADD_FIREBASE:
  62. return promptAndAddFirebaseToCloudProject();
  63. default:
  64. return;
  65. }
  66. }
  67. async function doSetup(setup, config, options) {
  68. setup.project = {};
  69. logger_1.logger.info();
  70. logger_1.logger.info(`First, let's associate this project directory with a Firebase project.`);
  71. logger_1.logger.info(`You can create multiple project aliases by running ${clc.bold("firebase use --add")}, `);
  72. logger_1.logger.info(`but for now we'll just set up a default project.`);
  73. logger_1.logger.info();
  74. const projectFromRcFile = _.get(setup.rcfile, "projects.default");
  75. if (projectFromRcFile && !options.project) {
  76. utils.logBullet(`.firebaserc already has a default project, using ${projectFromRcFile}.`);
  77. const rcProject = await (0, projects_1.getFirebaseProject)(projectFromRcFile);
  78. setup.projectId = rcProject.projectId;
  79. setup.projectLocation = _.get(rcProject, "resources.locationId");
  80. return;
  81. }
  82. let projectMetaData;
  83. if (options.project) {
  84. logger_1.logger.debug(`Using project from CLI flag: ${options.project}`);
  85. projectMetaData = await (0, projects_1.getFirebaseProject)(options.project);
  86. }
  87. else {
  88. projectMetaData = await projectChoicePrompt(options);
  89. if (!projectMetaData) {
  90. return;
  91. }
  92. }
  93. const projectInfo = toProjectInfo(projectMetaData);
  94. utils.logBullet(`Using project ${projectInfo.label}`);
  95. _.set(setup.rcfile, "projects.default", projectInfo.id);
  96. setup.projectId = projectInfo.id;
  97. setup.instance = projectInfo.instance;
  98. setup.projectLocation = projectInfo.location;
  99. utils.makeActiveProject(config.projectDir, projectInfo.id);
  100. }
  101. exports.doSetup = doSetup;