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.

checkProjectBilling.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.enableBilling = void 0;
  4. const clc = require("colorette");
  5. const opn = require("open");
  6. const cloudbilling = require("../gcp/cloudbilling");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const extensionsHelper_1 = require("./extensionsHelper");
  10. const prompt = require("../prompt");
  11. const utils = require("../utils");
  12. const ADD_BILLING_ACCOUNT = "Add new billing account";
  13. function logBillingStatus(enabled, projectId) {
  14. if (!enabled) {
  15. throw new error_1.FirebaseError(`${extensionsHelper_1.logPrefix}: ${clc.bold(projectId)} could not be upgraded. Please add a billing account via the Firebase console before proceeding.`);
  16. }
  17. utils.logLabeledSuccess(extensionsHelper_1.logPrefix, `${clc.bold(projectId)} has successfully been upgraded.`);
  18. }
  19. async function openBillingAccount(projectId, url, open) {
  20. if (open) {
  21. try {
  22. opn(url);
  23. }
  24. catch (err) {
  25. logger_1.logger.debug("Unable to open billing URL: " + err.stack);
  26. }
  27. }
  28. await prompt.promptOnce({
  29. name: "continue",
  30. type: "confirm",
  31. message: "Press enter when finished upgrading your project to continue setting up your extension.",
  32. default: true,
  33. });
  34. return cloudbilling.checkBillingEnabled(projectId);
  35. }
  36. async function chooseBillingAccount(projectId, accounts) {
  37. const choices = accounts.map((m) => m.displayName);
  38. choices.push(ADD_BILLING_ACCOUNT);
  39. const answer = await prompt.promptOnce({
  40. name: "billing",
  41. type: "list",
  42. message: `Extensions require your project to be upgraded to the Blaze plan. You have access to the following billing accounts.
  43. Please select the one that you would like to associate with this project:`,
  44. choices: choices,
  45. });
  46. let billingEnabled;
  47. if (answer === ADD_BILLING_ACCOUNT) {
  48. const billingURL = `https://console.cloud.google.com/billing/linkedaccount?project=${projectId}`;
  49. billingEnabled = await openBillingAccount(projectId, billingURL, true);
  50. }
  51. else {
  52. const billingAccount = accounts.find((a) => a.displayName === answer);
  53. billingEnabled = await cloudbilling.setBillingAccount(projectId, billingAccount.name);
  54. }
  55. return logBillingStatus(billingEnabled, projectId);
  56. }
  57. async function setUpBillingAccount(projectId) {
  58. const billingURL = `https://console.cloud.google.com/billing/linkedaccount?project=${projectId}`;
  59. logger_1.logger.info();
  60. logger_1.logger.info(`Extension require your project to be upgraded to the Blaze plan. Please visit the following link to add a billing account:`);
  61. logger_1.logger.info();
  62. logger_1.logger.info(clc.bold(clc.underline(billingURL)));
  63. logger_1.logger.info();
  64. const open = await prompt.promptOnce({
  65. name: "open-url",
  66. type: "confirm",
  67. message: "Press enter to open the URL.",
  68. default: true,
  69. });
  70. const billingEnabled = await openBillingAccount(projectId, billingURL, open);
  71. return logBillingStatus(billingEnabled, projectId);
  72. }
  73. async function enableBilling(projectId) {
  74. const billingAccounts = await cloudbilling.listBillingAccounts();
  75. if (billingAccounts) {
  76. const accounts = billingAccounts.filter((account) => account.open);
  77. return accounts.length > 0
  78. ? chooseBillingAccount(projectId, accounts)
  79. : setUpBillingAccount(projectId);
  80. }
  81. }
  82. exports.enableBilling = enableBilling;