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.

108 lines
4.1 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.shouldUploadBeSkipped = exports.deploy = void 0;
  4. const tmp_1 = require("tmp");
  5. const clc = require("colorette");
  6. const fs = require("fs");
  7. const checkIam_1 = require("./checkIam");
  8. const utils_1 = require("../../utils");
  9. const projectConfig_1 = require("../../functions/projectConfig");
  10. const gcs = require("../../gcp/storage");
  11. const gcf = require("../../gcp/cloudfunctions");
  12. const gcfv2 = require("../../gcp/cloudfunctionsv2");
  13. const backend = require("./backend");
  14. const backend_1 = require("./backend");
  15. (0, tmp_1.setGracefulCleanup)();
  16. async function uploadSourceV1(projectId, source, wantBackend) {
  17. const v1Endpoints = backend.allEndpoints(wantBackend).filter((e) => e.platform === "gcfv1");
  18. if (v1Endpoints.length === 0) {
  19. return;
  20. }
  21. const region = v1Endpoints[0].region;
  22. const uploadUrl = await gcf.generateUploadUrl(projectId, region);
  23. const uploadOpts = {
  24. file: source.functionsSourceV1,
  25. stream: fs.createReadStream(source.functionsSourceV1),
  26. };
  27. await gcs.upload(uploadOpts, uploadUrl, {
  28. "x-goog-content-length-range": "0,104857600",
  29. });
  30. return uploadUrl;
  31. }
  32. async function uploadSourceV2(projectId, source, wantBackend) {
  33. const v2Endpoints = backend.allEndpoints(wantBackend).filter((e) => e.platform === "gcfv2");
  34. if (v2Endpoints.length === 0) {
  35. return;
  36. }
  37. const region = v2Endpoints[0].region;
  38. const res = await gcfv2.generateUploadUrl(projectId, region);
  39. const uploadOpts = {
  40. file: source.functionsSourceV2,
  41. stream: fs.createReadStream(source.functionsSourceV2),
  42. };
  43. await gcs.upload(uploadOpts, res.uploadUrl);
  44. return res.storageSource;
  45. }
  46. async function uploadCodebase(context, codebase, wantBackend) {
  47. var _a;
  48. const source = (_a = context.sources) === null || _a === void 0 ? void 0 : _a[codebase];
  49. if (!source || (!source.functionsSourceV1 && !source.functionsSourceV2)) {
  50. return;
  51. }
  52. const uploads = [];
  53. try {
  54. uploads.push(uploadSourceV1(context.projectId, source, wantBackend));
  55. uploads.push(uploadSourceV2(context.projectId, source, wantBackend));
  56. const [sourceUrl, storage] = await Promise.all(uploads);
  57. if (sourceUrl) {
  58. source.sourceUrl = sourceUrl;
  59. }
  60. if (storage) {
  61. source.storage = storage;
  62. }
  63. const sourceDir = (0, projectConfig_1.configForCodebase)(context.config, codebase).source;
  64. if (uploads.length) {
  65. (0, utils_1.logSuccess)(`${clc.green(clc.bold("functions:"))} ${clc.bold(sourceDir)} folder uploaded successfully`);
  66. }
  67. }
  68. catch (err) {
  69. (0, utils_1.logWarning)(clc.yellow("functions:") + " Upload Error: " + err.message);
  70. throw err;
  71. }
  72. }
  73. async function deploy(context, options, payload) {
  74. if (!context.config) {
  75. return;
  76. }
  77. if (!payload.functions) {
  78. return;
  79. }
  80. await (0, checkIam_1.checkHttpIam)(context, options, payload);
  81. const uploads = [];
  82. for (const [codebase, { wantBackend, haveBackend }] of Object.entries(payload.functions)) {
  83. if (shouldUploadBeSkipped(context, wantBackend, haveBackend)) {
  84. continue;
  85. }
  86. uploads.push(uploadCodebase(context, codebase, wantBackend));
  87. }
  88. await Promise.all(uploads);
  89. }
  90. exports.deploy = deploy;
  91. function shouldUploadBeSkipped(context, wantBackend, haveBackend) {
  92. if (context.filters && context.filters.length > 0) {
  93. return false;
  94. }
  95. const wantEndpoints = backend.allEndpoints(wantBackend);
  96. const haveEndpoints = backend.allEndpoints(haveBackend);
  97. if (wantEndpoints.length !== haveEndpoints.length) {
  98. return false;
  99. }
  100. return wantEndpoints.every((wantEndpoint) => {
  101. const haveEndpoint = (0, backend_1.findEndpoint)(haveBackend, (endpoint) => endpoint.id === wantEndpoint.id);
  102. if (!haveEndpoint) {
  103. return false;
  104. }
  105. return haveEndpoint.hash && wantEndpoint.hash && haveEndpoint.hash === wantEndpoint.hash;
  106. });
  107. }
  108. exports.shouldUploadBeSkipped = shouldUploadBeSkipped;