version-checkers.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var semver_1 = require("semver");
  4. var get_package_version_1 = require("./get-package-version");
  5. var logger_1 = require("./logger");
  6. var messages_1 = require("./messages");
  7. var logger = logger_1.rootLogger.child({ namespace: 'versions' });
  8. var ExpectedVersions;
  9. (function (ExpectedVersions) {
  10. ExpectedVersions["Jest"] = ">=24 <25";
  11. ExpectedVersions["TypeScript"] = ">=2.7 <4";
  12. ExpectedVersions["BabelJest"] = ">=24 <25";
  13. ExpectedVersions["BabelCore"] = ">=7.0.0-beta.0 <8";
  14. })(ExpectedVersions = exports.ExpectedVersions || (exports.ExpectedVersions = {}));
  15. exports.VersionCheckers = {
  16. jest: createVersionChecker('jest', ExpectedVersions.Jest),
  17. typescript: createVersionChecker('typescript', ExpectedVersions.TypeScript),
  18. babelJest: createVersionChecker('babel-jest', ExpectedVersions.BabelJest),
  19. babelCore: createVersionChecker('@babel/core', ExpectedVersions.BabelCore),
  20. };
  21. function checkVersion(name, expectedRange, action) {
  22. if (action === void 0) { action = 'warn'; }
  23. var version = get_package_version_1.getPackageVersion(name);
  24. var success = !!version && semver_1.satisfies(version, expectedRange);
  25. logger.debug({
  26. actualVersion: version,
  27. expectedVersion: expectedRange,
  28. }, 'checking version of %s: %s', name, success ? 'OK' : 'NOT OK');
  29. if (!action || success)
  30. return success;
  31. var message = messages_1.interpolate(version ? messages_1.Errors.UntestedDependencyVersion : messages_1.Errors.MissingDependency, {
  32. module: name,
  33. actualVersion: version || '??',
  34. expectedVersion: rangeToHumanString(expectedRange),
  35. });
  36. if (action === 'warn') {
  37. logger.warn(message);
  38. }
  39. else if (action === 'throw') {
  40. logger.fatal(message);
  41. throw new RangeError(message);
  42. }
  43. return success;
  44. }
  45. function rangeToHumanString(versionRange) {
  46. return new semver_1.Range(versionRange).toString();
  47. }
  48. function createVersionChecker(moduleName, expectedVersion) {
  49. var memo;
  50. var warn = function () {
  51. if (memo !== undefined)
  52. return memo;
  53. return (memo = checkVersion(moduleName, expectedVersion, 'warn'));
  54. };
  55. var raise = function () { return checkVersion(moduleName, expectedVersion, 'throw'); };
  56. return {
  57. raise: raise,
  58. warn: warn,
  59. forget: function () {
  60. memo = undefined;
  61. },
  62. };
  63. }