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.

rules.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initRules = void 0;
  4. const clc = require("colorette");
  5. const fs = require("fs");
  6. const gcp = require("../../../gcp");
  7. const fsutils = require("../../../fsutils");
  8. const prompt_1 = require("../../../prompt");
  9. const logger_1 = require("../../../logger");
  10. const utils = require("../../../utils");
  11. const DEFAULT_RULES_FILE = "firestore.rules";
  12. const RULES_TEMPLATE = fs.readFileSync(__dirname + "/../../../../templates/init/firestore/firestore.rules", "utf8");
  13. function initRules(setup, config) {
  14. logger_1.logger.info();
  15. logger_1.logger.info("Firestore Security Rules allow you to define how and when to allow");
  16. logger_1.logger.info("requests. You can keep these rules in your project directory");
  17. logger_1.logger.info("and publish them with " + clc.bold("firebase deploy") + ".");
  18. logger_1.logger.info();
  19. return (0, prompt_1.prompt)(setup.config.firestore, [
  20. {
  21. type: "input",
  22. name: "rules",
  23. message: "What file should be used for Firestore Rules?",
  24. default: DEFAULT_RULES_FILE,
  25. },
  26. ])
  27. .then(() => {
  28. const filename = setup.config.firestore.rules;
  29. if (fsutils.fileExistsSync(filename)) {
  30. const msg = "File " +
  31. clc.bold(filename) +
  32. " already exists." +
  33. " Do you want to overwrite it with the Firestore Rules from the Firebase Console?";
  34. return (0, prompt_1.promptOnce)({
  35. type: "confirm",
  36. message: msg,
  37. default: false,
  38. });
  39. }
  40. return Promise.resolve(true);
  41. })
  42. .then((overwrite) => {
  43. if (!overwrite) {
  44. return Promise.resolve();
  45. }
  46. if (!setup.projectId) {
  47. return config.writeProjectFile(setup.config.firestore.rules, getDefaultRules());
  48. }
  49. return getRulesFromConsole(setup.projectId).then((contents) => {
  50. return config.writeProjectFile(setup.config.firestore.rules, contents);
  51. });
  52. });
  53. }
  54. exports.initRules = initRules;
  55. function getDefaultRules() {
  56. const date = utils.thirtyDaysFromNow();
  57. const formattedForRules = `${date.getFullYear()}, ${date.getMonth() + 1}, ${date.getDate()}`;
  58. return RULES_TEMPLATE.replace(/{{IN_30_DAYS}}/g, formattedForRules);
  59. }
  60. function getRulesFromConsole(projectId) {
  61. return gcp.rules
  62. .getLatestRulesetName(projectId, "cloud.firestore")
  63. .then((name) => {
  64. if (!name) {
  65. logger_1.logger.debug("No rulesets found, using default.");
  66. return [{ name: DEFAULT_RULES_FILE, content: getDefaultRules() }];
  67. }
  68. logger_1.logger.debug("Found ruleset: " + name);
  69. return gcp.rules.getRulesetContent(name);
  70. })
  71. .then((rules) => {
  72. if (rules.length <= 0) {
  73. return utils.reject("Ruleset has no files", { exit: 1 });
  74. }
  75. if (rules.length > 1) {
  76. return utils.reject("Ruleset has too many files: " + rules.length, { exit: 1 });
  77. }
  78. return rules[0].content;
  79. });
  80. }