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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isSystemParam = exports.readEnvFile = exports.promptForNewParams = exports.getParamsForUpdate = exports.getParams = exports.getParamsWithCurrentValuesAsDefaults = exports.setNewDefaults = exports.buildBindingOptionsWithBaseValue = exports.getBaseParamBindings = void 0;
  4. const path = require("path");
  5. const clc = require("colorette");
  6. const fs = require("fs-extra");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const extensionsHelper_1 = require("./extensionsHelper");
  10. const askUserForParam = require("./askUserForParam");
  11. const track_1 = require("../track");
  12. const env = require("../functions/env");
  13. const utils_1 = require("../utils");
  14. const warnings_1 = require("./warnings");
  15. function getBaseParamBindings(params) {
  16. let ret = {};
  17. for (const [k, v] of Object.entries(params)) {
  18. ret = Object.assign(Object.assign({}, ret), { [k]: v.baseValue });
  19. }
  20. return ret;
  21. }
  22. exports.getBaseParamBindings = getBaseParamBindings;
  23. function buildBindingOptionsWithBaseValue(baseParams) {
  24. let paramOptions = {};
  25. for (const [k, v] of Object.entries(baseParams)) {
  26. paramOptions = Object.assign(Object.assign({}, paramOptions), { [k]: { baseValue: v } });
  27. }
  28. return paramOptions;
  29. }
  30. exports.buildBindingOptionsWithBaseValue = buildBindingOptionsWithBaseValue;
  31. function setNewDefaults(params, newDefaults) {
  32. params.forEach((param) => {
  33. if (newDefaults[param.param.toUpperCase()]) {
  34. param.default = newDefaults[param.param.toUpperCase()];
  35. }
  36. });
  37. return params;
  38. }
  39. exports.setNewDefaults = setNewDefaults;
  40. function getParamsWithCurrentValuesAsDefaults(extensionInstance) {
  41. var _a, _b, _c, _d;
  42. const specParams = (0, utils_1.cloneDeep)(((_c = (_b = (_a = extensionInstance === null || extensionInstance === void 0 ? void 0 : extensionInstance.config) === null || _a === void 0 ? void 0 : _a.source) === null || _b === void 0 ? void 0 : _b.spec) === null || _c === void 0 ? void 0 : _c.params) || []);
  43. const currentParams = (0, utils_1.cloneDeep)(((_d = extensionInstance === null || extensionInstance === void 0 ? void 0 : extensionInstance.config) === null || _d === void 0 ? void 0 : _d.params) || {});
  44. return setNewDefaults(specParams, currentParams);
  45. }
  46. exports.getParamsWithCurrentValuesAsDefaults = getParamsWithCurrentValuesAsDefaults;
  47. async function getParams(args) {
  48. let params;
  49. if (args.nonInteractive && !args.paramsEnvPath) {
  50. const paramsMessage = args.paramSpecs
  51. .map((p) => {
  52. return `\t${p.param}${p.required ? "" : " (Optional)"}`;
  53. })
  54. .join("\n");
  55. throw new error_1.FirebaseError("In non-interactive mode but no `--params` flag found. " +
  56. "To install this extension in non-interactive mode, set `--params` to a path to an .env file" +
  57. " containing values for this extension's params:\n" +
  58. paramsMessage);
  59. }
  60. else if (args.paramsEnvPath) {
  61. (0, warnings_1.paramsFlagDeprecationWarning)();
  62. params = getParamsFromFile({
  63. paramSpecs: args.paramSpecs,
  64. paramsEnvPath: args.paramsEnvPath,
  65. });
  66. }
  67. else {
  68. const firebaseProjectParams = await (0, extensionsHelper_1.getFirebaseProjectParams)(args.projectId);
  69. params = await askUserForParam.ask({
  70. projectId: args.projectId,
  71. instanceId: args.instanceId,
  72. paramSpecs: args.paramSpecs,
  73. firebaseProjectParams,
  74. reconfiguring: !!args.reconfiguring,
  75. });
  76. }
  77. const paramNames = Object.keys(params);
  78. void (0, track_1.track)("Extension Params", paramNames.length ? "Not Present" : "Present", paramNames.length);
  79. return params;
  80. }
  81. exports.getParams = getParams;
  82. async function getParamsForUpdate(args) {
  83. let params;
  84. if (args.nonInteractive && !args.paramsEnvPath) {
  85. const paramsMessage = args.newSpec.params
  86. .map((p) => {
  87. return `\t${p.param}${p.required ? "" : " (Optional)"}`;
  88. })
  89. .join("\n");
  90. throw new error_1.FirebaseError("In non-interactive mode but no `--params` flag found. " +
  91. "To update this extension in non-interactive mode, set `--params` to a path to an .env file" +
  92. " containing values for this extension's params:\n" +
  93. paramsMessage);
  94. }
  95. else if (args.paramsEnvPath) {
  96. params = getParamsFromFile({
  97. paramSpecs: args.newSpec.params,
  98. paramsEnvPath: args.paramsEnvPath,
  99. });
  100. }
  101. else {
  102. params = await promptForNewParams({
  103. spec: args.spec,
  104. newSpec: args.newSpec,
  105. currentParams: args.currentParams,
  106. projectId: args.projectId,
  107. instanceId: args.instanceId,
  108. });
  109. }
  110. const paramNames = Object.keys(params);
  111. void (0, track_1.track)("Extension Params", paramNames.length ? "Not Present" : "Present", paramNames.length);
  112. return params;
  113. }
  114. exports.getParamsForUpdate = getParamsForUpdate;
  115. async function promptForNewParams(args) {
  116. const newParamBindingOptions = buildBindingOptionsWithBaseValue(args.currentParams);
  117. const firebaseProjectParams = await (0, extensionsHelper_1.getFirebaseProjectParams)(args.projectId);
  118. const sameParam = (param1) => (param2) => {
  119. return param1.type === param2.type && param1.param === param2.param;
  120. };
  121. const paramDiff = (left, right) => {
  122. return left.filter((aLeft) => !right.find(sameParam(aLeft)));
  123. };
  124. const oldParams = args.spec.params.filter((p) => Object.keys(args.currentParams).includes(p.param));
  125. let paramsDiffDeletions = paramDiff(oldParams, args.newSpec.params);
  126. paramsDiffDeletions = (0, extensionsHelper_1.substituteParams)(paramsDiffDeletions, firebaseProjectParams);
  127. let paramsDiffAdditions = paramDiff(args.newSpec.params, oldParams);
  128. paramsDiffAdditions = (0, extensionsHelper_1.substituteParams)(paramsDiffAdditions, firebaseProjectParams);
  129. if (paramsDiffDeletions.length) {
  130. logger_1.logger.info("The following params will no longer be used:");
  131. for (const param of paramsDiffDeletions) {
  132. logger_1.logger.info(clc.red(`- ${param.param}: ${args.currentParams[param.param.toUpperCase()]}`));
  133. delete newParamBindingOptions[param.param.toUpperCase()];
  134. }
  135. }
  136. if (paramsDiffAdditions.length) {
  137. logger_1.logger.info("To update this instance, configure the following new parameters:");
  138. for (const param of paramsDiffAdditions) {
  139. const chosenValue = await askUserForParam.askForParam({
  140. projectId: args.projectId,
  141. instanceId: args.instanceId,
  142. paramSpec: param,
  143. reconfiguring: false,
  144. });
  145. newParamBindingOptions[param.param] = chosenValue;
  146. }
  147. }
  148. return newParamBindingOptions;
  149. }
  150. exports.promptForNewParams = promptForNewParams;
  151. function getParamsFromFile(args) {
  152. let envParams;
  153. try {
  154. envParams = readEnvFile(args.paramsEnvPath);
  155. void (0, track_1.track)("Extension Env File", "Present");
  156. }
  157. catch (err) {
  158. void (0, track_1.track)("Extension Env File", "Invalid");
  159. throw new error_1.FirebaseError(`Error reading env file: ${err.message}\n`, { original: err });
  160. }
  161. const params = (0, extensionsHelper_1.populateDefaultParams)(envParams, args.paramSpecs);
  162. (0, extensionsHelper_1.validateCommandLineParams)(params, args.paramSpecs);
  163. logger_1.logger.info(`Using param values from ${args.paramsEnvPath}`);
  164. return buildBindingOptionsWithBaseValue(params);
  165. }
  166. function readEnvFile(envPath) {
  167. const buf = fs.readFileSync(path.resolve(envPath), "utf8");
  168. const result = env.parse(buf.toString().trim());
  169. if (result.errors.length) {
  170. throw new error_1.FirebaseError(`Error while parsing ${envPath} - unable to parse following lines:\n${result.errors.join("\n")}`);
  171. }
  172. return result.envs;
  173. }
  174. exports.readEnvFile = readEnvFile;
  175. function isSystemParam(paramName) {
  176. const regex = /^firebaseextensions\.[a-zA-Z0-9\.]*\//;
  177. return regex.test(paramName);
  178. }
  179. exports.isSystemParam = isSystemParam;