paths-to-module-name-mapper.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var _a;
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var bs_logger_1 = require("bs-logger");
  16. var logger_1 = require("../util/logger");
  17. var messages_1 = require("../util/messages");
  18. var escapeRegex = function (str) { return str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'); };
  19. var logger = logger_1.rootLogger.child((_a = {}, _a[bs_logger_1.LogContexts.namespace] = 'path-mapper', _a));
  20. exports.pathsToModuleNameMapper = function (mapping, _a) {
  21. var e_1, _b;
  22. var _c = (_a === void 0 ? {} : _a).prefix, prefix = _c === void 0 ? '' : _c;
  23. var jestMap = {};
  24. try {
  25. for (var _d = __values(Object.keys(mapping)), _e = _d.next(); !_e.done; _e = _d.next()) {
  26. var fromPath = _e.value;
  27. var pattern = void 0;
  28. var toPaths = mapping[fromPath];
  29. if (toPaths.length === 0) {
  30. logger.warn(messages_1.interpolate(messages_1.Errors.NotMappingPathWithEmptyMap, { path: fromPath }));
  31. continue;
  32. }
  33. else if (toPaths.length > 1) {
  34. logger.warn(messages_1.interpolate(messages_1.Errors.MappingOnlyFirstTargetOfPath, {
  35. path: fromPath,
  36. count: toPaths.length,
  37. }));
  38. }
  39. var target = toPaths[0];
  40. var segments = fromPath.split(/\*/g);
  41. if (segments.length === 1) {
  42. pattern = "^" + escapeRegex(fromPath) + "$";
  43. jestMap[pattern] = "" + prefix + target;
  44. }
  45. else if (segments.length === 2) {
  46. pattern = "^" + escapeRegex(segments[0]) + "(.*)" + escapeRegex(segments[1]) + "$";
  47. jestMap[pattern] = "" + prefix + target.replace(/\*/g, '$1');
  48. }
  49. else {
  50. logger.warn(messages_1.interpolate(messages_1.Errors.NotMappingMultiStarPath, { path: fromPath }));
  51. continue;
  52. }
  53. }
  54. }
  55. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  56. finally {
  57. try {
  58. if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
  59. }
  60. finally { if (e_1) throw e_1.error; }
  61. }
  62. return jestMap;
  63. };