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.

versioning.js 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.checkFunctionsSDKVersion = exports.getLatestSDKVersion = exports.getFunctionsSDKVersion = exports.findModuleVersion = exports.FUNCTIONS_SDK_VERSION_TOO_OLD_WARNING = void 0;
  4. const fs = require("fs");
  5. const path = require("path");
  6. const clc = require("colorette");
  7. const semver = require("semver");
  8. const spawn = require("cross-spawn");
  9. const utils = require("../../../../utils");
  10. const logger_1 = require("../../../../logger");
  11. const track_1 = require("../../../../track");
  12. const MIN_SDK_VERSION = "2.0.0";
  13. exports.FUNCTIONS_SDK_VERSION_TOO_OLD_WARNING = clc.bold(clc.yellow("functions: ")) +
  14. "You must have a " +
  15. clc.bold("firebase-functions") +
  16. " version that is at least 2.0.0. Please run " +
  17. clc.bold("npm i --save firebase-functions@latest") +
  18. " in the functions folder.";
  19. function findModuleVersion(name, resolvedPath) {
  20. let searchPath = path.dirname(resolvedPath);
  21. while (true) {
  22. if (searchPath === "/" || path.basename(searchPath) === "node_modules") {
  23. logger_1.logger.debug(`Failed to find version of module ${name}: reached end of search path ${searchPath}`);
  24. return;
  25. }
  26. const maybePackageJson = path.join(searchPath, "package.json");
  27. if (fs.existsSync(maybePackageJson)) {
  28. const pkg = require(maybePackageJson);
  29. if (pkg.name === name) {
  30. return pkg.version;
  31. }
  32. logger_1.logger.debug(`Failed to find version of module ${name}: instead found ${pkg.name} at ${searchPath}`);
  33. return;
  34. }
  35. searchPath = path.dirname(searchPath);
  36. }
  37. }
  38. exports.findModuleVersion = findModuleVersion;
  39. function getFunctionsSDKVersion(sourceDir) {
  40. try {
  41. return findModuleVersion("firebase-functions", require.resolve("firebase-functions", { paths: [sourceDir] }));
  42. }
  43. catch (e) {
  44. if (e.code === "MODULE_NOT_FOUND") {
  45. utils.logLabeledWarning("functions", "Couldn't find firebase-functions package in your source code. Have you run 'npm install'?");
  46. }
  47. logger_1.logger.debug("getFunctionsSDKVersion encountered error:", e);
  48. return;
  49. }
  50. }
  51. exports.getFunctionsSDKVersion = getFunctionsSDKVersion;
  52. function getLatestSDKVersion() {
  53. var _a;
  54. const child = spawn.sync("npm", ["show", "firebase-functions", "--json=true"], {
  55. encoding: "utf8",
  56. });
  57. if (child.error) {
  58. logger_1.logger.debug("checkFunctionsSDKVersion was unable to fetch information from NPM", child.error.stack);
  59. return;
  60. }
  61. const output = JSON.parse(child.stdout);
  62. if (Object.keys(output).length === 0) {
  63. return;
  64. }
  65. return (_a = output["dist-tags"]) === null || _a === void 0 ? void 0 : _a["latest"];
  66. }
  67. exports.getLatestSDKVersion = getLatestSDKVersion;
  68. function checkFunctionsSDKVersion(currentVersion) {
  69. try {
  70. if (semver.lt(currentVersion, MIN_SDK_VERSION)) {
  71. void (0, track_1.track)("functions_runtime_notices", "functions_sdk_too_old");
  72. utils.logWarning(exports.FUNCTIONS_SDK_VERSION_TOO_OLD_WARNING);
  73. }
  74. const latest = exports.getLatestSDKVersion();
  75. if (!latest) {
  76. return;
  77. }
  78. if (semver.eq(currentVersion, latest)) {
  79. return;
  80. }
  81. utils.logWarning(clc.bold(clc.yellow("functions: ")) +
  82. "package.json indicates an outdated version of firebase-functions. Please upgrade using " +
  83. clc.bold("npm install --save firebase-functions@latest") +
  84. " in your functions directory.");
  85. if (semver.major(currentVersion) < semver.major(latest)) {
  86. utils.logWarning(clc.bold(clc.yellow("functions: ")) +
  87. "Please note that there will be breaking changes when you upgrade.");
  88. }
  89. }
  90. catch (e) {
  91. logger_1.logger.debug("checkFunctionsSDKVersion encountered error:", e);
  92. return;
  93. }
  94. }
  95. exports.checkFunctionsSDKVersion = checkFunctionsSDKVersion;