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.

hosting-clone.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const colorette_1 = require("colorette");
  5. const ora = require("ora");
  6. const command_1 = require("../command");
  7. const error_1 = require("../error");
  8. const api_1 = require("../hosting/api");
  9. const utils = require("../utils");
  10. const requireAuth_1 = require("../requireAuth");
  11. const { marked } = require("marked");
  12. const logger_1 = require("../logger");
  13. exports.command = new command_1.Command("hosting:clone <source> <targetChannel>")
  14. .description("clone a version from one site to another")
  15. .help(`<source> and <targetChannel> accept the following format: <siteId>:<channelId>
  16. For example, to copy the content for a site \`my-site\` from a preview channel \`staging\` to a \`live\` channel, the command would look be:
  17. firebase hosting:clone my-site:foo my-site:live`)
  18. .before(requireAuth_1.requireAuth)
  19. .action(async (source = "", targetChannel = "") => {
  20. var _a, _b, _c, _d;
  21. let sourceVersionName;
  22. let sourceVersion;
  23. let [sourceSiteId, sourceChannelId] = source.split(":");
  24. let [targetSiteId, targetChannelId] = targetChannel.split(":");
  25. if (!sourceSiteId || !sourceChannelId) {
  26. [sourceSiteId, sourceVersion] = source.split("@");
  27. if (!sourceSiteId || !sourceVersion) {
  28. throw new error_1.FirebaseError(`"${source}" is not a valid source. Must be in the form "<site>:<channel>" or "<site>@<version>"`);
  29. }
  30. sourceVersionName = `sites/${sourceSiteId}/versions/${sourceVersion}`;
  31. }
  32. if (!targetSiteId || !targetChannelId) {
  33. throw new error_1.FirebaseError(`"${targetChannel}" is not a valid target channel. Must be in the form "<site>:<channel>" (to clone to the active website, use "live" as the channel).`);
  34. }
  35. targetChannelId = (0, api_1.normalizeName)(targetChannelId);
  36. if (sourceChannelId) {
  37. sourceChannelId = (0, api_1.normalizeName)(sourceChannelId);
  38. }
  39. const equalSiteIds = sourceSiteId === targetSiteId;
  40. const equalChannelIds = sourceChannelId === targetChannelId;
  41. if (equalSiteIds && equalChannelIds) {
  42. throw new error_1.FirebaseError(`Source and destination cannot be equal. Please pick a different source or desination.`);
  43. }
  44. if (!sourceVersionName) {
  45. const sChannel = await (0, api_1.getChannel)("-", sourceSiteId, sourceChannelId);
  46. if (!sChannel) {
  47. throw new error_1.FirebaseError(`Could not find the channel ${(0, colorette_1.bold)(sourceChannelId)} for site ${(0, colorette_1.bold)(sourceSiteId)}.`);
  48. }
  49. sourceVersionName = (_b = (_a = sChannel.release) === null || _a === void 0 ? void 0 : _a.version) === null || _b === void 0 ? void 0 : _b.name;
  50. if (!sourceVersionName) {
  51. throw new error_1.FirebaseError(`Could not find a version on the channel ${(0, colorette_1.bold)(sourceChannelId)} for site ${(0, colorette_1.bold)(sourceSiteId)}.`);
  52. }
  53. }
  54. let tChannel = await (0, api_1.getChannel)("-", targetSiteId, targetChannelId);
  55. if (!tChannel) {
  56. utils.logBullet(`could not find channel ${(0, colorette_1.bold)(targetChannelId)} in site ${(0, colorette_1.bold)(targetSiteId)}, creating it...`);
  57. try {
  58. tChannel = await (0, api_1.createChannel)("-", targetSiteId, targetChannelId);
  59. }
  60. catch (e) {
  61. throw new error_1.FirebaseError(`Could not create the channel ${(0, colorette_1.bold)(targetChannelId)} for site ${(0, colorette_1.bold)(targetSiteId)}.`, { original: e });
  62. }
  63. utils.logSuccess(`Created new channel ${targetChannelId}`);
  64. try {
  65. const tProjectId = parseProjectId(tChannel.name);
  66. await (0, api_1.addAuthDomains)(tProjectId, [tChannel.url]);
  67. }
  68. catch (e) {
  69. utils.logLabeledWarning("hosting:clone", marked(`Unable to add channel domain to Firebase Auth. Visit the Firebase Console at ${utils.consoleUrl(targetSiteId, "/authentication/providers")}`));
  70. logger_1.logger.debug("[hosting] unable to add auth domain", e);
  71. }
  72. }
  73. const currentTargetVersionName = (_d = (_c = tChannel.release) === null || _c === void 0 ? void 0 : _c.version) === null || _d === void 0 ? void 0 : _d.name;
  74. if (equalSiteIds && sourceVersionName === currentTargetVersionName) {
  75. utils.logSuccess(`Channels ${(0, colorette_1.bold)(sourceChannelId)} and ${(0, colorette_1.bold)(targetChannel)} are serving identical versions. No need to clone.`);
  76. return;
  77. }
  78. let targetVersionName = sourceVersionName;
  79. const spinner = ora("Cloning site content...").start();
  80. try {
  81. if (!equalSiteIds) {
  82. const targetVersion = await (0, api_1.cloneVersion)(targetSiteId, sourceVersionName, true);
  83. if (!targetVersion) {
  84. throw new error_1.FirebaseError(`Could not clone the version ${(0, colorette_1.bold)(sourceVersion)} for site ${(0, colorette_1.bold)(targetSiteId)}.`);
  85. }
  86. targetVersionName = targetVersion.name;
  87. }
  88. await (0, api_1.createRelease)(targetSiteId, targetChannelId, targetVersionName);
  89. }
  90. catch (err) {
  91. spinner.fail();
  92. throw err;
  93. }
  94. spinner.succeed();
  95. utils.logSuccess(`Site ${(0, colorette_1.bold)(sourceSiteId)} ${sourceChannelId ? "channel" : "version"} ${(0, colorette_1.bold)(sourceChannelId || sourceVersion)} has been cloned to site ${(0, colorette_1.bold)(targetSiteId)} channel ${(0, colorette_1.bold)(targetChannelId)}.`);
  96. utils.logSuccess(`Channel URL (${targetChannelId}): ${tChannel.url}`);
  97. });
  98. function parseProjectId(name) {
  99. const matches = name.match(`^projects/([^/]+)`);
  100. return matches ? matches[1] || "" : "";
  101. }