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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.convertToSortedKeyValueArray = 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 runtimeConfigHashString = JSON.stringify(convertToSortedKeyValueArray(runtimeConfig));
  70. hashes.push(runtimeConfigHashString);
  71. const runtimeConfigString = JSON.stringify(runtimeConfig, null, 2);
  72. archive.append(runtimeConfigString, {
  73. name: CONFIG_DEST_FILE,
  74. mode: 420,
  75. });
  76. }
  77. await pipeAsync(archive, fileStream);
  78. }
  79. catch (err) {
  80. throw new error_1.FirebaseError("Could not read source directory. Remove links and shortcuts and try again.", {
  81. original: err,
  82. exit: 1,
  83. });
  84. }
  85. utils.logBullet(clc.cyan(clc.bold("functions:")) +
  86. " packaged " +
  87. clc.bold(sourceDir) +
  88. " (" +
  89. filesize(archive.pointer()) +
  90. ") for uploading");
  91. const hash = hashes.join(".");
  92. return { pathToSource: tmpFile, hash };
  93. }
  94. async function prepareFunctionsUpload(sourceDir, config, runtimeConfig) {
  95. return packageSource(sourceDir, config, runtimeConfig);
  96. }
  97. exports.prepareFunctionsUpload = prepareFunctionsUpload;
  98. function convertToSortedKeyValueArray(config) {
  99. if (typeof config !== "object" || config === null)
  100. return config;
  101. return Object.keys(config)
  102. .sort()
  103. .map((key) => {
  104. return { key, value: convertToSortedKeyValueArray(config[key]) };
  105. });
  106. }
  107. exports.convertToSortedKeyValueArray = convertToSortedKeyValueArray;