Sin descripción
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.showPostDeprecationNotice = exports.readInstanceParam = exports.getInstanceRef = exports.getInstanceTarget = exports.instanceExists = exports.loadConfig = exports.removeFromManifest = exports.writeLocalSecrets = exports.writeToManifest = exports.ENV_DIRECTORY = void 0;
  4. const clc = require("colorette");
  5. const path = require("path");
  6. const refs = require("./refs");
  7. const config_1 = require("../config");
  8. const planner_1 = require("../deploy/extensions/planner");
  9. const logger_1 = require("../logger");
  10. const prompt_1 = require("../prompt");
  11. const paramHelper_1 = require("./paramHelper");
  12. const error_1 = require("../error");
  13. const utils = require("../utils");
  14. const extensionsHelper_1 = require("./extensionsHelper");
  15. const types_1 = require("./types");
  16. exports.ENV_DIRECTORY = "extensions";
  17. async function writeToManifest(specs, config, options, allowOverwrite = false) {
  18. if (config.has("extensions") &&
  19. Object.keys(config.get("extensions")).length &&
  20. !options.nonInteractive &&
  21. !options.force) {
  22. const currentExtensions = Object.entries(config.get("extensions"))
  23. .map((i) => `${i[0]}: ${i[1]}`)
  24. .join("\n\t");
  25. if (allowOverwrite) {
  26. const overwrite = await (0, prompt_1.promptOnce)({
  27. type: "list",
  28. message: `firebase.json already contains extensions:\n${currentExtensions}\nWould you like to overwrite or merge?`,
  29. choices: [
  30. { name: "Overwrite", value: true },
  31. { name: "Merge", value: false },
  32. ],
  33. });
  34. if (overwrite) {
  35. config.set("extensions", {});
  36. }
  37. }
  38. }
  39. writeExtensionsToFirebaseJson(specs, config);
  40. await writeEnvFiles(specs, config, options.force);
  41. await writeLocalSecrets(specs, config, options.force);
  42. }
  43. exports.writeToManifest = writeToManifest;
  44. async function writeLocalSecrets(specs, config, force) {
  45. for (const spec of specs) {
  46. const extensionSpec = await (0, planner_1.getExtensionSpec)(spec);
  47. if (!extensionSpec.params) {
  48. continue;
  49. }
  50. const writeBuffer = {};
  51. const locallyOverridenSecretParams = extensionSpec.params.filter((p) => { var _a; return p.type === types_1.ParamType.SECRET && ((_a = spec.params[p.param]) === null || _a === void 0 ? void 0 : _a.local); });
  52. for (const paramSpec of locallyOverridenSecretParams) {
  53. const key = paramSpec.param;
  54. const localValue = spec.params[key].local;
  55. writeBuffer[key] = localValue;
  56. }
  57. const content = Object.entries(writeBuffer)
  58. .sort((a, b) => {
  59. return a[0].localeCompare(b[0]);
  60. })
  61. .map((r) => `${r[0]}=${r[1]}`)
  62. .join("\n");
  63. if (content) {
  64. await config.askWriteProjectFile(`extensions/${spec.instanceId}.secret.local`, content, force);
  65. }
  66. }
  67. }
  68. exports.writeLocalSecrets = writeLocalSecrets;
  69. function removeFromManifest(instanceId, config) {
  70. if (!instanceExists(instanceId, config)) {
  71. throw new error_1.FirebaseError(`Extension instance ${instanceId} not found in firebase.json.`);
  72. }
  73. const extensions = config.get("extensions", {});
  74. extensions[instanceId] = undefined;
  75. config.set("extensions", extensions);
  76. config.writeProjectFile("firebase.json", config.src);
  77. logger_1.logger.info(`Removed extension instance ${instanceId} from firebase.json`);
  78. config.deleteProjectFile(`extensions/${instanceId}.env`);
  79. logger_1.logger.info(`Removed extension instance environment config extensions/${instanceId}.env`);
  80. if (config.projectFileExists(`extensions/${instanceId}.env.local`)) {
  81. config.deleteProjectFile(`extensions/${instanceId}.env.local`);
  82. logger_1.logger.info(`Removed extension instance local environment config extensions/${instanceId}.env.local`);
  83. }
  84. if (config.projectFileExists(`extensions/${instanceId}.secret.local`)) {
  85. config.deleteProjectFile(`extensions/${instanceId}.secret.local`);
  86. logger_1.logger.info(`Removed extension instance local secret config extensions/${instanceId}.secret.local`);
  87. }
  88. }
  89. exports.removeFromManifest = removeFromManifest;
  90. function loadConfig(options) {
  91. const existingConfig = config_1.Config.load(options, true);
  92. if (!existingConfig) {
  93. throw new error_1.FirebaseError("Not currently in a Firebase directory. Run `firebase init` to create a Firebase directory.");
  94. }
  95. return existingConfig;
  96. }
  97. exports.loadConfig = loadConfig;
  98. function instanceExists(instanceId, config) {
  99. return !!config.get("extensions", {})[instanceId];
  100. }
  101. exports.instanceExists = instanceExists;
  102. function getInstanceTarget(instanceId, config) {
  103. if (!instanceExists(instanceId, config)) {
  104. throw new error_1.FirebaseError(`Could not find extension instance ${instanceId} in firebase.json`);
  105. }
  106. return config.get("extensions", {})[instanceId];
  107. }
  108. exports.getInstanceTarget = getInstanceTarget;
  109. function getInstanceRef(instanceId, config) {
  110. const source = getInstanceTarget(instanceId, config);
  111. if ((0, extensionsHelper_1.isLocalPath)(source)) {
  112. throw new error_1.FirebaseError(`Extension instance ${instanceId} doesn't have a ref because it is from a local source`);
  113. }
  114. return refs.parse(source);
  115. }
  116. exports.getInstanceRef = getInstanceRef;
  117. function writeExtensionsToFirebaseJson(specs, config) {
  118. const extensions = config.get("extensions", {});
  119. for (const s of specs) {
  120. let target;
  121. if (s.ref) {
  122. target = refs.toExtensionVersionRef(s.ref);
  123. }
  124. else if (s.localPath) {
  125. target = s.localPath;
  126. }
  127. else {
  128. throw new error_1.FirebaseError(`Unable to resolve ManifestInstanceSpec, make sure you provide either extension ref or a local path to extension source code`);
  129. }
  130. extensions[s.instanceId] = target;
  131. }
  132. config.set("extensions", extensions);
  133. config.writeProjectFile("firebase.json", config.src);
  134. utils.logSuccess("Wrote extensions to " + clc.bold("firebase.json") + "...");
  135. }
  136. async function writeEnvFiles(specs, config, force) {
  137. for (const spec of specs) {
  138. const content = Object.entries(spec.params)
  139. .filter((r) => r[1].baseValue !== "")
  140. .sort((a, b) => {
  141. return a[0].localeCompare(b[0]);
  142. })
  143. .map((r) => `${r[0]}=${r[1].baseValue}`)
  144. .join("\n");
  145. await config.askWriteProjectFile(`extensions/${spec.instanceId}.env`, content, force);
  146. }
  147. }
  148. function readInstanceParam(args) {
  149. var _a;
  150. const aliases = (_a = args.aliases) !== null && _a !== void 0 ? _a : [];
  151. const filesToCheck = [
  152. `${args.instanceId}.env`,
  153. ...aliases.map((alias) => `${args.instanceId}.env.${alias}`),
  154. ...(args.projectNumber ? [`${args.instanceId}.env.${args.projectNumber}`] : []),
  155. ...(args.projectId ? [`${args.instanceId}.env.${args.projectId}`] : []),
  156. ];
  157. if (args.checkLocal) {
  158. filesToCheck.push(`${args.instanceId}.env.local`);
  159. }
  160. let noFilesFound = true;
  161. const combinedParams = {};
  162. for (const fileToCheck of filesToCheck) {
  163. try {
  164. const params = readParamsFile(args.projectDir, fileToCheck);
  165. logger_1.logger.debug(`Successfully read params from ${fileToCheck}`);
  166. noFilesFound = false;
  167. Object.assign(combinedParams, params);
  168. }
  169. catch (err) {
  170. logger_1.logger.debug(`${err}`);
  171. }
  172. }
  173. if (noFilesFound) {
  174. throw new error_1.FirebaseError(`No params file found for ${args.instanceId}`);
  175. }
  176. return combinedParams;
  177. }
  178. exports.readInstanceParam = readInstanceParam;
  179. function readParamsFile(projectDir, fileName) {
  180. const paramPath = path.join(projectDir, exports.ENV_DIRECTORY, fileName);
  181. const params = (0, paramHelper_1.readEnvFile)(paramPath);
  182. return params;
  183. }
  184. function showPostDeprecationNotice() {
  185. utils.logLabeledBullet(extensionsHelper_1.logPrefix, "The behavior of ext:install, ext:update, ext:configure, and ext:uninstall has changed in firebase-tools@11.0.0. " +
  186. "Instead of deploying extensions directly, " +
  187. "changes to extension instances will be written to firebase.json and ./extensions/*.env. " +
  188. `Then ${clc.bold("firebase deploy (--only extensions)")} will deploy the changes to your Firebase project. See https://firebase.google.com/docs/extensions/manifest for more details.`);
  189. }
  190. exports.showPostDeprecationNotice = showPostDeprecationNotice;