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.

index.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.doSetup = void 0;
  4. const clc = require("colorette");
  5. const logger_1 = require("../../../logger");
  6. const prompt_1 = require("../../../prompt");
  7. const requirePermissions_1 = require("../../../requirePermissions");
  8. const ensureApiEnabled_1 = require("../../../ensureApiEnabled");
  9. const projectConfig_1 = require("../../../functions/projectConfig");
  10. const error_1 = require("../../../error");
  11. const MAX_ATTEMPTS = 5;
  12. async function doSetup(setup, config, options) {
  13. var _a, _b;
  14. const projectId = (_b = (_a = setup === null || setup === void 0 ? void 0 : setup.rcfile) === null || _a === void 0 ? void 0 : _a.projects) === null || _b === void 0 ? void 0 : _b.default;
  15. if (projectId) {
  16. await (0, requirePermissions_1.requirePermissions)(Object.assign(Object.assign({}, options), { project: projectId }));
  17. await Promise.all([
  18. (0, ensureApiEnabled_1.ensure)(projectId, "cloudfunctions.googleapis.com", "unused", true),
  19. (0, ensureApiEnabled_1.ensure)(projectId, "runtimeconfig.googleapis.com", "unused", true),
  20. ]);
  21. }
  22. setup.functions = {};
  23. if (!config.src.functions) {
  24. setup.config.functions = [];
  25. return initNewCodebase(setup, config);
  26. }
  27. setup.config.functions = (0, projectConfig_1.normalizeAndValidate)(setup.config.functions);
  28. const codebases = setup.config.functions.map((cfg) => clc.bold(cfg.codebase));
  29. logger_1.logger.info(`\nDetected existing codebase(s): ${codebases.join(", ")}\n`);
  30. const choices = [
  31. {
  32. name: "Initialize",
  33. value: "new",
  34. },
  35. {
  36. name: "Overwrite",
  37. value: "overwrite",
  38. },
  39. ];
  40. const initOpt = await (0, prompt_1.promptOnce)({
  41. type: "list",
  42. message: "Would you like to initialize a new codebase, or overwrite an existing one?",
  43. default: "new",
  44. choices,
  45. });
  46. return initOpt === "new" ? initNewCodebase(setup, config) : overwriteCodebase(setup, config);
  47. }
  48. exports.doSetup = doSetup;
  49. async function initNewCodebase(setup, config) {
  50. logger_1.logger.info("Let's create a new codebase for your functions.");
  51. logger_1.logger.info("A directory corresponding to the codebase will be created in your project");
  52. logger_1.logger.info("with sample code pre-configured.\n");
  53. logger_1.logger.info("See https://firebase.google.com/docs/functions/organize-functions for");
  54. logger_1.logger.info("more information on organizing your functions using codebases.\n");
  55. logger_1.logger.info(`Functions can be deployed with ${clc.bold("firebase deploy")}.\n`);
  56. let source;
  57. let codebase;
  58. if (setup.config.functions.length === 0) {
  59. source = "functions";
  60. codebase = "default";
  61. }
  62. else {
  63. let attempts = 0;
  64. while (true) {
  65. if (attempts++ >= MAX_ATTEMPTS) {
  66. throw new error_1.FirebaseError("Exceeded max number of attempts to input valid codebase name. Please restart.");
  67. }
  68. codebase = await (0, prompt_1.promptOnce)({
  69. type: "input",
  70. message: "What should be the name of this codebase?",
  71. });
  72. try {
  73. (0, projectConfig_1.validateCodebase)(codebase);
  74. (0, projectConfig_1.assertUnique)(setup.config.functions, "codebase", codebase);
  75. break;
  76. }
  77. catch (err) {
  78. logger_1.logger.error(err);
  79. }
  80. }
  81. attempts = 0;
  82. while (true) {
  83. if (attempts >= MAX_ATTEMPTS) {
  84. throw new error_1.FirebaseError("Exceeded max number of attempts to input valid source. Please restart.");
  85. }
  86. attempts++;
  87. source = await (0, prompt_1.promptOnce)({
  88. type: "input",
  89. message: `In what sub-directory would you like to initialize your functions for codebase ${clc.bold(codebase)}?`,
  90. default: codebase,
  91. });
  92. try {
  93. (0, projectConfig_1.assertUnique)(setup.config.functions, "source", source);
  94. break;
  95. }
  96. catch (err) {
  97. logger_1.logger.error(err);
  98. }
  99. }
  100. }
  101. setup.config.functions.push({
  102. source,
  103. codebase,
  104. });
  105. setup.functions.source = source;
  106. setup.functions.codebase = codebase;
  107. return languageSetup(setup, config);
  108. }
  109. async function overwriteCodebase(setup, config) {
  110. let codebase;
  111. if (setup.config.functions.length > 1) {
  112. const choices = setup.config.functions.map((cfg) => ({
  113. name: cfg["codebase"],
  114. value: cfg["codebase"],
  115. }));
  116. codebase = await (0, prompt_1.promptOnce)({
  117. type: "list",
  118. message: "Which codebase would you like to overwrite?",
  119. choices,
  120. });
  121. }
  122. else {
  123. codebase = setup.config.functions[0].codebase;
  124. }
  125. const cbconfig = (0, projectConfig_1.configForCodebase)(setup.config.functions, codebase);
  126. setup.functions.source = cbconfig.source;
  127. setup.functions.codebase = cbconfig.codebase;
  128. logger_1.logger.info(`\nOverwriting ${clc.bold(`codebase ${codebase}...\n`)}`);
  129. return languageSetup(setup, config);
  130. }
  131. async function languageSetup(setup, config) {
  132. const choices = [
  133. {
  134. name: "JavaScript",
  135. value: "javascript",
  136. },
  137. {
  138. name: "TypeScript",
  139. value: "typescript",
  140. },
  141. ];
  142. const language = await (0, prompt_1.promptOnce)({
  143. type: "list",
  144. message: "What language would you like to use to write Cloud Functions?",
  145. default: "javascript",
  146. choices,
  147. });
  148. const cbconfig = (0, projectConfig_1.configForCodebase)(setup.config.functions, setup.functions.codebase);
  149. switch (language) {
  150. case "javascript":
  151. cbconfig.ignore = ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log"];
  152. break;
  153. case "typescript":
  154. cbconfig.ignore = ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log"];
  155. break;
  156. }
  157. return require("./" + language).setup(setup, config);
  158. }