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.

parseRuntimeAndValidateSDK.js 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getRuntimeChoice = exports.DEPRECATED_NODE_VERSION_INFO = exports.UNSUPPORTED_NODE_VERSION_PACKAGE_JSON_MSG = exports.UNSUPPORTED_NODE_VERSION_FIREBASE_JSON_MSG = exports.RUNTIME_NOT_SET = void 0;
  4. const path = require("path");
  5. const clc = require("colorette");
  6. const error_1 = require("../../../../error");
  7. const track_1 = require("../../../../track");
  8. const runtimes = require("../../runtimes");
  9. const cjson = require("cjson");
  10. const ENGINE_RUNTIMES = {
  11. 6: "nodejs6",
  12. 8: "nodejs8",
  13. 10: "nodejs10",
  14. 12: "nodejs12",
  15. 14: "nodejs14",
  16. 16: "nodejs16",
  17. 18: "nodejs18",
  18. };
  19. const ENGINE_RUNTIMES_NAMES = Object.values(ENGINE_RUNTIMES);
  20. exports.RUNTIME_NOT_SET = "`runtime` field is required but was not found in firebase.json.\n" +
  21. "To fix this, add the following lines to the `functions` section of your firebase.json:\n" +
  22. '"runtime": "nodejs16"\n';
  23. exports.UNSUPPORTED_NODE_VERSION_FIREBASE_JSON_MSG = clc.bold(`functions.runtime value is unsupported. ` +
  24. `Valid choices are: ${clc.bold("nodejs{10|12|14|16}")}.`);
  25. exports.UNSUPPORTED_NODE_VERSION_PACKAGE_JSON_MSG = clc.bold(`package.json in functions directory has an engines field which is unsupported. ` +
  26. `Valid choices are: ${clc.bold('{"node": 10|12|14|16}')}`);
  27. exports.DEPRECATED_NODE_VERSION_INFO = `\n\nDeploys to runtimes below Node.js 10 are now disabled in the Firebase CLI. ` +
  28. `${clc.bold(`Existing Node.js 8 functions ${clc.underline("will stop executing at a future date")}`)}. Update existing functions to Node.js 10 or greater as soon as possible.`;
  29. function getRuntimeChoiceFromPackageJson(sourceDir) {
  30. const packageJsonPath = path.join(sourceDir, "package.json");
  31. let loaded;
  32. try {
  33. loaded = cjson.load(packageJsonPath);
  34. }
  35. catch (err) {
  36. throw new error_1.FirebaseError(`Unable to load ${packageJsonPath}: ${err}`);
  37. }
  38. const engines = loaded.engines;
  39. if (!engines || !engines.node) {
  40. throw new error_1.FirebaseError(exports.RUNTIME_NOT_SET);
  41. }
  42. return ENGINE_RUNTIMES[engines.node];
  43. }
  44. function getRuntimeChoice(sourceDir, runtimeFromConfig) {
  45. const runtime = runtimeFromConfig || getRuntimeChoiceFromPackageJson(sourceDir);
  46. const errorMessage = (runtimeFromConfig
  47. ? exports.UNSUPPORTED_NODE_VERSION_FIREBASE_JSON_MSG
  48. : exports.UNSUPPORTED_NODE_VERSION_PACKAGE_JSON_MSG) + exports.DEPRECATED_NODE_VERSION_INFO;
  49. if (!runtime || !ENGINE_RUNTIMES_NAMES.includes(runtime)) {
  50. void (0, track_1.track)("functions_runtime_notices", "package_missing_runtime");
  51. throw new error_1.FirebaseError(errorMessage, { exit: 1 });
  52. }
  53. if (runtimes.isDeprecatedRuntime(runtime) || !runtimes.isValidRuntime(runtime)) {
  54. void (0, track_1.track)("functions_runtime_notices", `${runtime}_deploy_prohibited`);
  55. throw new error_1.FirebaseError(errorMessage, { exit: 1 });
  56. }
  57. return runtime;
  58. }
  59. exports.getRuntimeChoice = getRuntimeChoice;