utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isRequire = exports.createImport = exports.getModulePath = exports.isPolyfillSource = exports.filterStageFromList = exports.getLowestUnreleased = exports.isUnreleasedVersion = exports.prettifyTargets = exports.prettifyVersion = exports.findSuggestion = exports.getValues = exports.semverify = void 0;
  6. function _invariant() {
  7. const data = _interopRequireDefault(require("invariant"));
  8. _invariant = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _semver() {
  14. const data = _interopRequireDefault(require("semver"));
  15. _semver = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _jsLevenshtein() {
  21. const data = _interopRequireDefault(require("js-levenshtein"));
  22. _jsLevenshtein = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _helperModuleImports() {
  28. const data = require("@babel/helper-module-imports");
  29. _helperModuleImports = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _unreleasedLabels = _interopRequireDefault(require("../data/unreleased-labels"));
  35. var _targetsParser = require("./targets-parser");
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. const versionRegExp = /^(\d+|\d+.\d+)$/;
  38. const semverify = version => {
  39. if (typeof version === "string" && _semver().default.valid(version)) {
  40. return version;
  41. }
  42. (0, _invariant().default)(typeof version === "number" || typeof version === "string" && versionRegExp.test(version), `'${version}' is not a valid version`);
  43. const split = version.toString().split(".");
  44. while (split.length < 3) {
  45. split.push("0");
  46. }
  47. return split.join(".");
  48. };
  49. exports.semverify = semverify;
  50. const getValues = object => Object.keys(object).map(key => object[key]);
  51. exports.getValues = getValues;
  52. const findSuggestion = (options, option) => {
  53. let levenshteinValue = Infinity;
  54. return options.reduce((suggestion, validOption) => {
  55. const value = (0, _jsLevenshtein().default)(validOption, option);
  56. if (value < levenshteinValue) {
  57. levenshteinValue = value;
  58. return validOption;
  59. }
  60. return suggestion;
  61. }, undefined);
  62. };
  63. exports.findSuggestion = findSuggestion;
  64. const prettifyVersion = version => {
  65. if (typeof version !== "string") {
  66. return version;
  67. }
  68. const parts = [_semver().default.major(version)];
  69. const minor = _semver().default.minor(version);
  70. const patch = _semver().default.patch(version);
  71. if (minor || patch) {
  72. parts.push(minor);
  73. }
  74. if (patch) {
  75. parts.push(patch);
  76. }
  77. return parts.join(".");
  78. };
  79. exports.prettifyVersion = prettifyVersion;
  80. const prettifyTargets = targets => {
  81. return Object.keys(targets).reduce((results, target) => {
  82. let value = targets[target];
  83. const unreleasedLabel = _unreleasedLabels.default[target];
  84. if (typeof value === "string" && unreleasedLabel !== value) {
  85. value = prettifyVersion(value);
  86. }
  87. results[target] = value;
  88. return results;
  89. }, {});
  90. };
  91. exports.prettifyTargets = prettifyTargets;
  92. const isUnreleasedVersion = (version, env) => {
  93. const unreleasedLabel = _unreleasedLabels.default[env];
  94. return !!unreleasedLabel && unreleasedLabel === version.toString().toLowerCase();
  95. };
  96. exports.isUnreleasedVersion = isUnreleasedVersion;
  97. const getLowestUnreleased = (a, b, env) => {
  98. const unreleasedLabel = _unreleasedLabels.default[env];
  99. const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
  100. if (hasUnreleased) {
  101. return a === hasUnreleased ? b : a || b;
  102. }
  103. return (0, _targetsParser.semverMin)(a, b);
  104. };
  105. exports.getLowestUnreleased = getLowestUnreleased;
  106. const filterStageFromList = (list, stageList) => {
  107. return Object.keys(list).reduce((result, item) => {
  108. if (!stageList[item]) {
  109. result[item] = list[item];
  110. }
  111. return result;
  112. }, {});
  113. };
  114. exports.filterStageFromList = filterStageFromList;
  115. const isPolyfillSource = source => source === "@babel/polyfill" || source === "core-js";
  116. exports.isPolyfillSource = isPolyfillSource;
  117. const modulePathMap = {
  118. "regenerator-runtime": "regenerator-runtime/runtime"
  119. };
  120. const getModulePath = mod => modulePathMap[mod] || `core-js/modules/${mod}`;
  121. exports.getModulePath = getModulePath;
  122. const createImport = (path, mod) => (0, _helperModuleImports().addSideEffect)(path, getModulePath(mod));
  123. exports.createImport = createImport;
  124. const isRequire = (t, path) => t.isExpressionStatement(path.node) && t.isCallExpression(path.node.expression) && t.isIdentifier(path.node.expression.callee) && path.node.expression.callee.name === "require" && path.node.expression.arguments.length === 1 && t.isStringLiteral(path.node.expression.arguments[0]) && isPolyfillSource(path.node.expression.arguments[0].value);
  125. exports.isRequire = isRequire;