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.

specHelper.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getNodeVersion = exports.getFunctionProperties = exports.getFunctionResourcesWithParamSubstitution = exports.readFileFromDirectory = exports.readPostinstall = exports.readExtensionYaml = void 0;
  4. const yaml = require("js-yaml");
  5. const path = require("path");
  6. const fs = require("fs-extra");
  7. const error_1 = require("../../error");
  8. const extensionsHelper_1 = require("../extensionsHelper");
  9. const utils_1 = require("../utils");
  10. const functionsEmulatorUtils_1 = require("../../emulator/functionsEmulatorUtils");
  11. const SPEC_FILE = "extension.yaml";
  12. const POSTINSTALL_FILE = "POSTINSTALL.md";
  13. const validFunctionTypes = [
  14. "firebaseextensions.v1beta.function",
  15. "firebaseextensions.v1beta.v2function",
  16. "firebaseextensions.v1beta.scheduledFunction",
  17. ];
  18. function wrappedSafeLoad(source) {
  19. try {
  20. return yaml.safeLoad(source);
  21. }
  22. catch (err) {
  23. if (err instanceof yaml.YAMLException) {
  24. throw new error_1.FirebaseError(`YAML Error: ${err.message}`, { original: err });
  25. }
  26. throw err;
  27. }
  28. }
  29. async function readExtensionYaml(directory) {
  30. const extensionYaml = await readFileFromDirectory(directory, SPEC_FILE);
  31. const source = extensionYaml.source;
  32. return wrappedSafeLoad(source);
  33. }
  34. exports.readExtensionYaml = readExtensionYaml;
  35. async function readPostinstall(directory) {
  36. const content = await readFileFromDirectory(directory, POSTINSTALL_FILE);
  37. return content.source;
  38. }
  39. exports.readPostinstall = readPostinstall;
  40. function readFileFromDirectory(directory, file) {
  41. return new Promise((resolve, reject) => {
  42. fs.readFile(path.resolve(directory, file), "utf8", (err, data) => {
  43. if (err) {
  44. if (err.code === "ENOENT") {
  45. return reject(new error_1.FirebaseError(`Could not find "${file}" in "${directory}"`, { original: err }));
  46. }
  47. reject(new error_1.FirebaseError(`Failed to read file "${file}" in "${directory}"`, { original: err }));
  48. }
  49. else {
  50. resolve(data);
  51. }
  52. });
  53. }).then((source) => {
  54. return {
  55. source,
  56. sourceDirectory: directory,
  57. };
  58. });
  59. }
  60. exports.readFileFromDirectory = readFileFromDirectory;
  61. function getFunctionResourcesWithParamSubstitution(extensionSpec, params) {
  62. const rawResources = extensionSpec.resources.filter((resource) => validFunctionTypes.includes(resource.type));
  63. return (0, extensionsHelper_1.substituteParams)(rawResources, params);
  64. }
  65. exports.getFunctionResourcesWithParamSubstitution = getFunctionResourcesWithParamSubstitution;
  66. function getFunctionProperties(resources) {
  67. return resources.map((r) => r.properties);
  68. }
  69. exports.getFunctionProperties = getFunctionProperties;
  70. function getNodeVersion(resources) {
  71. const invalidRuntimes = [];
  72. const versions = resources.map((r) => {
  73. if ((0, utils_1.getResourceRuntime)(r)) {
  74. const runtimeName = (0, utils_1.getResourceRuntime)(r);
  75. const runtime = (0, functionsEmulatorUtils_1.parseRuntimeVersion)(runtimeName);
  76. if (!runtime) {
  77. invalidRuntimes.push(runtimeName);
  78. }
  79. else {
  80. return runtime;
  81. }
  82. }
  83. return 14;
  84. });
  85. if (invalidRuntimes.length) {
  86. throw new error_1.FirebaseError(`The following runtimes are not supported by the Emulator Suite: ${invalidRuntimes.join(", ")}. \n Only Node runtimes are supported.`);
  87. }
  88. return Math.max(...versions);
  89. }
  90. exports.getNodeVersion = getNodeVersion;