GSI - Employe Self Service Mobile
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.

97 lines
4.9 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.askForEventArcLocation = exports.askShouldCollectEventsConfig = exports.askForAllowedEventTypes = exports.askForEventsConfig = exports.checkAllowedEventTypesResponse = void 0;
  4. const prompt_1 = require("../prompt");
  5. const extensionsApi = require("../extensions/extensionsApi");
  6. const utils = require("../utils");
  7. const clc = require("colorette");
  8. const logger_1 = require("../logger");
  9. const { marked } = require("marked");
  10. function checkAllowedEventTypesResponse(response, validEvents) {
  11. const validEventTypes = validEvents.map((e) => e.type);
  12. if (response.length === 0) {
  13. return false;
  14. }
  15. for (const e of response) {
  16. if (!validEventTypes.includes(e)) {
  17. utils.logWarning(`Unexpected event type '${e}' was configured to be emitted. This event type is not part of the extension spec.`);
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. exports.checkAllowedEventTypesResponse = checkAllowedEventTypesResponse;
  24. async function askForEventsConfig(events, projectId, instanceId) {
  25. var _a, _b;
  26. logger_1.logger.info(`\n${clc.bold("Enable Events")}: ${marked("If you enable events, you can write custom event handlers ([https://firebase.google.com/docs/extensions/install-extensions#eventarc](https://firebase.google.com/docs/extensions/install-extensions#eventarc)) that respond to these events.\n\nYou can always enable or disable events later. Events will be emitted via Eventarc. Fees apply ([https://cloud.google.com/eventarc/pricing](https://cloud.google.com/eventarc/pricing)).")}`);
  27. if (!(await askShouldCollectEventsConfig())) {
  28. return undefined;
  29. }
  30. let existingInstance;
  31. try {
  32. existingInstance = instanceId
  33. ? await extensionsApi.getInstance(projectId, instanceId)
  34. : undefined;
  35. }
  36. catch (_c) {
  37. }
  38. const preselectedTypes = (_a = existingInstance === null || existingInstance === void 0 ? void 0 : existingInstance.config.allowedEventTypes) !== null && _a !== void 0 ? _a : [];
  39. const oldLocation = (_b = existingInstance === null || existingInstance === void 0 ? void 0 : existingInstance.config.eventarcChannel) === null || _b === void 0 ? void 0 : _b.split("/")[3];
  40. const location = await askForEventArcLocation(oldLocation);
  41. const channel = `projects/${projectId}/locations/${location}/channels/firebase`;
  42. const allowedEventTypes = await askForAllowedEventTypes(events, preselectedTypes);
  43. return { channel, allowedEventTypes };
  44. }
  45. exports.askForEventsConfig = askForEventsConfig;
  46. async function askForAllowedEventTypes(eventDescriptors, preselectedTypes) {
  47. let valid = false;
  48. let response = [];
  49. const eventTypes = eventDescriptors.map((e, index) => ({
  50. checked: false,
  51. name: `${index + 1}. ${e.type}\n ${e.description}`,
  52. value: e.type,
  53. }));
  54. while (!valid) {
  55. response = await (0, prompt_1.promptOnce)({
  56. name: "selectedEventTypesInput",
  57. type: "checkbox",
  58. default: preselectedTypes !== null && preselectedTypes !== void 0 ? preselectedTypes : [],
  59. message: `Please select the events [${eventTypes.length} types total] that this extension is permitted to emit. ` +
  60. "You can implement your own handlers that trigger when these events are emitted to customize the extension's behavior. ",
  61. choices: eventTypes,
  62. pageSize: 20,
  63. });
  64. valid = checkAllowedEventTypesResponse(response, eventDescriptors);
  65. }
  66. return response.filter((e) => e !== "");
  67. }
  68. exports.askForAllowedEventTypes = askForAllowedEventTypes;
  69. async function askShouldCollectEventsConfig() {
  70. return (0, prompt_1.promptOnce)({
  71. type: "confirm",
  72. name: "shouldCollectEvents",
  73. message: `Would you like to enable events?`,
  74. default: false,
  75. });
  76. }
  77. exports.askShouldCollectEventsConfig = askShouldCollectEventsConfig;
  78. async function askForEventArcLocation(preselectedLocation) {
  79. let valid = false;
  80. const allowedRegions = ["us-central1", "us-west1", "europe-west4", "asia-northeast1"];
  81. let location = "";
  82. while (!valid) {
  83. location = await (0, prompt_1.promptOnce)({
  84. name: "input",
  85. type: "list",
  86. default: preselectedLocation !== null && preselectedLocation !== void 0 ? preselectedLocation : "us-central1",
  87. message: "Which location would you like the Eventarc channel to live in? We recommend using the default option. A channel location that differs from the extension's Cloud Functions location can incur egress cost.",
  88. choices: allowedRegions.map((e) => ({ checked: false, value: e })),
  89. });
  90. valid = allowedRegions.includes(location);
  91. if (!valid) {
  92. utils.logWarning(`Unexpected EventArc region '${location}' was specified. Allowed regions: ${allowedRegions.join(", ")}`);
  93. }
  94. }
  95. return location;
  96. }
  97. exports.askForEventArcLocation = askForEventArcLocation;