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.

prepareFunctionsUpload.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.prepareFunctionsUpload = exports.getFunctionsConfig = void 0;
  4. const archiver = require("archiver");
  5. const clc = require("colorette");
  6. const filesize = require("filesize");
  7. const fs = require("fs");
  8. const path = require("path");
  9. const tmp = require("tmp");
  10. const error_1 = require("../../error");
  11. const logger_1 = require("../../logger");
  12. const hash_1 = require("./cache/hash");
  13. const functionsConfig = require("../../functionsConfig");
  14. const utils = require("../../utils");
  15. const fsAsync = require("../../fsAsync");
  16. const CONFIG_DEST_FILE = ".runtimeconfig.json";
  17. async function getFunctionsConfig(projectId) {
  18. var _a, _b;
  19. try {
  20. return await functionsConfig.materializeAll(projectId);
  21. }
  22. catch (err) {
  23. logger_1.logger.debug(err);
  24. let errorCode = (_b = (_a = err === null || err === void 0 ? void 0 : err.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode;
  25. if (!errorCode) {
  26. logger_1.logger.debug("Got unexpected error from Runtime Config; it has no status code:", err);
  27. errorCode = 500;
  28. }
  29. if (errorCode === 500 || errorCode === 503) {
  30. throw new error_1.FirebaseError("Cloud Runtime Config is currently experiencing issues, " +
  31. "which is preventing your functions from being deployed. " +
  32. "Please wait a few minutes and then try to deploy your functions again." +
  33. "\nRun `firebase deploy --except functions` if you want to continue deploying the rest of your project.");
  34. }
  35. }
  36. return {};
  37. }
  38. exports.getFunctionsConfig = getFunctionsConfig;
  39. async function pipeAsync(from, to) {
  40. from.pipe(to);
  41. await from.finalize();
  42. return new Promise((resolve, reject) => {
  43. to.on("finish", resolve);
  44. to.on("error", reject);
  45. });
  46. }
  47. async function packageSource(sourceDir, config, runtimeConfig) {
  48. const tmpFile = tmp.fileSync({ prefix: "firebase-functions-", postfix: ".zip" }).name;
  49. const fileStream = fs.createWriteStream(tmpFile, {
  50. flags: "w",
  51. encoding: "binary",
  52. });
  53. const archive = archiver("zip");
  54. const hashes = [];
  55. const ignore = config.ignore || ["node_modules", ".git"];
  56. ignore.push("firebase-debug.log", "firebase-debug.*.log", CONFIG_DEST_FILE);
  57. try {
  58. const files = await fsAsync.readdirRecursive({ path: sourceDir, ignore: ignore });
  59. for (const file of files) {
  60. const name = path.relative(sourceDir, file.name);
  61. const fileHash = await (0, hash_1.getSourceHash)(file.name);
  62. hashes.push(fileHash);
  63. archive.file(file.name, {
  64. name,
  65. mode: file.mode,
  66. });
  67. }
  68. if (typeof runtimeConfig !== "undefined") {
  69. const runtimeConfigString = JSON.stringify(runtimeConfig, null, 2);
  70. hashes.push(runtimeConfigString);
  71. archive.append(runtimeConfigString, {
  72. name: CONFIG_DEST_FILE,
  73. mode: 420,
  74. });
  75. }
  76. await pipeAsync(archive, fileStream);
  77. }
  78. catch (err) {
  79. throw new error_1.FirebaseError("Could not read source directory. Remove links and shortcuts and try again.", {
  80. original: err,
  81. exit: 1,
  82. });
  83. }
  84. utils.logBullet(clc.cyan(clc.bold("functions:")) +
  85. " packaged " +
  86. clc.bold(sourceDir) +
  87. " (" +
  88. filesize(archive.pointer()) +
  89. ") for uploading");
  90. const hash = hashes.join(".");
  91. return { pathToSource: tmpFile, hash };
  92. }
  93. async function prepareFunctionsUpload(sourceDir, config, runtimeConfig) {
  94. return packageSource(sourceDir, config, runtimeConfig);
  95. }
  96. exports.prepareFunctionsUpload = prepareFunctionsUpload;