Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.inferUpdateSource = exports.updateFromUrlSource = exports.updateFromLocalSource = exports.update = exports.warningUpdateToOtherSource = exports.getExistingSourceOrigin = void 0;
  4. const clc = require("colorette");
  5. const semver = require("semver");
  6. const { marked } = require("marked");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const extensionsApi = require("./extensionsApi");
  10. const extensionsHelper_1 = require("./extensionsHelper");
  11. const utils = require("../utils");
  12. const displayExtensionInfo_1 = require("./displayExtensionInfo");
  13. function invalidSourceErrMsgTemplate(instanceId, source) {
  14. return `Unable to update from the source \`${clc.bold(source)}\`. To update this instance, you can either:\n
  15. - Run \`${clc.bold("firebase ext:update " + instanceId)}\` to update from the published source.\n
  16. - Check your directory path or URL, then run \`${clc.bold("firebase ext:update " + instanceId + " <otherSource>")}\` to update from a local directory or URL source.`;
  17. }
  18. async function getExistingSourceOrigin(projectId, instanceId) {
  19. const instance = await extensionsApi.getInstance(projectId, instanceId);
  20. return instance && instance.config.extensionRef
  21. ? extensionsHelper_1.SourceOrigin.PUBLISHED_EXTENSION
  22. : extensionsHelper_1.SourceOrigin.LOCAL;
  23. }
  24. exports.getExistingSourceOrigin = getExistingSourceOrigin;
  25. function showUpdateVersionInfo(instanceId, from, to, source) {
  26. if (source) {
  27. source = clc.bold(source);
  28. }
  29. else {
  30. source = "version";
  31. }
  32. utils.logLabeledBullet(extensionsHelper_1.logPrefix, `Updating ${clc.bold(instanceId)} from version ${clc.bold(from)} to ${source} (${clc.bold(to)})`);
  33. if (semver.lt(to, from)) {
  34. utils.logLabeledWarning(extensionsHelper_1.logPrefix, "The version you are updating to is less than the current version for this extension. This extension may not be backwards compatible.");
  35. }
  36. return;
  37. }
  38. function warningUpdateToOtherSource(sourceOrigin) {
  39. let targetText;
  40. if ([extensionsHelper_1.SourceOrigin.PUBLISHED_EXTENSION, extensionsHelper_1.SourceOrigin.PUBLISHED_EXTENSION_VERSION].includes(sourceOrigin)) {
  41. targetText = "published extension";
  42. }
  43. else if (sourceOrigin === extensionsHelper_1.SourceOrigin.LOCAL) {
  44. targetText = "local directory";
  45. }
  46. else if (sourceOrigin === extensionsHelper_1.SourceOrigin.URL) {
  47. targetText = "URL";
  48. }
  49. const warning = `All the instance's resources and logic will be overwritten to use the source code and files from the ${targetText}.\n`;
  50. logger_1.logger.info(marked(warning));
  51. }
  52. exports.warningUpdateToOtherSource = warningUpdateToOtherSource;
  53. async function update(updateOptions) {
  54. const { projectId, instanceId, source, extRef, params, canEmitEvents, allowedEventTypes, eventarcChannel, } = updateOptions;
  55. if (extRef) {
  56. return await extensionsApi.updateInstanceFromRegistry({
  57. projectId,
  58. instanceId,
  59. extRef,
  60. params,
  61. canEmitEvents,
  62. allowedEventTypes,
  63. eventarcChannel,
  64. });
  65. }
  66. else if (source) {
  67. return await extensionsApi.updateInstance({
  68. projectId,
  69. instanceId,
  70. extensionSource: source,
  71. params,
  72. canEmitEvents,
  73. allowedEventTypes,
  74. eventarcChannel,
  75. });
  76. }
  77. throw new error_1.FirebaseError(`Neither a source nor a version of the extension was supplied for ${instanceId}. Please make sure this is a valid extension and try again.`);
  78. }
  79. exports.update = update;
  80. async function updateFromLocalSource(projectId, instanceId, localSource, existingSpec) {
  81. await (0, displayExtensionInfo_1.displayExtInfo)(instanceId, "", existingSpec, false);
  82. let source;
  83. try {
  84. source = await (0, extensionsHelper_1.createSourceFromLocation)(projectId, localSource);
  85. }
  86. catch (err) {
  87. throw new error_1.FirebaseError(invalidSourceErrMsgTemplate(instanceId, localSource));
  88. }
  89. utils.logLabeledBullet(extensionsHelper_1.logPrefix, `${clc.bold("You are updating this extension instance to a local source.")}`);
  90. showUpdateVersionInfo(instanceId, existingSpec.version, source.spec.version, localSource);
  91. warningUpdateToOtherSource(extensionsHelper_1.SourceOrigin.LOCAL);
  92. return source.name;
  93. }
  94. exports.updateFromLocalSource = updateFromLocalSource;
  95. async function updateFromUrlSource(projectId, instanceId, urlSource, existingSpec) {
  96. await (0, displayExtensionInfo_1.displayExtInfo)(instanceId, "", existingSpec, false);
  97. let source;
  98. try {
  99. source = await (0, extensionsHelper_1.createSourceFromLocation)(projectId, urlSource);
  100. }
  101. catch (err) {
  102. throw new error_1.FirebaseError(invalidSourceErrMsgTemplate(instanceId, urlSource));
  103. }
  104. utils.logLabeledBullet(extensionsHelper_1.logPrefix, `${clc.bold("You are updating this extension instance to a URL source.")}`);
  105. showUpdateVersionInfo(instanceId, existingSpec.version, source.spec.version, urlSource);
  106. warningUpdateToOtherSource(extensionsHelper_1.SourceOrigin.URL);
  107. return source.name;
  108. }
  109. exports.updateFromUrlSource = updateFromUrlSource;
  110. function inferUpdateSource(updateSource, existingRef) {
  111. if (!updateSource) {
  112. return `${existingRef}@latest`;
  113. }
  114. if (semver.valid(updateSource)) {
  115. return `${existingRef}@${updateSource}`;
  116. }
  117. if (!(0, extensionsHelper_1.isLocalOrURLPath)(updateSource) && updateSource.split("/").length < 2) {
  118. return updateSource.includes("@")
  119. ? `firebase/${updateSource}`
  120. : `firebase/${updateSource}@latest`;
  121. }
  122. if (!(0, extensionsHelper_1.isLocalOrURLPath)(updateSource) && !updateSource.includes("@")) {
  123. return `${updateSource}@latest`;
  124. }
  125. return updateSource;
  126. }
  127. exports.inferUpdateSource = inferUpdateSource;