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.

planner.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.resolveVersion = exports.want = exports.have = exports.getExtensionSpec = exports.getExtension = exports.getExtensionVersion = void 0;
  4. const semver = require("semver");
  5. const extensionsApi = require("../../extensions/extensionsApi");
  6. const refs = require("../../extensions/refs");
  7. const error_1 = require("../../error");
  8. const extensionsHelper_1 = require("../../extensions/extensionsHelper");
  9. const logger_1 = require("../../logger");
  10. const manifest_1 = require("../../extensions/manifest");
  11. const specHelper_1 = require("../../extensions/emulator/specHelper");
  12. async function getExtensionVersion(i) {
  13. if (!i.extensionVersion) {
  14. if (!i.ref) {
  15. throw new error_1.FirebaseError(`Can't get ExtensionVersion for ${i.instanceId} because it has no ref`);
  16. }
  17. i.extensionVersion = await extensionsApi.getExtensionVersion(refs.toExtensionVersionRef(i.ref));
  18. }
  19. return i.extensionVersion;
  20. }
  21. exports.getExtensionVersion = getExtensionVersion;
  22. async function getExtension(i) {
  23. if (!i.ref) {
  24. throw new error_1.FirebaseError(`Can't get Extension for ${i.instanceId} because it has no ref`);
  25. }
  26. if (!i.extension) {
  27. i.extension = await extensionsApi.getExtension(refs.toExtensionRef(i.ref));
  28. }
  29. return i.extension;
  30. }
  31. exports.getExtension = getExtension;
  32. async function getExtensionSpec(i) {
  33. if (!i.extensionSpec) {
  34. if (i.ref) {
  35. const extensionVersion = await getExtensionVersion(i);
  36. i.extensionSpec = extensionVersion.spec;
  37. }
  38. else if (i.localPath) {
  39. i.extensionSpec = await (0, specHelper_1.readExtensionYaml)(i.localPath);
  40. i.extensionSpec.postinstallContent = await (0, specHelper_1.readPostinstall)(i.localPath);
  41. }
  42. else {
  43. throw new error_1.FirebaseError("InstanceSpec had no ref or localPath, unable to get extensionSpec");
  44. }
  45. }
  46. return i.extensionSpec;
  47. }
  48. exports.getExtensionSpec = getExtensionSpec;
  49. async function have(projectId) {
  50. const instances = await extensionsApi.listInstances(projectId);
  51. return instances.map((i) => {
  52. const dep = {
  53. instanceId: i.name.split("/").pop(),
  54. params: i.config.params,
  55. allowedEventTypes: i.config.allowedEventTypes,
  56. eventarcChannel: i.config.eventarcChannel,
  57. etag: i.etag,
  58. };
  59. if (i.config.extensionRef) {
  60. const ref = refs.parse(i.config.extensionRef);
  61. dep.ref = ref;
  62. dep.ref.version = i.config.extensionVersion;
  63. }
  64. return dep;
  65. });
  66. }
  67. exports.have = have;
  68. async function want(args) {
  69. const instanceSpecs = [];
  70. const errors = [];
  71. for (const e of Object.entries(args.extensions)) {
  72. try {
  73. const instanceId = e[0];
  74. const params = (0, manifest_1.readInstanceParam)({
  75. projectDir: args.projectDir,
  76. instanceId,
  77. projectId: args.projectId,
  78. projectNumber: args.projectNumber,
  79. aliases: args.aliases,
  80. checkLocal: args.emulatorMode,
  81. });
  82. const autoPopulatedParams = await (0, extensionsHelper_1.getFirebaseProjectParams)(args.projectId, args.emulatorMode);
  83. const subbedParams = (0, extensionsHelper_1.substituteParams)(params, autoPopulatedParams);
  84. const allowedEventTypes = subbedParams.ALLOWED_EVENT_TYPES !== undefined
  85. ? subbedParams.ALLOWED_EVENT_TYPES.split(",").filter((e) => e !== "")
  86. : undefined;
  87. const eventarcChannel = subbedParams.EVENTARC_CHANNEL;
  88. delete subbedParams["EVENTARC_CHANNEL"];
  89. delete subbedParams["ALLOWED_EVENT_TYPES"];
  90. if ((0, extensionsHelper_1.isLocalPath)(e[1])) {
  91. instanceSpecs.push({
  92. instanceId,
  93. localPath: e[1],
  94. params: subbedParams,
  95. allowedEventTypes: allowedEventTypes,
  96. eventarcChannel: eventarcChannel,
  97. });
  98. }
  99. else {
  100. const ref = refs.parse(e[1]);
  101. ref.version = await resolveVersion(ref);
  102. instanceSpecs.push({
  103. instanceId,
  104. ref,
  105. params: subbedParams,
  106. allowedEventTypes: allowedEventTypes,
  107. eventarcChannel: eventarcChannel,
  108. });
  109. }
  110. }
  111. catch (err) {
  112. logger_1.logger.debug(`Got error reading extensions entry ${e}: ${err}`);
  113. errors.push(err);
  114. }
  115. }
  116. if (errors.length) {
  117. const messages = errors.map((err) => `- ${err.message}`).join("\n");
  118. throw new error_1.FirebaseError(`Errors while reading 'extensions' in 'firebase.json'\n${messages}`);
  119. }
  120. return instanceSpecs;
  121. }
  122. exports.want = want;
  123. async function resolveVersion(ref) {
  124. const extensionRef = refs.toExtensionRef(ref);
  125. const versions = await extensionsApi.listExtensionVersions(extensionRef, undefined, true);
  126. if (versions.length === 0) {
  127. throw new error_1.FirebaseError(`No versions found for ${extensionRef}`);
  128. }
  129. if (!ref.version || ref.version === "latest") {
  130. return versions
  131. .filter((ev) => ev.spec.version !== undefined)
  132. .map((ev) => ev.spec.version)
  133. .sort(semver.compare)
  134. .pop();
  135. }
  136. const maxSatisfying = semver.maxSatisfying(versions.map((ev) => ev.spec.version), ref.version);
  137. if (!maxSatisfying) {
  138. throw new error_1.FirebaseError(`No version of ${extensionRef} matches requested version ${ref.version}`);
  139. }
  140. return maxSatisfying;
  141. }
  142. exports.resolveVersion = resolveVersion;