GSI - Employe Self Service Mobile
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.

141 lines
4.8 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generateDotenvFilename = exports.toDotenvFormat = exports.hydrateEnvs = exports.configToEnv = exports.convertKey = exports.hydrateConfigs = exports.getProjectInfos = void 0;
  4. const clc = require("colorette");
  5. const env = require("./env");
  6. const functionsConfig = require("../functionsConfig");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const projectUtils_1 = require("../projectUtils");
  10. const rc_1 = require("../rc");
  11. const utils_1 = require("../utils");
  12. const functional_1 = require("../functional");
  13. function getProjectInfos(options) {
  14. const result = {};
  15. const rc = (0, rc_1.loadRC)(options);
  16. if (rc.projects) {
  17. for (const [alias, projectId] of Object.entries(rc.projects)) {
  18. if (Object.keys(result).includes(projectId)) {
  19. (0, utils_1.logWarning)(`Multiple aliases found for ${clc.bold(projectId)}. ` +
  20. `Preferring alias (${clc.bold(result[projectId])}) over (${clc.bold(alias)}).`);
  21. continue;
  22. }
  23. result[projectId] = alias;
  24. }
  25. }
  26. const projectId = (0, projectUtils_1.getProjectId)(options);
  27. if (projectId && !Object.keys(result).includes(projectId)) {
  28. result[projectId] = projectId;
  29. }
  30. return Object.entries(result).map(([k, v]) => {
  31. const result = { projectId: k };
  32. if (k !== v) {
  33. result.alias = v;
  34. }
  35. return result;
  36. });
  37. }
  38. exports.getProjectInfos = getProjectInfos;
  39. async function hydrateConfigs(pInfos) {
  40. const hydrate = pInfos.map((info) => {
  41. return functionsConfig
  42. .materializeAll(info.projectId)
  43. .then((config) => {
  44. info.config = config;
  45. return;
  46. })
  47. .catch((err) => {
  48. logger_1.logger.debug(`Failed to fetch runtime config for project ${info.projectId}: ${err.message}`);
  49. });
  50. });
  51. await Promise.all(hydrate);
  52. }
  53. exports.hydrateConfigs = hydrateConfigs;
  54. function convertKey(configKey, prefix) {
  55. const baseKey = configKey
  56. .toUpperCase()
  57. .replace(/\./g, "_")
  58. .replace(/-/g, "_");
  59. let envKey = baseKey;
  60. try {
  61. env.validateKey(envKey);
  62. }
  63. catch (err) {
  64. if (err instanceof env.KeyValidationError) {
  65. envKey = prefix + envKey;
  66. env.validateKey(envKey);
  67. }
  68. }
  69. return envKey;
  70. }
  71. exports.convertKey = convertKey;
  72. function configToEnv(configs, prefix) {
  73. const success = [];
  74. const errors = [];
  75. for (const [configKey, value] of (0, functional_1.flatten)(configs)) {
  76. try {
  77. const envKey = convertKey(configKey, prefix);
  78. success.push({ origKey: configKey, newKey: envKey, value: value });
  79. }
  80. catch (err) {
  81. if (err instanceof env.KeyValidationError) {
  82. errors.push({
  83. origKey: configKey,
  84. newKey: err.key,
  85. err: err.message,
  86. value: value,
  87. });
  88. }
  89. else {
  90. throw new error_1.FirebaseError("Unexpected error while converting config", {
  91. exit: 2,
  92. original: err,
  93. });
  94. }
  95. }
  96. }
  97. return { success, errors };
  98. }
  99. exports.configToEnv = configToEnv;
  100. function hydrateEnvs(pInfos, prefix) {
  101. let errMsg = "";
  102. for (const pInfo of pInfos) {
  103. const { success, errors } = configToEnv(pInfo.config, prefix);
  104. if (errors.length > 0) {
  105. const msg = `${pInfo.projectId} ` +
  106. `${pInfo.alias ? "(" + pInfo.alias + ")" : ""}:\n` +
  107. errors.map((err) => `\t${err.origKey} => ${clc.bold(err.newKey)} (${err.err})`).join("\n") +
  108. "\n";
  109. errMsg += msg;
  110. }
  111. else {
  112. pInfo.envs = success;
  113. }
  114. }
  115. return errMsg;
  116. }
  117. exports.hydrateEnvs = hydrateEnvs;
  118. const CHARACTERS_TO_ESCAPE_SEQUENCES = {
  119. "\n": "\\n",
  120. "\r": "\\r",
  121. "\t": "\\t",
  122. "\v": "\\v",
  123. "\\": "\\\\",
  124. '"': '\\"',
  125. "'": "\\'",
  126. };
  127. function escape(s) {
  128. return s.replace(/[\n\r\t\v\\"']/g, (ch) => CHARACTERS_TO_ESCAPE_SEQUENCES[ch]);
  129. }
  130. function toDotenvFormat(envs, header = "") {
  131. const lines = envs.map(({ newKey, value }) => `${newKey}="${escape(value)}"`);
  132. const maxLineLen = Math.max(...lines.map((l) => l.length));
  133. return (`${header}\n` +
  134. lines.map((line, idx) => `${line.padEnd(maxLineLen)} # from ${envs[idx].origKey}`).join("\n"));
  135. }
  136. exports.toDotenvFormat = toDotenvFormat;
  137. function generateDotenvFilename(pInfo) {
  138. var _a;
  139. return `.env.${(_a = pInfo.alias) !== null && _a !== void 0 ? _a : pInfo.projectId}`;
  140. }
  141. exports.generateDotenvFilename = generateDotenvFilename;