暫無描述
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.

projectUtils.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAliases = exports.needProjectNumber = exports.needProjectId = exports.getProjectId = void 0;
  4. const projects_1 = require("./management/projects");
  5. const clc = require("colorette");
  6. const { marked } = require("marked");
  7. const { FirebaseError } = require("./error");
  8. function getProjectId({ projectId, project, }) {
  9. return projectId || project;
  10. }
  11. exports.getProjectId = getProjectId;
  12. function needProjectId({ projectId, project, rc, }) {
  13. if (projectId || project) {
  14. return projectId || project;
  15. }
  16. const aliases = (rc === null || rc === void 0 ? void 0 : rc.projects) || {};
  17. const aliasCount = Object.keys(aliases).length;
  18. if (aliasCount === 0) {
  19. throw new FirebaseError("No currently active project.\n" +
  20. "To run this command, you need to specify a project. You have two options:\n" +
  21. "- Run this command with " +
  22. clc.bold("--project <alias_or_project_id>") +
  23. ".\n" +
  24. "- Set an active project by running " +
  25. clc.bold("firebase use --add") +
  26. ", then rerun this command.\n" +
  27. "To list all the Firebase projects to which you have access, run " +
  28. clc.bold("firebase projects:list") +
  29. ".\n" +
  30. marked("To learn about active projects for the CLI, visit https://firebase.google.com/docs/cli#project_aliases"));
  31. }
  32. const aliasList = Object.entries(aliases)
  33. .map(([aname, projectId]) => ` ${aname} (${projectId})`)
  34. .join("\n");
  35. throw new FirebaseError("No project active, but project aliases are available.\n\nRun " +
  36. clc.bold("firebase use <alias>") +
  37. " with one of these options:\n\n" +
  38. aliasList);
  39. }
  40. exports.needProjectId = needProjectId;
  41. async function needProjectNumber(options) {
  42. if (options.projectNumber) {
  43. return options.projectNumber;
  44. }
  45. const projectId = needProjectId(options);
  46. const metadata = await (0, projects_1.getFirebaseProject)(projectId);
  47. options.projectNumber = metadata.projectNumber;
  48. return options.projectNumber;
  49. }
  50. exports.needProjectNumber = needProjectNumber;
  51. function getAliases(options, projectId) {
  52. if (options.rc.hasProjects) {
  53. return Object.entries(options.rc.projects)
  54. .filter((entry) => entry[1] === projectId)
  55. .map((entry) => entry[0]);
  56. }
  57. return [];
  58. }
  59. exports.getAliases = getAliases;