Няма описание
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.

functionsShellCommandAction.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.actionFunction = void 0;
  4. const clc = require("colorette");
  5. const repl = require("repl");
  6. const _ = require("lodash");
  7. const request = require("request");
  8. const util = require("util");
  9. const functions_1 = require("./serve/functions");
  10. const LocalFunction = require("./localFunction");
  11. const utils = require("./utils");
  12. const logger_1 = require("./logger");
  13. const shell = require("./emulator/functionsEmulatorShell");
  14. const commandUtils = require("./emulator/commandUtils");
  15. const types_1 = require("./emulator/types");
  16. const hubClient_1 = require("./emulator/hubClient");
  17. const portUtils_1 = require("./emulator/portUtils");
  18. const constants_1 = require("./emulator/constants");
  19. const serveFunctions = new functions_1.FunctionsServer();
  20. const actionFunction = async (options) => {
  21. var _a, _b, _c, _d, _e, _f;
  22. if (typeof options.port === "string") {
  23. options.port = parseInt(options.port, 10);
  24. }
  25. let debugPort = undefined;
  26. if (options.inspectFunctions) {
  27. debugPort = commandUtils.parseInspectionPort(options);
  28. }
  29. utils.assertDefined(options.project);
  30. const hubClient = new hubClient_1.EmulatorHubClient(options.project);
  31. let remoteEmulators = {};
  32. if (hubClient.foundHub()) {
  33. remoteEmulators = await hubClient.getEmulators();
  34. logger_1.logger.debug("Running emulators: ", remoteEmulators);
  35. }
  36. const runningEmulators = types_1.EMULATORS_SUPPORTED_BY_FUNCTIONS.filter((e) => remoteEmulators[e] !== undefined);
  37. const otherEmulators = types_1.EMULATORS_SUPPORTED_BY_FUNCTIONS.filter((e) => remoteEmulators[e] === undefined);
  38. let host = constants_1.Constants.getDefaultHost();
  39. let port = 5000;
  40. const functionsInfo = remoteEmulators[types_1.Emulators.FUNCTIONS];
  41. if (functionsInfo) {
  42. utils.logLabeledWarning("functions", `You are already running the Cloud Functions emulator on port ${functionsInfo.port}. Running the emulator and the Functions shell simultaenously can result in unexpected behavior.`);
  43. }
  44. else if (!options.port) {
  45. port = (_c = (_b = (_a = options.config.src.emulators) === null || _a === void 0 ? void 0 : _a.functions) === null || _b === void 0 ? void 0 : _b.port) !== null && _c !== void 0 ? _c : port;
  46. host = (_f = (_e = (_d = options.config.src.emulators) === null || _d === void 0 ? void 0 : _d.functions) === null || _e === void 0 ? void 0 : _e.host) !== null && _f !== void 0 ? _f : host;
  47. options.host = host;
  48. }
  49. const listen = (await (0, portUtils_1.resolveHostAndAssignPorts)({
  50. [types_1.Emulators.FUNCTIONS]: { host, port },
  51. })).functions;
  52. options.host = listen[0].address;
  53. options.port = listen[0].port;
  54. return serveFunctions
  55. .start(options, {
  56. quiet: true,
  57. remoteEmulators,
  58. debugPort,
  59. })
  60. .then(() => {
  61. return serveFunctions.connect();
  62. })
  63. .then(() => {
  64. const instance = serveFunctions.get();
  65. const emulator = new shell.FunctionsEmulatorShell(instance);
  66. if (emulator.emulatedFunctions && emulator.emulatedFunctions.length === 0) {
  67. logger_1.logger.info("No functions emulated.");
  68. process.exit();
  69. }
  70. const initializeContext = (context) => {
  71. for (const trigger of emulator.triggers) {
  72. if (emulator.emulatedFunctions.includes(trigger.id)) {
  73. const localFunction = new LocalFunction(trigger, emulator.urls, emulator);
  74. const triggerNameDotNotation = trigger.name.replace(/-/g, ".");
  75. _.set(context, triggerNameDotNotation, localFunction.call);
  76. }
  77. }
  78. context.help =
  79. "Instructions for the Functions Shell can be found at: " +
  80. "https://firebase.google.com/docs/functions/local-emulator";
  81. };
  82. for (const e of runningEmulators) {
  83. const info = remoteEmulators[e];
  84. utils.logLabeledBullet("functions", `Connected to running ${clc.bold(e)} emulator at ${info.host}:${info.port}, calls to this service will affect the emulator`);
  85. }
  86. utils.logLabeledWarning("functions", `The following emulators are not running, calls to these services will affect production: ${clc.bold(otherEmulators.join(", "))}`);
  87. const writer = (output) => {
  88. if (output instanceof request.Request) {
  89. return "Sent request to function.";
  90. }
  91. return util.inspect(output);
  92. };
  93. const prompt = "firebase > ";
  94. const replServer = repl.start({
  95. prompt: prompt,
  96. writer: writer,
  97. useColors: true,
  98. });
  99. initializeContext(replServer.context);
  100. replServer.on("reset", initializeContext);
  101. return new Promise((resolve) => {
  102. replServer.on("exit", () => {
  103. return serveFunctions.stop().then(resolve).catch(resolve);
  104. });
  105. });
  106. });
  107. };
  108. exports.actionFunction = actionFunction;