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.

firebase.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const semver = require("semver");
  5. const pkg = require("../../package.json");
  6. const nodeVersion = process.version;
  7. if (!semver.satisfies(nodeVersion, pkg.engines.node)) {
  8. console.error(`Firebase CLI v${pkg.version} is incompatible with Node.js ${nodeVersion} Please upgrade Node.js to version ${pkg.engines.node}`);
  9. process.exit(1);
  10. }
  11. const updateNotifierPkg = require("update-notifier");
  12. const clc = require("colorette");
  13. const TerminalRenderer = require("marked-terminal");
  14. const updateNotifier = updateNotifierPkg({ pkg });
  15. const marked_1 = require("marked");
  16. marked_1.marked.setOptions({
  17. renderer: new TerminalRenderer(),
  18. });
  19. const node_path_1 = require("node:path");
  20. const triple_beam_1 = require("triple-beam");
  21. const stripAnsi = require("strip-ansi");
  22. const fs = require("node:fs");
  23. const configstore_1 = require("../configstore");
  24. const errorOut_1 = require("../errorOut");
  25. const handlePreviewToggles_1 = require("../handlePreviewToggles");
  26. const logger_1 = require("../logger");
  27. const client = require("..");
  28. const fsutils = require("../fsutils");
  29. const utils = require("../utils");
  30. const winston = require("winston");
  31. let args = process.argv.slice(2);
  32. let cmd;
  33. function findAvailableLogFile() {
  34. const candidates = ["firebase-debug.log"];
  35. for (let i = 1; i < 10; i++) {
  36. candidates.push(`firebase-debug.${i}.log`);
  37. }
  38. for (const c of candidates) {
  39. const logFilename = (0, node_path_1.join)(process.cwd(), c);
  40. try {
  41. const fd = fs.openSync(logFilename, "r+");
  42. fs.closeSync(fd);
  43. return logFilename;
  44. }
  45. catch (e) {
  46. if (e.code === "ENOENT") {
  47. return logFilename;
  48. }
  49. }
  50. }
  51. throw new Error("Unable to obtain permissions for firebase-debug.log");
  52. }
  53. const logFilename = findAvailableLogFile();
  54. if (!process.env.DEBUG && args.includes("--debug")) {
  55. process.env.DEBUG = "true";
  56. }
  57. process.env.IS_FIREBASE_CLI = "true";
  58. logger_1.logger.add(new winston.transports.File({
  59. level: "debug",
  60. filename: logFilename,
  61. format: winston.format.printf((info) => {
  62. const segments = [info.message, ...(info[triple_beam_1.SPLAT] || [])].map(utils.tryStringify);
  63. return `[${info.level}] ${stripAnsi(segments.join(" "))}`;
  64. }),
  65. }));
  66. logger_1.logger.debug("-".repeat(70));
  67. logger_1.logger.debug("Command: ", process.argv.join(" "));
  68. logger_1.logger.debug("CLI Version: ", pkg.version);
  69. logger_1.logger.debug("Platform: ", process.platform);
  70. logger_1.logger.debug("Node Version: ", process.version);
  71. logger_1.logger.debug("Time: ", new Date().toString());
  72. if (utils.envOverrides.length) {
  73. logger_1.logger.debug("Env Overrides:", utils.envOverrides.join(", "));
  74. }
  75. logger_1.logger.debug("-".repeat(70));
  76. logger_1.logger.debug();
  77. const experiments_1 = require("../experiments");
  78. const fetchMOTD_1 = require("../fetchMOTD");
  79. (0, experiments_1.enableExperimentsFromCliEnvVariable)();
  80. (0, fetchMOTD_1.fetchMOTD)();
  81. process.on("exit", (code) => {
  82. code = process.exitCode || code;
  83. if (!process.env.DEBUG && code < 2 && fsutils.fileExistsSync(logFilename)) {
  84. fs.unlinkSync(logFilename);
  85. }
  86. if (code > 0 && process.stdout.isTTY) {
  87. const lastError = configstore_1.configstore.get("lastError") || 0;
  88. const timestamp = Date.now();
  89. if (lastError > timestamp - 120000) {
  90. let help;
  91. if (code === 1 && cmd) {
  92. help = "Having trouble? Try " + clc.bold("firebase [command] --help");
  93. }
  94. else {
  95. help = "Having trouble? Try again or contact support with contents of firebase-debug.log";
  96. }
  97. if (cmd) {
  98. console.log();
  99. console.log(help);
  100. }
  101. }
  102. configstore_1.configstore.set("lastError", timestamp);
  103. }
  104. else {
  105. configstore_1.configstore.delete("lastError");
  106. }
  107. try {
  108. const updateMessage = `Update available ${clc.gray("{currentVersion}")} → ${clc.green("{latestVersion}")}\n` +
  109. `To update to the latest version using npm, run\n${clc.cyan("npm install -g firebase-tools")}\n` +
  110. `For other CLI management options, visit the ${(0, marked_1.marked)("[CLI documentation](https://firebase.google.com/docs/cli#update-cli)")}`;
  111. updateNotifier.notify({ defer: false, isGlobal: true, message: updateMessage });
  112. }
  113. catch (err) {
  114. logger_1.logger.debug("Error when notifying about new CLI updates:");
  115. if (err instanceof Error) {
  116. logger_1.logger.debug(err);
  117. }
  118. else {
  119. logger_1.logger.debug(`${err}`);
  120. }
  121. }
  122. });
  123. process.on("uncaughtException", (err) => {
  124. (0, errorOut_1.errorOut)(err);
  125. });
  126. if (!(0, handlePreviewToggles_1.handlePreviewToggles)(args)) {
  127. cmd = client.cli.parse(process.argv);
  128. args = args.filter((arg) => !arg.includes("-"));
  129. if (!args.length) {
  130. client.cli.help();
  131. }
  132. }