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.

use.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const clc = require("colorette");
  5. const command_1 = require("../command");
  6. const projects_1 = require("../management/projects");
  7. const logger_1 = require("../logger");
  8. const prompt_1 = require("../prompt");
  9. const requireAuth_1 = require("../requireAuth");
  10. const command_2 = require("../command");
  11. const utils = require("../utils");
  12. function listAliases(options) {
  13. if (options.rc.hasProjects) {
  14. logger_1.logger.info("Project aliases for", clc.bold(options.projectRoot || "") + ":");
  15. logger_1.logger.info();
  16. for (const [alias, projectId] of Object.entries(options.rc.projects)) {
  17. const listing = alias + " (" + projectId + ")";
  18. if (options.project === projectId || options.projectAlias === alias) {
  19. logger_1.logger.info(clc.cyan(clc.bold("* " + listing)));
  20. }
  21. else {
  22. logger_1.logger.info(" " + listing);
  23. }
  24. }
  25. logger_1.logger.info();
  26. }
  27. logger_1.logger.info("Run", clc.bold("firebase use --add"), "to define a new project alias.");
  28. }
  29. function verifyMessage(name) {
  30. return "please verify project " + clc.bold(name) + " exists and you have access.";
  31. }
  32. exports.command = new command_1.Command("use [alias_or_project_id]")
  33. .description("set an active Firebase project for your working directory")
  34. .option("--add", "create a new project alias interactively")
  35. .option("--alias <name>", "create a new alias for the provided project id")
  36. .option("--unalias <name>", "remove an already created project alias")
  37. .option("--clear", "clear the active project selection")
  38. .before(requireAuth_1.requireAuth)
  39. .action((newActive, options) => {
  40. let aliasOpt;
  41. const i = process.argv.indexOf("--alias");
  42. if (i >= 0 && process.argv.length > i + 1) {
  43. aliasOpt = process.argv[i + 1];
  44. }
  45. if (!options.projectRoot) {
  46. return utils.reject(clc.bold("firebase use") +
  47. " must be run from a Firebase project directory.\n\nRun " +
  48. clc.bold("firebase init") +
  49. " to start a project directory in the current folder.");
  50. }
  51. if (newActive) {
  52. let project;
  53. const hasAlias = options.rc.hasProjectAlias(newActive);
  54. const resolvedProject = options.rc.resolveAlias(newActive);
  55. (0, command_2.validateProjectId)(resolvedProject);
  56. return (0, projects_1.getFirebaseProject)(resolvedProject)
  57. .then((foundProject) => {
  58. project = foundProject;
  59. })
  60. .catch(() => {
  61. return utils.reject("Invalid project selection, " + verifyMessage(newActive));
  62. })
  63. .then(() => {
  64. if (aliasOpt) {
  65. if (!project) {
  66. return utils.reject("Cannot create alias " + clc.bold(aliasOpt) + ", " + verifyMessage(newActive));
  67. }
  68. options.rc.addProjectAlias(aliasOpt, newActive);
  69. logger_1.logger.info("Created alias", clc.bold(aliasOpt), "for", resolvedProject + ".");
  70. }
  71. if (hasAlias) {
  72. if (!project) {
  73. return utils.reject("Unable to use alias " + clc.bold(newActive) + ", " + verifyMessage(resolvedProject));
  74. }
  75. utils.makeActiveProject(options.projectRoot, newActive);
  76. logger_1.logger.info("Now using alias", clc.bold(newActive), "(" + resolvedProject + ")");
  77. }
  78. else if (project) {
  79. utils.makeActiveProject(options.projectRoot, newActive);
  80. logger_1.logger.info("Now using project", clc.bold(newActive));
  81. }
  82. else {
  83. return utils.reject("Invalid project selection, " + verifyMessage(newActive));
  84. }
  85. });
  86. }
  87. else if (options.unalias) {
  88. if (options.rc.hasProjectAlias(options.unalias)) {
  89. options.rc.removeProjectAlias(options.unalias);
  90. logger_1.logger.info("Removed alias", clc.bold(options.unalias));
  91. logger_1.logger.info();
  92. listAliases(options);
  93. }
  94. }
  95. else if (options.add) {
  96. if (options.nonInteractive) {
  97. return utils.reject("Cannot run " +
  98. clc.bold("firebase use --add") +
  99. " in non-interactive mode. Use " +
  100. clc.bold("firebase use <project_id> --alias <alias>") +
  101. " instead.");
  102. }
  103. return (0, projects_1.listFirebaseProjects)().then((projects) => {
  104. const results = {};
  105. return (0, prompt_1.prompt)(results, [
  106. {
  107. type: "list",
  108. name: "project",
  109. message: "Which project do you want to add?",
  110. choices: projects.map((p) => p.projectId).sort(),
  111. },
  112. {
  113. type: "input",
  114. name: "alias",
  115. message: "What alias do you want to use for this project? (e.g. staging)",
  116. validate: (input) => {
  117. return input && input.length > 0;
  118. },
  119. },
  120. ]).then(() => {
  121. options.rc.addProjectAlias(results.alias, results.project);
  122. utils.makeActiveProject(options.projectRoot, results.alias);
  123. logger_1.logger.info();
  124. logger_1.logger.info("Created alias", clc.bold(results.alias || ""), "for", results.project + ".");
  125. logger_1.logger.info("Now using alias", clc.bold(results.alias || "") + " (" + results.project + ")");
  126. });
  127. });
  128. }
  129. else if (options.clear) {
  130. utils.makeActiveProject(options.projectRoot, undefined);
  131. options.projectAlias = null;
  132. options.project = null;
  133. logger_1.logger.info("Cleared active project.");
  134. logger_1.logger.info();
  135. listAliases(options);
  136. }
  137. else {
  138. if (options.nonInteractive || !process.stdout.isTTY) {
  139. if (options.project) {
  140. logger_1.logger.info(options.project);
  141. return options.project;
  142. }
  143. return utils.reject("No active project");
  144. }
  145. if (options.projectAlias) {
  146. logger_1.logger.info("Active Project:", clc.bold(clc.cyan(options.projectAlias + " (" + options.project + ")")));
  147. }
  148. else if (options.project) {
  149. logger_1.logger.info("Active Project:", clc.bold(clc.cyan(options.project)));
  150. }
  151. else {
  152. let msg = "No project is currently active";
  153. if (options.rc.hasProjects) {
  154. msg += ", and no aliases have been created.";
  155. }
  156. logger_1.logger.info(msg + ".");
  157. }
  158. logger_1.logger.info();
  159. listAliases(options);
  160. return options.project;
  161. }
  162. });