Geen omschrijving
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.

functionsConfigClone.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.functionsConfigClone = void 0;
  4. const _ = require("lodash");
  5. const clc = require("colorette");
  6. const error_1 = require("./error");
  7. const functionsConfig = require("./functionsConfig");
  8. const runtimeconfig = require("./gcp/runtimeconfig");
  9. function matchPrefix(short, long) {
  10. if (short.length > long.length) {
  11. return false;
  12. }
  13. return short.reduce((accum, x, i) => accum && x === long[i], true);
  14. }
  15. function applyExcept(json, except) {
  16. for (const key of except) {
  17. _.unset(json, key);
  18. }
  19. }
  20. function cloneVariable(varName, toProject) {
  21. return runtimeconfig.variables.get(varName).then((variable) => {
  22. const id = functionsConfig.varNameToIds(variable.name);
  23. return runtimeconfig.variables.set(toProject, id.config, id.variable, variable.text);
  24. });
  25. }
  26. function cloneConfig(configName, toProject) {
  27. return runtimeconfig.variables.list(configName).then((variables) => {
  28. return Promise.all(variables.map((variable) => {
  29. return cloneVariable(variable.name, toProject);
  30. }));
  31. });
  32. }
  33. async function cloneConfigOrVariable(key, fromProject, toProject) {
  34. const parts = key.split(".");
  35. if (functionsConfig.RESERVED_NAMESPACES.includes(parts[0])) {
  36. throw new error_1.FirebaseError("Cannot clone reserved namespace " + clc.bold(parts[0]));
  37. }
  38. const configName = ["projects", fromProject, "configs", parts[0]].join("/");
  39. if (parts.length === 1) {
  40. return cloneConfig(configName, toProject);
  41. }
  42. return runtimeconfig.variables.list(configName).then((variables) => {
  43. const promises = [];
  44. for (const variable of variables) {
  45. const varId = functionsConfig.varNameToIds(variable.name).variable;
  46. const variablePrefixFilter = parts.slice(1);
  47. if (matchPrefix(variablePrefixFilter, varId.split("/"))) {
  48. promises.push(cloneVariable(variable.name, toProject));
  49. }
  50. }
  51. return Promise.all(promises);
  52. });
  53. }
  54. async function functionsConfigClone(fromProject, toProject, only, except = []) {
  55. if (only) {
  56. return Promise.all(only.map((key) => {
  57. return cloneConfigOrVariable(key, fromProject, toProject);
  58. }));
  59. }
  60. return functionsConfig.materializeAll(fromProject).then((toClone) => {
  61. _.unset(toClone, "firebase");
  62. applyExcept(toClone, except);
  63. return Promise.all(Object.entries(toClone).map(([configId, val]) => {
  64. return functionsConfig.setVariablesRecursive(toProject, configId, "", val);
  65. }));
  66. });
  67. }
  68. exports.functionsConfigClone = functionsConfigClone;