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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.doSetup = void 0;
  4. const clc = require("colorette");
  5. const fs = require("fs");
  6. const rimraf_1 = require("rimraf");
  7. const apiv2_1 = require("../../../apiv2");
  8. const github_1 = require("./github");
  9. const prompt_1 = require("../../../prompt");
  10. const logger_1 = require("../../../logger");
  11. const frameworks_1 = require("../../../frameworks");
  12. const experiments = require("../../../experiments");
  13. const INDEX_TEMPLATE = fs.readFileSync(__dirname + "/../../../../templates/init/hosting/index.html", "utf8");
  14. const MISSING_TEMPLATE = fs.readFileSync(__dirname + "/../../../../templates/init/hosting/404.html", "utf8");
  15. const DEFAULT_IGNORES = ["firebase.json", "**/.*", "**/node_modules/**"];
  16. async function doSetup(setup, config) {
  17. var _a;
  18. setup.hosting = {};
  19. let discoveredFramework = experiments.isEnabled("webframeworks")
  20. ? await (0, frameworks_1.discover)(config.projectDir, false)
  21. : undefined;
  22. if (experiments.isEnabled("webframeworks")) {
  23. if (discoveredFramework) {
  24. const name = frameworks_1.WebFrameworks[discoveredFramework.framework].name;
  25. await (0, prompt_1.promptOnce)({
  26. name: "useDiscoveredFramework",
  27. type: "confirm",
  28. default: true,
  29. message: `Detected an existing ${name} codebase in the current directory, should we use this?`,
  30. }, setup.hosting);
  31. }
  32. if (setup.hosting.useDiscoveredFramework) {
  33. setup.hosting.source = ".";
  34. setup.hosting.useWebFrameworks = true;
  35. }
  36. else {
  37. await (0, prompt_1.promptOnce)({
  38. name: "useWebFrameworks",
  39. type: "confirm",
  40. default: false,
  41. message: `Do you want to use a web framework? (${clc.bold("experimental")})`,
  42. }, setup.hosting);
  43. }
  44. }
  45. if (setup.hosting.useWebFrameworks) {
  46. await (0, prompt_1.promptOnce)({
  47. name: "source",
  48. type: "input",
  49. default: "hosting",
  50. message: "What folder would you like to use for your web application's root directory?",
  51. }, setup.hosting);
  52. if (setup.hosting.source !== ".")
  53. delete setup.hosting.useDiscoveredFramework;
  54. discoveredFramework = await (0, frameworks_1.discover)(setup.hosting.source);
  55. if (discoveredFramework) {
  56. const name = frameworks_1.WebFrameworks[discoveredFramework.framework].name;
  57. await (0, prompt_1.promptOnce)({
  58. name: "useDiscoveredFramework",
  59. type: "confirm",
  60. default: true,
  61. message: `Detected an existing ${name} codebase in ${setup.hosting.source}, should we use this?`,
  62. }, setup.hosting);
  63. }
  64. if (setup.hosting.useDiscoveredFramework && discoveredFramework) {
  65. setup.hosting.webFramework = discoveredFramework.framework;
  66. }
  67. else {
  68. const choices = [];
  69. for (const value in frameworks_1.WebFrameworks) {
  70. if (frameworks_1.WebFrameworks[value]) {
  71. const { name, init } = frameworks_1.WebFrameworks[value];
  72. if (init)
  73. choices.push({ name, value });
  74. }
  75. }
  76. const defaultChoice = (_a = choices.find(({ value }) => value === (discoveredFramework === null || discoveredFramework === void 0 ? void 0 : discoveredFramework.framework))) === null || _a === void 0 ? void 0 : _a.value;
  77. await (0, prompt_1.promptOnce)({
  78. name: "whichFramework",
  79. type: "list",
  80. message: "Please choose the framework:",
  81. default: defaultChoice,
  82. choices,
  83. }, setup.hosting);
  84. if (discoveredFramework)
  85. (0, rimraf_1.sync)(setup.hosting.source);
  86. await frameworks_1.WebFrameworks[setup.hosting.whichFramework].init(setup);
  87. }
  88. setup.config.hosting = {
  89. source: setup.hosting.source,
  90. ignore: DEFAULT_IGNORES,
  91. };
  92. }
  93. else {
  94. logger_1.logger.info();
  95. logger_1.logger.info(`Your ${clc.bold("public")} directory is the folder (relative to your project directory) that`);
  96. logger_1.logger.info(`will contain Hosting assets to be uploaded with ${clc.bold("firebase deploy")}. If you`);
  97. logger_1.logger.info("have a build process for your assets, use your build's output directory.");
  98. logger_1.logger.info();
  99. await (0, prompt_1.prompt)(setup.hosting, [
  100. {
  101. name: "public",
  102. type: "input",
  103. default: "public",
  104. message: "What do you want to use as your public directory?",
  105. },
  106. {
  107. name: "spa",
  108. type: "confirm",
  109. default: false,
  110. message: "Configure as a single-page app (rewrite all urls to /index.html)?",
  111. },
  112. ]);
  113. setup.config.hosting = {
  114. public: setup.hosting.public,
  115. ignore: DEFAULT_IGNORES,
  116. };
  117. }
  118. await (0, prompt_1.promptOnce)({
  119. name: "github",
  120. type: "confirm",
  121. default: false,
  122. message: "Set up automatic builds and deploys with GitHub?",
  123. }, setup.hosting);
  124. if (!setup.hosting.useWebFrameworks) {
  125. if (setup.hosting.spa) {
  126. setup.config.hosting.rewrites = [{ source: "**", destination: "/index.html" }];
  127. }
  128. else {
  129. await config.askWriteProjectFile(`${setup.hosting.public}/404.html`, MISSING_TEMPLATE);
  130. }
  131. const c = new apiv2_1.Client({ urlPrefix: "https://www.gstatic.com", auth: false });
  132. const response = await c.get("/firebasejs/releases.json");
  133. await config.askWriteProjectFile(`${setup.hosting.public}/index.html`, INDEX_TEMPLATE.replace(/{{VERSION}}/g, response.body.current.version));
  134. }
  135. if (setup.hosting.github) {
  136. return (0, github_1.initGitHub)(setup);
  137. }
  138. }
  139. exports.doSetup = doSetup;